2017-04-25 425 views
1
# 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_}) 

基本上我不知道它为什么返回无。为什么我的tf_gradients返回None?

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

我试着添加打印语句,我可以看到prob_和LOG_出来就好了,但我不知道正在发生的事情,是造成上述问题的tf.gradients。

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

回答

0

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

因此,尽管技术上梯度应为零,但由于technical reasons in TensorFlow,因此计算为None

+0

太棒了!那么一种方法是让model.output是prob_placeholder?这样prob_placeholder与model.weights间接相关吗? – user49593

+0

@ user162264通常的方法是将输出标签保存在'tf.placeholder'中,然后计算一个损失函数,该函数采用NN的真实标签和输出并计算一个值。然后可以计算损失w.r.t的梯度。模型变量。 –

相关问题