为什么我的tf_gradients返回None?

时间:2017-04-25 07:45:37

标签: tensorflow

# Defining the tf ops
prob_placeholder = tf.placeholder(tf.float32, shape=(2))
log_placeholder = tf.log(prob_placeholder)
grads_placeholder = tf.gradients(ys=tf.log(prob_placeholder), xs=model.weights)


# t is some index into the holders (which are lists)
# s is some state || p_a is some list of [p_1, 1 - p_1] || a_ is either 0 or 1 ||  r is 1

prob_ = tf_sess.run(prob_placeholder, {prob_placeholder: p_a})
log_ = tf_sess.run(log_placeholder, {prob_placeholder: prob_})
print(prob_, log_)
grads_ = tf_sess.run(grads_placeholder, {prob_placeholder: prob_})

基本上我不确定为什么它会返回无。

TypeError: Fetch argument None has invalid type <type 'NoneType'>

我已经尝试添加print语句,我可以看到prob_和log_出来就好了,但我不确定导致上述问题的tf.gradients中发生了什么。

model.weights基本上是我正在使用的模型的权重。

1 个答案:

答案 0 :(得分:4)

prob_placeholder没有明确依赖model.weights,即它在功能上不依赖于model.weights您定义它们的方式。

因此,虽然从技术上讲,渐变应该为零,但由于technical reasons in TensorFlow,它的计算结果为None