我试图将conv1d_transpose与VALID填充一起用于一维生成对抗网络,并不断出现以下错误:
ValueError: Incompatible shapes between op input and calculated input gradient. Forward operation: g_Generator_Output/Generator_Output. Input index: 2. Original input shape: (?, 1, 256, 32). Calculated input gradient shape: (?, 1, 253, 32)
我的理解是输出宽度应使用:
W_out =(W_in-1)*步幅+过滤宽度
在我的情况下为256 =(253-1)* 1 + 4
按照错误消息的建议,我尝试将W_out设置为253,但这会返回以下错误
ValueError: Incompatible shapes between op input and calculated input gradient. Forward operation: g_gen_Deconv2/gen_Deconv2. Input index: 2. Original input shape: (?, 1, 253, 32). Calculated input gradient shape: (?, 1, 250, 32)
相关代码如下,其中self.dc [i-1]是反卷积的输入:
for i in range(1,len(deconv_shape)):
with tf.name_scope(self.name+'_'+deconv_shape[i][-1]):
batch_size=tf.shape(self.dc[i-1])[0]
if deconv_shape[i][3]=='VALID':
W_out=(self.dc[i-1].shape[1].value-1)*deconv_shape[i][2]+deconv_shape[i][0][0]
#W_out=253
deconv_shape[i][1]=tf.stack([batch_size,W_out,deconv_shape[i][1][2]])
deconv_shape[i][0]=tf.get_variable(self.name+'_'+deconv_shape[i][-1]+'_filters', deconv_shape[i][0], initializer=tf.random_normal_initializer(stddev=0.02))
self.dc.append(tf.contrib.nn.conv1d_transpose(self.dc[i-1],*deconv_shape[i]))
if BATCH_NORMALISE:
self.dc[i]=tf.layers.batch_normalization(self.dc[i],training=self.training,name=self.name+'_'+deconv_shape[i][-1]+'_BN')
tf.summary.histogram('dc'+str(i),self.dc[i])
和deconv_shape [i]定义如下:
[[kernel_size,filters,infilters],[BATCH_SIZE,1,filters],strides,'VALID','NWC','gen_Deconv'+str(i)]
kernel_size = 4,filters = infilters = 32,步幅= 1