I have an autoencoder and I try to use the specific value of input layer in in intermediate layer using lambda and produce a new tensor and send to next layers but it produces this error:
Traceback (most recent call last):
File "", line 99, in model=Model(inputs=[image,wtm],outputs=decoded)
File "D:\software\Anaconda3\envs\py36\lib\site-packages\keras\legacy\interfaces.py", line 91, in wrapper return func(*args, **kwargs)
File "D:\software\Anaconda3\envs\py36\lib\site-packages\keras\engine\network.py", line 93, in init self._init_graph_network(*args, **kwargs)
File "D:\software\Anaconda3\envs\py36\lib\site-packages\keras\engine\network.py", line 231, in _init_graph_network self.inputs, self.outputs)
File "D:\software\Anaconda3\envs\py36\lib\site-packages\keras\engine\network.py", line 1366, in _map_graph_network tensor_index=tensor_index)
File "D:\software\Anaconda3\envs\py36\lib\site-packages\keras\engine\network.py", line 1353, in build_map node_index, tensor_index)
File "D:\software\Anaconda3\envs\py36\lib\site-packages\keras\engine\network.py", line 1353, in build_map node_index, tensor_index)
File "D:\software\Anaconda3\envs\py36\lib\site-packages\keras\engine\network.py", line 1353, in build_map node_index, tensor_index)
File "D:\software\Anaconda3\envs\py36\lib\site-packages\keras\engine\network.py", line 1353, in build_map node_index, tensor_index)
File "D:\software\Anaconda3\envs\py36\lib\site-packages\keras\engine\network.py", line 1353, in build_map node_index, tensor_index)
File "D:\software\Anaconda3\envs\py36\lib\site-packages\keras\engine\network.py", line 1353, in build_map node_index, tensor_index)
File "D:\software\Anaconda3\envs\py36\lib\site-packages\keras\engine\network.py", line 1353, in build_map node_index, tensor_index)
File "D:\software\Anaconda3\envs\py36\lib\site-packages\keras\engine\network.py", line 1325, in build_map node = layer._inbound_nodes[node_index]
AttributeError: 'NoneType' object has no attribute '_inbound_nodes'
this is my code and after adding the first lambda layer it produces this error! could you please tell why this error has happened? I appreciate your help? the thing I need is sth like this:
wtm={[0,1,1,0],[0,1,1,0],[0,0,0,0],[0,1,0,0]}
I choose wtm[:,i,j]
and produce new tensor with shape (28,28,1)
and with value wtm[:,i,j].
wt_random=np.random.randint(2, size=(49999,4,4))
w_expand=wt_random.astype(np.float32)
wv_random=np.random.randint(2, size=(9999,4,4))
wv_expand=wv_random.astype(np.float32)
#w_expand[:,:4,:4]=wt_random
#wv_expand[:,:4,:4]=wv_random
x,y,z=w_expand.shape
w_expand=w_expand.reshape((x,y,z,1))
x,y,z=wv_expand.shape
wv_expand=wv_expand.reshape((x,y,z,1))
#-----------------building w test---------------------------------------------
w_test = np.random.randint(2,size=(1,4,4))
w_test=w_test.astype(np.float32)
#wt_expand=np.zeros((1,28,28),dtype='float32')
#wt_expand[:,0:4,0:4]=w_test
w_test=w_test.reshape((1,4,4,1))
wtm=Input((4,4,1))
image = Input((28, 28, 1))
conv1 = Conv2D(64, (5, 5), activation='relu', padding='same', name='convl1e')(image)
conv2 = Conv2D(64, (5, 5), activation='relu', padding='same', name='convl2e')(conv1)
conv3 = Conv2D(64, (5, 5), activation='relu', padding='same', name='convl3e')(conv2)
BN=BatchNormalization()(conv3)
encoded = Conv2D(1, (5, 5), activation='relu', padding='same',name='encoded_I')(BN)
rep=Kr.layers.Lambda(lambda x:Kr.backend.repeat(x,28))
a=rep(Kr.layers.Lambda(lambda x:x[1,1])(wtm))
add_const = Kr.layers.Lambda(lambda x: x[0] + x[1])
encoded_merged = add_const([encoded,a])
#-----------------------decoder------------------------------------------------
#------------------------------------------------------------------------------
deconv1 = Conv2D(64, (5, 5), activation='elu', padding='same', name='convl1d')(encoded_merged)
deconv2 = Conv2D(64, (5, 5), activation='elu', padding='same', name='convl2d')(deconv1)
deconv3 = Conv2D(64, (5, 5), activation='elu',padding='same', name='convl3d')(deconv2)
deconv4 = Conv2D(64, (5, 5), activation='elu',padding='same', name='convl4d')(deconv3)
BNd=BatchNormalization()(deconv4)
#DrO2=Dropout(0.25,name='DrO2')(BNd)
decoded = Conv2D(1, (5, 5), activation='sigmoid', padding='same', name='decoder_output')(BNd)
#model=Model(inputs=image,outputs=decoded)
model=Model(inputs=[image,wtm],outputs=decoded)
decoded_noise = GaussianNoise(0.5)(decoded)
#----------------------w extraction------------------------------------
convw1 = Conv2D(64, (5,5), activation='relu', name='conl1w')(decoded_noise)#24
convw2 = Conv2D(64, (5,5), activation='relu', name='convl2w')(convw1)#20
#Avw1=AveragePooling2D(pool_size=(2,2))(convw2)
convw3 = Conv2D(64, (5,5), activation='relu' ,name='conl3w')(convw2)#16
convw4 = Conv2D(64, (5,5), activation='relu' ,name='conl4w')(convw3)#12
#Avw2=AveragePooling2D(pool_size=(2,2))(convw4)
convw5 = Conv2D(64, (5,5), activation='relu', name='conl5w')(convw4)#8
convw6 = Conv2D(64, (5,5), activation='relu', name='conl6w')(convw5)#4
convw7 = Conv2D(64, (5,5), activation='relu',padding='same', name='conl7w',dilation_rate=(2,2))(convw6)#4
convw8 = Conv2D(64, (5,5), activation='relu', padding='same',name='conl8w',dilation_rate=(2,2))(convw7)#4
convw9 = Conv2D(64, (5,5), activation='relu',padding='same', name='conl9w',dilation_rate=(2,2))(convw8)#4
convw10 = Conv2D(64, (5,5), activation='relu',padding='same', name='conl10w',dilation_rate=(2,2))(convw9)#4
BNed=BatchNormalization()(convw10)
pred_w = Conv2D(1, (1, 1), activation='sigmoid', padding='same', name='reconstructed_W',dilation_rate=(2,2))(BNed)
w_extraction=Model(inputs=[image,wtm],outputs=[decoded,pred_w])
w_extraction.summary()
(x_train, _), (x_test, _) = mnist.load_data()
x_validation=x_train[1:10000,:,:]
x_train=x_train[10001:60000,:,:]
#
x_train = x_train.astype('float32') / 255.
x_test = x_test.astype('float32') / 255.
x_validation = x_validation.astype('float32') / 255.
x_train = np.reshape(x_train, (len(x_train), 28, 28, 1)) # adapt this if using `channels_first` image data format
x_test = np.reshape(x_test, (len(x_test), 28, 28, 1)) # adapt this if using `channels_first` image data format
x_validation = np.reshape(x_validation, (len(x_validation), 28, 28, 1))
#---------------------compile and train the model------------------------------
opt=SGD(momentum=0.99)
w_extraction.compile(optimizer='adam', loss={'decoder_output':'mse','reconstructed_W':'binary_crossentropy'}, loss_weights={'decoder_output': 0.2, 'reconstructed_W': 1.0},metrics=['mae'])
es = EarlyStopping(monitor='val_loss', mode='min', verbose=1, patience=40)
#rlrp = ReduceLROnPlateau(monitor='val_loss', factor=0.1, patience=20, min_delta=1E-4, verbose=1)
mc = ModelCheckpoint('best_model_5x5F_dp_gn.h5', monitor='val_loss', mode='min', verbose=1, save_best_only=True)
history=w_extraction.fit([x_train,w_expand], [x_train,w_expand],
epochs=1,
batch_size=64,
validation_data=([x_validation,wv_expand], [x_validation,wv_expand]),
callbacks=[TensorBoard(log_dir='E:concatnatenetwork', histogram_freq=0, write_graph=False),es,mc])
when I implement it, this error is shown:
Traceback (most recent call last):
File "", line 1, in encoded_merged = add_const([encoded,a])
File "D:\software\Anaconda3\envs\py36\lib\site-packages\keras\engine\base_layer.py", line 457, in call output = self.call(inputs, **kwargs)
File "D:\software\Anaconda3\envs\py36\lib\site-packages\keras\layers\core.py", line 687, in call return self.function(inputs, **arguments)
File "", line 1, in add_const = Kr.layers.Lambda(lambda x: x[0] + x1)
File "D:\software\Anaconda3\envs\py36\lib\site-packages\tensorflow\python\ops\math_ops.py", line 866, in binary_op_wrapper return func(x, y, name=name)
File "D:\software\Anaconda3\envs\py36\lib\site-packages\tensorflow\python\ops\gen_math_ops.py", line 301, in add "Add", x=x, y=y, name=name)
File "D:\software\Anaconda3\envs\py36\lib\site-packages\tensorflow\python\framework\op_def_library.py", line 787, in _apply_op_helper op_def=op_def)
File "D:\software\Anaconda3\envs\py36\lib\site-packages\tensorflow\python\util\deprecation.py", line 488, in new_func return func(*args, **kwargs)
File "D:\software\Anaconda3\envs\py36\lib\site-packages\tensorflow\python\framework\ops.py", line 3274, in create_op op_def=op_def)
File "D:\software\Anaconda3\envs\py36\lib\site-packages\tensorflow\python\framework\ops.py", line 1792, in init control_input_ops)
File "D:\software\Anaconda3\envs\py36\lib\site-packages\tensorflow\python\framework\ops.py", line 1631, in _create_c_op raise ValueError(str(e))
ValueError: Dimensions must be equal, but are 28 and 4 for 'lambda_9/add' (op: 'Add') with input shapes: [?,28,28,1], [4,28,1].