![](/img/trans.png)
[英]Tensorflow TypeError: Fetch argument None has invalid type <type 'NoneType'>?
[英]Tensorflow Error when using tf.gradients: TypeError: Fetch argument None has invalid type <type 'NoneType'>
我刚开始学习tensorflow
并在使用tf.gradients
函数时遇到以下错误。 下面给出了tf.gradients
的代码和错误。
Python代码:
import tensorflow as tf
if __name__ =='__main__':
os.environ['CUDA_VISIBLE_DEVICES'] = "0"
w1 = tf.Variable([[1, 2]])
w2 = tf.Variable([[2], [1]])
res = tf.matmul(w1,w2)
grads = tf.gradients(res, [w1,w2])
with tf.Session() as sess:
tf.global_variables_initializer().run()
print(sess.run(res))
re = sess.run(grads)
print(re)
错误:
[[4]]
re = sess.run(grads)
File "/home/BH/fyn123456/.conda/envs/py36/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 929, in run
run_metadata_ptr)
File "/home/BH/fyn123456/.conda/envs/py36/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 1137, in _run
self._graph, fetches, feed_dict_tensor, feed_handles=feed_handles)
File "/home/BH/fyn123456/.conda/envs/py36/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 471, in __init__
self._fetch_mapper = _FetchMapper.for_fetch(fetches)
File "/home/BH/fyn123456/.conda/envs/py36/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 261, in for_fetch
return _ListFetchMapper(fetch)
File "/home/BH/fyn123456/.conda/envs/py36/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 370, in __init__
self._mappers = [_FetchMapper.for_fetch(fetch) for fetch in fetches]
File "/home/BH/fyn123456/.conda/envs/py36/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 370, in <listcomp>
self._mappers = [_FetchMapper.for_fetch(fetch) for fetch in fetches]
File "/home/BH/fyn123456/.conda/envs/py36/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 258, in for_fetch
type(fetch)))
TypeError: Fetch argument None has invalid type <class 'NoneType'>
我的变量是整数类型(因为你用整数初始化它们)并且在数学上没有在整数类型上定义渐变,所以我无法评估它们。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.