Tensorflow:反捲積,解決AdamOptimizer報錯ValueError
阿新 • • 發佈:2018-12-25
使用tensorflow把GAN搭建好了,除錯過程出現ValueError: Shapes (100, 14, 14, 64) and (100, 12, 12, 64) are not compatible,這個小錯誤差點把人弄崩潰,經過一番折騰,終於解決,直接給出該錯誤的問題出處吧:
def deconv_layer(input,kernel_size_x,kernel_size_y,channel_in,channel_out,output_shape_n,isnorm=True,name="conv",active="relu"): with tf.name_scope(name): w = tf.Variable(tf.truncated_normal(shape=[kernel_size_x,kernel_size_y,channel_in,channel_out], stddev=0.01), name="W") b = tf.Variable(tf.zeros([channel_in])+0.1, name="B") conv = tf.nn.conv2d_transpose(input,w,output_shape=output_shape_n,strides=[1,2,2,1],padding="SAME") if isnorm: conv = tf.contrib.layers.batch_norm(inputs = conv, center=True, scale=True, is_training=True) if active == "relu": act = tf.nn.relu(conv + b) if active == "tanh": act = tf.nn.tanh(conv + b) return act
在我的程式碼中生成器的反捲積過程為:1. —>2. —>3. —>4. —>5. —>6. 。錯誤為在5. –>6. 中反捲積操作tf.nn.conv2d_transpose裡的填充模式為"SAME",使得Adam後向傳播的時候無法計算梯度。
解決方案:在5. –>6. 中的填充模式改為VALID即可。