1. 程式人生 > >Tensorflow:反捲積,解決AdamOptimizer報錯ValueError

Tensorflow:反捲積,解決AdamOptimizer報錯ValueError

使用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. b a t c h s i z e

100 batchsize*100 —>2. b a t c h s
i z e 2 2 25 batchsize*2*2*25
—>3. b a t c h s i z e 3 3 256 batchsize*3*3*256 —>4. b a t c h s i z e 6 6 128 batchsize*6*6*128 —>5. b a t c h s i z e 12 12 64 batchsize*12*12*64 —>6. b a t c h s i z e 28 28 1 batchsize*28*28*1 。錯誤為在5. b a t c h s i z e 12 12 64 batchsize*12*12*64 –>6. b a t c h s i z e 28 28 1 batchsize*28*28*1 中反捲積操作tf.nn.conv2d_transpose裡的填充模式為"SAME",使得Adam後向傳播的時候無法計算梯度。

解決方案:在5. b a t c h s i z e 12 12 64 batchsize*12*12*64 –>6. b a t c h s i z e 28 28 1 batchsize*28*28*1 中的填充模式改為VALID即可。