keras-team / keras

Deep Learning for humans
http://keras.io/
Apache License 2.0
61.98k stars 19.48k forks source link

TypeError: The added layer must be an instance of class Layer. Found: Tensor("input_12:0", shape=(?, 28, 28), dtype=float32) #14983

Closed mbarraghi closed 3 years ago

mbarraghi commented 3 years ago

hi I work on a code and faced this error. I would be grateful if you could help me class SOMLayer(Layer): def init(self,out_dim,kernel, strides,map_size,input_dim,kwargs): super(SOMLayer,self).init(kwargs) self.out_dim = out_dim self.kernel = kernel self.strides = strides self.map_size = map_size self.input_dim = input_dim self.input_side = input_dim[0] self.max_patches = math.ceil((self.input_side - self.kernel) // 2 +1) self.max_patches = self.max_patches*self.max_patches

def call(self, x):
    patches = image.extract_patches_2d(x, patch_size =(self.kernel, self.kernel), max_patches=self.max_patches)
    patches = patches.reshape((max_patches,self.kernel*self.kernel))
    som = MiniSom(self.map_size, self.map_size,self.kernel*self.kernel , sigma=0.3, learning_rate=0.5,neighborhood_function='gaussian') 
    som.pca_weights_init(patches)
    som.train_random(patches, 100)
    wins=[]
    for j in range (0,self.max_patches):
        win = som.winner(patches[i])
        win = patches.__getitem__(win)
        wins.append(win)
    wins = np.array(wins)
    return wins

class Sample(Layer): def init(self,out_dim, kwargs): self.out_dim = out_dim super(Sample,self).init(kwargs)

def call(self,x):
    sample_mat = np.reshape(x, (out_dim,out_dim))
    #sample_mat = np.array(sample_mat)
    return sample_mat

(train_images , train_labels), (test_images , test_labels) = mnist.load_data()

train_images = train_images.reshape((60000 , 28 * 28))

train_images = train_images.astype('float32') / 255

test_images = test_images.reshape((10000 , 28 * 28))

test_images = test_images.astype('float32') / 255 train_labels = to_categorical (train_labels) test_labels = to_categorical (test_labels)

x_train= train_images[:4000]

x_validation=train_images[4000:]

x_labels = train_labels[:4000]

y_validation = train_labels[40000:]

model1 = Sequential( [ keras.Input(shape=(28,28)), SOMLayer(100, kernel = 10, strides = 2,map_size = 20, input_dim=(28,28)),
Sample(out_dim = 10),
SOMLayer(25, kernel = 6, strides = 1,map_size = 15,input_dim=(10,10)), Sample(out_dim = 5),
SOMLayer(1, kernel = 5, strides = 1,map_size = 8,input_dim=(5,5)) ] )

TypeError Traceback (most recent call last)

in 6 SOMLayer(25, kernel = 6, strides = 1,map_size = 15,input_dim=(10,10)), 7 Sample(out_dim = 5), ----> 8 SOMLayer(1, kernel = 5, strides = 1,map_size = 8,input_dim=(5,5)) 9 ] 10 ) C:\ProgramData\Anaconda3\lib\site-packages\keras\engine\sequential.py in __init__(self, layers, name) 92 if layers: 93 for layer in layers: ---> 94 self.add(layer) 95 96 @property C:\ProgramData\Anaconda3\lib\site-packages\keras\engine\sequential.py in add(self, layer) 131 raise TypeError('The added layer must be ' 132 'an instance of class Layer. ' --> 133 'Found: ' + str(layer)) 134 self.built = False 135 if not self._layers: TypeError: The added layer must be an instance of class Layer. Found: Tensor("input_12:0", shape=(?, 28, 28), dtype=float32)
ariG23498 commented 3 years ago

Hey @mbarraghi

Could you use keras.layers.InputLayer instead of keras.Input as the first layer and see if that works? It is suggested that with a Sequential model we used an InputLayer and with the Functional API we use the Input layer.

rmothukuru commented 3 years ago

@mbarraghi, This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you.

rmothukuru commented 3 years ago

@mbarraghi, Can you please let us know if this comment has resolved your issue? Thanks!

google-ml-butler[bot] commented 3 years ago

This issue has been automatically marked as stale because it has no recent activity. It will be closed if no further activity occurs. Thank you.