gpt4 book ai didi

tensorflow - 对 conv2d_transpose 感到困惑

转载 作者:行者123 更新时间:2023-12-02 22:52:33 25 4
gpt4 key购买 nike

我在使用 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 是其后向对应项,因此它在输入、输出和滤波器形状之间具有相同的关系(但输入和输出的角色相反。)

希望有帮助!

关于tensorflow - 对 conv2d_transpose 感到困惑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35488717/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com