繁体   English   中英

使用tf.gradients时的Tensorflow错误:TypeError:Fetch参数None具有无效类型<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'>

我的变量是整数类型(因为你用整数初始化它们)并且在数学上没有在整数类型上定义渐变,所以我无法评估它们。

Tensorflow TypeError: Fetch argument None has invalid type <type 'n< div><div id="text_translate"><pre> a=tf.Variable(0, name='input') b=tf.constant(1) mid_val =tf.add(a,b) update_value =tf.compat.v1.assign(a,mid_val) tg=initialize_all_variables() with tf.compat.v1.Session() as sess: sess.run(tg) print(sess.run(a)) for i in range(3): sess.run(update_value) print(sess.run(a))</pre><p> 它在sess.run(tg)中给我错误并引发类型错误,在运行时它给出以下错误:</p><pre> &lt;ipython-input-20-fd2283cdd3bd&gt; in &lt;module&gt;() 10 11 with tf.compat.v1.Session() as sess: ---&gt; 12 sess.run(tg) 13 print(sess.run(a)) 14 3 frames /usr/local/lib/python3.7/dist-packages/tensorflow/python/client/session.py in for_fetch(fetch) 263 """ 264 if fetch is None: --&gt; 265 raise TypeError(f'Argument `fetch` = {fetch} has invalid type ' 266 f'"{type(fetch).__name__}". Cannot be None') 267 elif isinstance(fetch, (list, tuple)):</pre><p> 我应该怎么办?</p></div></type>

[英]Tensorflow TypeError: Fetch argument None has invalid type <type 'N

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Tensorflow TypeError:Fetch参数None具有无效类型 <type 'NoneType'> ? Tensorflow:TypeError:获取参数None具有无效的类型<class 'NoneType'> tensorflow TypeError:Fetch参数None具有无效类型<class 'NoneType'> TypeError:获取参数None具有无效的类型<type 'NoneType'> 多个网络产生Tensorflow TypeError:获取参数None具有无效的类型<class 'NoneType'> TensorFlow session 有两个神经网络 - TypeError: Fetch argument None has invalid type<class 'nonetype'></class> Tensorflow TypeError:获取参数None具有无效的类型 <class 'NoneType'> 在寻找梯度的同时 Tensorflow TypeError: Fetch argument None has invalid type <type 'n< div><div id="text_translate"><pre> a=tf.Variable(0, name='input') b=tf.constant(1) mid_val =tf.add(a,b) update_value =tf.compat.v1.assign(a,mid_val) tg=initialize_all_variables() with tf.compat.v1.Session() as sess: sess.run(tg) print(sess.run(a)) for i in range(3): sess.run(update_value) print(sess.run(a))</pre><p> 它在sess.run(tg)中给我错误并引发类型错误,在运行时它给出以下错误:</p><pre> &lt;ipython-input-20-fd2283cdd3bd&gt; in &lt;module&gt;() 10 11 with tf.compat.v1.Session() as sess: ---&gt; 12 sess.run(tg) 13 print(sess.run(a)) 14 3 frames /usr/local/lib/python3.7/dist-packages/tensorflow/python/client/session.py in for_fetch(fetch) 263 """ 264 if fetch is None: --&gt; 265 raise TypeError(f'Argument `fetch` = {fetch} has invalid type ' 266 f'"{type(fetch).__name__}". Cannot be None') 267 elif isinstance(fetch, (list, tuple)):</pre><p> 我应该怎么办?</p></div></type> TypeError:获取参数 None 的类型无效<class 'NoneType'> , 张量传递问题 提取参数None具有无效的类型<class 'NoneType'>
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM