我如何解决此错误“形状必须为4级但为3级”

时间:2019-03-05 22:42:24

标签: python tensorflow keras

我有一个CNN,我想填充形状为(1,4,4,1)的输入,填充后其形状变为(1,28,28,1),然后将其与另一层的输出连接形状为(1,28,28,1)。我使用了这段代码

wpad=Kr.layers.Lambda(lambda xy: Kr.backend.spatial_2d_padding(xy[0], padding=((0, 24), (0, 24))))
wtmp=wpad([wtm])
encoded_merged = Concatenate(axis=3)([encoded, wtmp])

但是会产生此错误

  

ValueError:“ lambda_1 / Pad_1”的形状必须为4级,但必须为3级   (op:“ Pad”),其输入形状为[4,4,1],[4,2]。

我不知道该如何解决这个问题。请你帮助我好吗? 我把整个网络放在这里:

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)
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)
w_test=w_test.reshape((1,4,4,1))

#-----------------------encoder------------------------------------------------
#------------------------------------------------------------------------------
wtm=Input((4,4,1))
image = Input((28, 28, 1))
conv1 = Conv2D(64, (5, 5), activation='relu', padding='same', name='convl1e',dilation_rate=(2,2))(image)
conv2 = Conv2D(64, (5, 5), activation='relu', padding='same', name='convl2e',dilation_rate=(2,2))(conv1)
conv3 = Conv2D(64, (5, 5), activation='relu', padding='same', name='convl3e',dilation_rate=(2,2))(conv2)
#conv3 = Conv2D(8, (3, 3), activation='relu', padding='same', name='convl3e', kernel_initializer='Orthogonal',bias_initializer='glorot_uniform')(conv2)
BN=BatchNormalization()(conv3)
#DrO1=Dropout(0.25,name='Dro1')(BN)
encoded =  Conv2D(1, (5, 5), activation='relu', padding='same',name='encoded_I')(BN)

#-----------------------adding w---------------------------------------
wpad=Kr.layers.Lambda(lambda xy: Kr.backend.spatial_2d_padding(xy[0], padding=((0, 24), (0, 24))))
wtmp=wpad([wtm])
encoded_merged = Concatenate(axis=3)([encoded, wtmp])

#-----------------------decoder------------------------------------------------
#------------------------------------------------------------------------------
deconv1 = Conv2D(64, (5, 5), activation='relu', padding='same', name='convl1d',dilation_rate=(2,2))(encoded_merged)
deconv2 = Conv2D(64, (5, 5), activation='relu', padding='same', name='convl2d',dilation_rate=(2,2))(deconv1)
deconv3 = Conv2D(64, (5, 5), activation='relu',padding='same', name='convl3d',dilation_rate=(2,2))(deconv2)
deconv4 = Conv2D(64, (5, 5), activation='relu',padding='same', name='convl4d',dilation_rate=(2,2))(deconv3)
BNd=BatchNormalization()(deconv4)    
decoded = Conv2D(1, (5, 5), activation='sigmoid', padding='same', name='decoder_output')(BNd)     
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
convw3 = Conv2D(64, (5,5), activation='relu' ,name='conl3w')(convw2)#16
convw4 = Conv2D(64, (5,5), activation='relu' ,name='conl4w')(convw3)#12
convw5 = Conv2D(64, (5,5), activation='relu', name='conl5w')(convw4)#8
convw6 = Conv2D(64, (5,5), activation='relu', name='conl6w')(convw5)#4
BNed=BatchNormalization()(convw6)
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()

1 个答案:

答案 0 :(得分:0)

我发现了问题所在。我将代码更改为

wpad=Kr.layers.Lambda(lambda xy: Kr.backend.spatial_2d_padding(xy, padding=((0, 24), (0, 24)))) 
wtmp=wpad(wtm) 

成功了