使用时出现此错误消息conv2d_transpose
:
W tensorflow/core/common_runtime/executor.cc:1102] 0x7fc81f0d6250 Compute status: Invalid argument: Conv2DBackpropInput: Number of rows of out_backprop doesn't match computed: actual = 32, computed = 4
[[Node: generator/g_h1/conv2d_transpose = Conv2DBackpropInput[T=DT_FLOAT, padding="SAME", strides=[1, 2, 2, 1], use_cudnn_on_gpu=true, _device="/job:localhost/replica:0/task:0/cpu:0"](generator/g_h1/conv2d_transpose/output_shape, generator/g_h1/w/read, _recv_l_0)]]
但是,它是在构建损失函数(Adam)的图形生成后发生的。有什么想法会导致这种情况吗?我怀疑这与输入尺寸有关,但我不确定为什么。
完整错误:https://gist.github.com/jimfleming/75d88e888044615dd6e3
相关代码:
# l shape: [batch_size, 32, 32, 4]
output_shape = [self.batch_size, 8, 8, 128]
filter_shape = [7, 7, 128, l.get_shape()[-1]]
strides = [1, 2, 2, 1]
with tf.variable_scope("g_h1"):
w = tf.get_variable('w', filter_shape, initializer=tf.random_normal_initializer(stddev=0.02))
h1 = tf.nn.conv2d_transpose(l, w, output_shape=output_shape, strides=strides, padding='SAME')
h1 = tf.nn.relu(h1)
output_shape = [self.batch_size, 16, 16, 128]
filter_shape = [7, 7, 128, h1.get_shape()[-1]]
strides = [1, 2, 2, 1]
with tf.variable_scope("g_h2"):
w = tf.get_variable('w', filter_shape, initializer=tf.random_normal_initializer(stddev=0.02))
h2 = tf.nn.conv2d_transpose(h1, w,output_shape=output_shape, strides=strides, padding='SAME')
h2 = tf.nn.relu(h2)
output_shape = [self.batch_size, 32, 32, 3]
filter_shape = [5, 5, 3, h2.get_shape()[-1]]
strides = [1, 2, 2, 1]
with tf.variable_scope("g_h3"):
w = tf.get_variable('w', filter_shape, initializer=tf.random_normal_initializer(stddev=0.02))
h3 = tf.nn.conv2d_transpose(h2, w,output_shape=output_shape, strides=strides, padding='SAME')
h3 = tf.nn.tanh(h3)
谢谢你的问题!您说得很对-问题是传递给tf.nn.conv2d_transpose的输入和输出尺寸不一致。(在计算梯度时可能会检测到错误,但梯度计算不是问题。)
让我们来看一下代码的第一部分,并对其进行一些简化:
sess = tf.Session()
batch_size = 3
output_shape = [batch_size, 8, 8, 128]
strides = [1, 2, 2, 1]
l = tf.constant(0.1, shape=[batch_size, 32, 32, 4])
w = tf.constant(0.1, shape=[7, 7, 128, 4])
h1 = tf.nn.conv2d_transpose(l, w, output_shape=output_shape, strides=strides, padding='SAME')
print sess.run(h1)
我用常量替换了变量---更容易看到正在发生的事情。
如果尝试运行此代码,则会收到类似的错误:
InvalidArgumentError: Conv2DCustomBackpropInput: Size of out_backprop doesn't match computed: actual = 32, computed = 4
[[Node: conv2d_transpose_6 = Conv2DBackpropInput[T=DT_FLOAT, data_format="NHWC", padding="SAME", strides=[1, 2, 2, 1], use_cudnn_on_gpu=true, _device="/job:localhost/replica:0/task:0/cpu:0"](conv2d_transpose_6/output_shape, Const_25, Const_24)]]
现在,该错误有点误导了---它谈到了'Conv2DCustomBackpropInput'的'out_backprop'参数。关键是tf.nn.conv2d_transpose实际上只是tf.nn.conv2d的梯度,因此Tensorflow在内部使用相同的代码(Conv2DCustomBackpropInput)计算tf.nn.conv2d的梯度并计算tf.nn.conv2d_transpose。
该错误意味着,鉴于形状为“ l”和“ w”,您请求的“ output_shape”是不可能的。
由于tf.nn.conv2d_transpose是tf.nn.conv2d的后向(渐变)副本,因此查看正确形状应采用的一种方法是使用相应的正向操作:
output = tf.constant(0.1, shape=output_shape)
expected_l = tf.nn.conv2d(output, w, strides=strides, padding='SAME')
print expected_l.get_shape()
# Prints (3, 4, 4, 4)
也就是说,在向前方向上,如果提供形状为“ output_shape”的张量,则会得到形状为(3、4、4、4)的张量。因此,解决问题的一种方法是将'l'的形状更改为(3,4,4,4); 如果将上面的代码更改为:
l = tf.constant(0.1, shape=[batch_size, 4, 4, 4])
一切正常。
通常,尝试使用tf.nn.conv2d来了解张量形状之间的关系。由于tf.nn.conv2d_transpose是其后向对应项,因此它在输入,输出和过滤器形状之间具有相同的关系(但输入和输出的作用相反)。
希望有帮助!
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句