Tensorflow细节陷阱–TypeError: Fetch argument None has invalid type <class ‘NoneType’>
import tensorflow as tf
w1=tf.Variable([[1,2]])
w2=tf.Variable([[3,4]])
y=tf.matmul(w1,[[9],[10]])
grads=tf.gradients(y,[w1])
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
gradval=sess.run(grads)
print(gradval)
报错
TypeError: Fetch argument None has invalid type <class ‘NoneType’>
import tensorflow as tf
w1=tf.Variable([[***1.***,2]])
w2=tf.Variable([[***3.***,4]])
y=tf.matmul(w1,[[***9.***],[10]])
grads=tf.gradients(y,[w1])
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
gradval=sess.run(grads)
print(gradval)
运行成功
[array([[ 9., 10.]], dtype=float32)]
下面的加粗斜体代码为更改之处。这让我想起来,tensorflow在不指定数据类型时,默认数据格式为float32,数字后加上一个点(.)就是提示tensorflow,我在使用浮点数。稍微的改变就可以使代码运行成功,不得不说代码像数学一样娇贵,眼里容不得一点儿沙子。
Bug天天有,总在角落处!