tensorflow TypeError: Fetch argument None has invalid type <class 'NoneType'> [英] tensorflow TypeError: Fetch argument None has invalid type <class 'NoneType'>

查看:130
本文介绍了tensorflow TypeError: Fetch argument None has invalid type <class 'NoneType'>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在做cs231n作业2时遇到了这个问题.

I was doing cs231n assignment 2 and encountered this problem.

我使用的是 tensorflow-gpu 1.5.0

I'm using tensorflow-gpu 1.5.0

代码如下

# define our input (e.g. the data that changes every batch)
# The first dim is None, and gets sets automatically based on batch size fed in
X = tf.placeholder(tf.float32, [None, 32, 32, 3])
y = tf.placeholder(tf.int64, [None])
is_training = tf.placeholder(tf.bool)

# define model
def complex_model(X,y,is_training):
    pass

y_out = complex_model(X,y,is_training)

# Now we're going to feed a random batch into the model 
# and make sure the output is the right size
x = np.random.randn(64, 32, 32,3)
with tf.Session() as sess:
    with tf.device("/cpu:0"): #"/cpu:0" or "/gpu:0"
    tf.global_variables_initializer().run()

    ans = sess.run(y_out,feed_dict={X:x,is_training:True})
    %timeit sess.run(y_out,feed_dict={X:x,is_training:True})
    print(ans.shape)
    print(np.array_equal(ans.shape, np.array([64, 10])))

完整的回溯

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-6-97f0b6c5a72e> in <module>()
      6         tf.global_variables_initializer().run()
      7 
----> 8         ans = sess.run(y_out,feed_dict={X:x,is_training:True})
      9         get_ipython().run_line_magic('timeit',     'sess.run(y_out,feed_dict={X:x,is_training:True})')
     10         print(ans.shape)

c:\users\kasper\appdata\local\programs\python\python36\lib\site-    packages\tensorflow\python\client\session.py in run(self, fetches, feed_dict, options, run_metadata)
    893     try:
    894       result = self._run(None, fetches, feed_dict, options_ptr,
--> 895                          run_metadata_ptr)
    896       if run_metadata:
    897         proto_data = tf_session.TF_GetBuffer(run_metadata_ptr)

c:\users\kasper\appdata\local\programs\python\python36\lib\site-packages\tensorflow\python\client\session.py in _run(self, handle, fetches, feed_dict, options, run_metadata)
   1111     # Create a fetch handler to take care of the structure of fetches.
   1112     fetch_handler = _FetchHandler(
-> 1113         self._graph, fetches, feed_dict_tensor,     feed_handles=feed_handles)
   1114 
   1115     # Run request and get response.

c:\users\kasper\appdata\local\programs\python\python36\lib\site-packages\tensorflow\python\client\session.py in __init__(self, graph, fetches, feeds, feed_handles)
    419     with graph.as_default():
--> 420       self._fetch_mapper = _FetchMapper.for_fetch(fetches)
    421     self._fetches = []
    422     self._targets = []

c:\users\kasper\appdata\local\programs\python\python36\lib\site-packages\tensorflow\python\client\session.py in for_fetch(fetch)
    235     if fetch is None:
    236       raise TypeError('Fetch argument %r has invalid type %r' %
--> 237                       (fetch, type(fetch)))
    238     elif isinstance(fetch, (list, tuple)):
    239       # NOTE(touts): This is also the code path for namedtuples.

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

我之前看到这个网站上也有人问过类似的问题,但这些问题似乎都没有解决我的问题.

I saw that similar questions have been asked on this site before,but those don't seem to solve mine.

任何帮助将不胜感激,谢谢!

Any help would be appreciated,thanks!

推荐答案

问题在于 sess.run()y_out 参数是 None,而它必须是 tf.Tensor(或类似张量的对象,例如 tf.Variable)或 tf.Operation.

The problem is that the y_out argument to sess.run() is None, whereas it must be a tf.Tensor (or tensor-like object, such as a tf.Variable) or a tf.Operation.

在您的示例中,y_out 由以下代码定义:

In your example, y_out is defined by the following code:

# define model
def complex_model(X,y,is_training):
    pass

y_out = complex_model(X,y,is_training)

complex_model() 不返回值,所以 y_out = complex_model(...) 会将 y_out 设置为 .我不确定此函数是否代表您的真实代码,但您的真实 complex_model() 函数也可能缺少 return 语句.

complex_model() doesn't return a value, so y_out = complex_model(...) will set y_out to None. I'm not sure if this function is representative of your real code, but it's possible that your real complex_model() function is also missing a return statement.

这篇关于tensorflow TypeError: Fetch argument None has invalid type &lt;class 'NoneType'&gt;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆