如何在TensorFlow中使用渐变作为输入

时间:2017-06-25 10:42:03

标签: python tensorflow

我正在尝试使用网络特定层的渐变作为下一层的输入(相对于图层权重的导数)。我的代码太长了,无法将其全部放在这里,但下面是有问题的部分(仅在插入后才会破坏)

weights = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, scope="conv5/weights:0")
deriv = tf.gradients(net, weights)
deriv = tf.squeeze(deriv)
deriv = tf.expand_dims(deriv, 0)
batch_deriv = tf.tile(deriv, [batch_size, 1, 1])
h = tf.reduce_sum(net, axis=1)# maybe normalize ?
new_feature_map = tf.concat([h, batch_deriv], axis=1)

我收到以下错误:(在优化器中,而不是在我的模型中,但我不知道为什么)

 File "/usr/lib/pycharm-community/helpers/pydev/pydevd.py", line 1596, in <module>
    globals = debugger.run(setup['file'], None, None, is_module)
  File "/usr/lib/pycharm-community/helpers/pydev/pydevd.py", line 974, in run
    pydev_imports.execfile(file, globals, locals)  # execute the script
  File ".../train.py", line 272, in <module>
    train()
  File ".../train.py", line 133, in train
    train_op = optimizer.minimize(loss, global_step=batch)
  File ".../local/lib/python2.7/site-packages/tensorflow/python/training/optimizer.py", line 315, in minimize
    grad_loss=grad_loss)
  File ".../local/lib/python2.7/site-packages/tensorflow/python/training/optimizer.py", line 386, in compute_gradients
    colocate_gradients_with_ops=colocate_gradients_with_ops)
  File ".../local/lib/python2.7/site-packages/tensorflow/python/ops/gradients_impl.py", line 551, in gradients
    out_grads[i] = control_flow_ops.ZerosLikeOutsideLoop(op, i)
  File ".../local/lib/python2.7/site-packages/tensorflow/python/ops/control_flow_ops.py", line 1305, in ZerosLikeOutsideLoop
    pred = op_ctxt.pred
AttributeError: 'NoneType' object has no attribute 'pred'

任何人都知道为什么会发生这种情况以及如何解决它?

更新

似乎改变了行

new_feature_map = tf.concat([h, batch_deriv], axis=1) 

new_feature_map = tf.concat([h, tf.stop_gradient(batch_deriv) ], axis=1)

“修复”它(它不再是dowsnt错误),但我不知道为什么。另外 - 在评估期间(在测试集上),似乎不计算梯度。

任何人都知道如何解决这个问题?

0 个答案:

没有答案