加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

python-3.x – 在最小化AdamOptimizer时,在op输入和计算输入渐变

发布时间:2020-12-20 11:03:51 所属栏目:Python 来源:网络整理
导读:运行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/loc
运行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,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: (?,1).  Calculated input gradient shape: (?,1)

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

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

我正在使用tensorflow 1.4.1

解决方法

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

将步幅= [1,1]改为strides = [1,1]使其工作正常.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读