python-3.x - 最小化 AdamOptimizer 时,运算输入和计算输入梯度之间的形状不兼容

标签 python-3.x tensorflow deconvolution

运行我的 tensorflow 代码时遇到以下错误:

Traceback (most recent call last):
  File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/framework/tensor_shape.py", line 579, in merge_with
    new_dims.append(dim.merge_with(other[i]))
  File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/framework/tensor_shape.py", line 138, in merge_with
    self.assert_is_compatible_with(other)
  File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/framework/tensor_shape.py", line 111, in assert_is_compatible_with
    other))
ValueError: Dimensions 5 and 4 are not compatible

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/ops/gradients_impl.py", line 602, in gradients
    in_grad.set_shape(t_in.get_shape())
  File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/framework/ops.py", line 407, in set_shape
    self._shape = self._shape.merge_with(shape)
  File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/framework/tensor_shape.py", line 582, in merge_with
    raise ValueError("Shapes %s and %s are not compatible" % (self, other))
ValueError: Shapes (?, 5, 15, 1) and (?, 4, 15, 1) are not compatible

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "experiment.py", line 65, in <module>
    batches_per_lot=batches_per_lot, sigma=dp_sigma, dp=dp)
  File "/home/srikrishna/Research/RGAN_kinect/RGAN_forecasting/model.py", line 247, in GAN_solvers
    G_solver = tf.train.AdamOptimizer().minimize(G_loss_mean_over_batch, var_list=generator_vars)
  File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/training/optimizer.py", line 343, in minimize
    grad_loss=grad_loss)
  File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/training/optimizer.py", line 414, in compute_gradients
    colocate_gradients_with_ops=colocate_gradients_with_ops)
  File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/ops/gradients_impl.py", line 609, in gradients
    % (op.name, i, t_in.shape, in_grad.shape))
ValueError: Incompatible shapes between op input and calculated input gradient.  Forward operation: generator/conv2d_transpose_1.  Input index: 2. Original input shape: (?, 4, 15, 1).  Calculated input gradient shape: (?, 5, 15, 1)

我正在尝试使用一些卷积和 conv2d_transpose。该错误来自最小化操作期间的 conv2d_transpose 层。不确定为什么会发生。以下是我构建网络的方法:

deconv1 = tf.nn.conv2d_transpose(output_3d, tf.get_variable('dw1', shape=[4, 4, 1, 1], initializer=tf.random_normal_initializer()),
                                         strides=[1, 2, 3, 1], output_shape=[-1, 4, 15, 1])
        de_relu1 = tf.nn.relu(deconv1, 'de_relu1')
deconv2 = tf.nn.conv2d_transpose(de_relu1, tf.get_variable('dw2', shape=[5, 5, 1, 1], initializer=tf.random_normal_initializer()),
                                         strides=[1, 4, 5, 1], output_shape=[-1, 20, 75, 1])
        de_relu2 = tf.nn.relu(deconv2, 'de_relu2')

我使用的是tensorflow 1.4.1

最佳答案

好吧,事实证明我以错误的方式解释了错误。问题是我为 deconv2 给出的输出形状不正确。事实证明,由于 conv2d_transpose 在 tensorflow 中的实现方式,所以 conv2d_transpose 的形状验证是在前向传播步骤中完成的。

strides = [1, 4, 5, 1] 更改为 strides=[1, 5, 5, 1] 使其工作正常。

关于python-3.x - 最小化 AdamOptimizer 时,运算输入和计算输入梯度之间的形状不兼容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49809177/

相关文章:

python - @staticmethod 装饰器有什么作用吗?

python - 创建三个类而不是两个 - keras 库

python - tf.group 和 tensorflow 集合有什么不同?

python - TensorFlow strided_slice 不会在整个范围内向后迭代

python - 转置卷积(反卷积)算法

python-3.x - Django Rest Framework 在 Azure 上非常慢

python - 如何将虚拟值填充为数据框中的模式?

python-3.x - Tensorflow 对象检测 API 培训问题

matlab - 使用Matlab对声音进行反卷积

python - 给定原始和复杂的一维数据的核估计