我在使用时收到此错误消息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)]]
Run Code Online (Sandbox Code Playgroud)
但是,它是在编译损失函数(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)
Run Code Online (Sandbox Code Playgroud)
小智 38
谢谢你的提问!你完全正确---问题是传递给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)
Run Code Online (Sandbox Code Playgroud)
我用常量替换了变量---更容易看出发生了什么.
如果您尝试运行此代码,则会收到类似的错误:
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)]]
Run Code Online (Sandbox Code Playgroud)
现在,错误有点误导 - 它讨论'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)
Run Code Online (Sandbox Code Playgroud)
也就是说,在向前方向上,如果你提供了一个形状'output_shape'的张量,你会得到一个形状的张量(3,4,4,4).因此解决问题的一种方法是将'l'的形状改为(3,4,4,4); 如果您将上面的代码更改为:
l = tf.constant(0.1, shape=[batch_size, 4, 4, 4])
Run Code Online (Sandbox Code Playgroud)
一切正常.
通常,尝试使用tf.nn.conv2d来了解张量形状之间的关系.由于tf.nn.conv2d_transpose是它的后向对应物,它在输入,输出和滤波器形状之间具有相同的关系(但输入和输出的作用相反).
希望有所帮助!
| 归档时间: |
|
| 查看次数: |
19586 次 |
| 最近记录: |