对 conv2d_transpose 感到困惑

Confused about conv2d_transpose

我在使用 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.

该错误意味着您请求的 'output_shape' 不可能,给定 'l' 和 'w' 的形状。

由于 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 是它的反向对应物,它在输入、输出和滤波器形状之间具有相同的关系(但输入和输出的角色相反。)

希望对您有所帮助!

tf.nn.conv2d_transpose() 函数中使用 padding='SAME' 也可能有效