Python tensorflow TypeError:获取参数None的类型无效<;类别';非类型'&燃气轮机;

Python tensorflow TypeError:获取参数None的类型无效<;类别';非类型'&燃气轮机;,python,tensorflow,Python,Tensorflow,我在做cs231n作业2时遇到了这个问题 我使用的是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.p

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

我使用的是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'>
---------------------------------------------------------------------------
TypeError回溯(最近一次调用上次)
在()
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打印(ans.shape)
c:\users\kasper\appdata\local\programs\python36\lib\site-packages\tensorflow\python\client\session.py处于运行状态(self、fetches、feed\u dict、options、run\u元数据)
893尝试:
894结果=self.\u运行(无、取数、输入、选项、,
-->895运行(元数据)
896如果运行\u元数据:
897 proto_data=tf_session.tf_GetBuffer(run_metadata_ptr)
c:\users\kasper\appdata\local\programs\python36\lib\site packages\tensorflow\python\client\session.py in\u run(self、handle、fetches、feed\u dict、options、run\u元数据)
1111#创建一个获取处理程序来处理获取的结构。
1112 fetch\u handler=\u FetchHandler(
->1113 self.\u图形、获取、馈送\u dict\u张量、馈送\u句柄=馈送\u句柄)
1114
1115#运行请求并获取响应。
c:\users\kasper\appdata\local\programs\python36\lib\site packages\tensorflow\python\client\session.py in\uuuuuuu init\uuuuu(self、graph、fetches、feed、feed\uhandles)
419和graph.as_default():
-->420 self.\u fetch\u mapper=\u FetchMapper.for\u fetch(fetches)
421 self._fetches=[]
422自我目标=[]
c:\users\kasper\appdata\local\programs\python36\lib\site packages\tensorflow\python\client\session.py in for_fetch(fetch)
235如果fetch为None:
236 raise TypeError('获取参数%r的类型无效%r'%
-->237(提取,键入(提取)))
238 elif isinstance(获取,(列表,元组)):
239注意(touts):这也是namedtuples的代码路径。
TypeError:获取参数None的类型无效
我以前在这个网站上看到过类似的问题,但这些似乎并不能解决我的问题


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

问题是
sess.run()
y\u out
参数是
None
,而它必须是
tf.Tensor
(或类似于Tensor的对象,如
tf.Variable
)或
tf.Operation

在您的示例中,
y\u out
由以下代码定义:

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

y_out = complex_model(X,y,is_training)

complex\u model()
不返回值,因此
y\u out=complex\u model(…)
y\u out
设置为
None
。我不确定此函数是否代表您的真实代码,但您的真实
complex_model()
函数也可能缺少
return
语句。

我相信mrry是正确的

如果再次查看笔记本,您将注意到描述单元格,如下所示:

培训特定的模型

在本节中,我们将为您指定一个要构建的模型。 这里的目标不是获得好的表现(这将是下一个),而是 相反,要理解TensorFlow就很舒服了 文档和配置您自己的模型

使用上述代码作为指导,并使用以下代码 在TensorFlow文档中,指定具有以下内容的模型 架构:

7x7 Convolutional Layer with 32 filters and stride of 1
ReLU Activation Layer
Spatial Batch Normalization Layer (trainable parameters, with scale and centering)
2x2 Max Pooling layer with a stride of 2
Affine layer with 1024 output units
ReLU Activation Layer
Affine layer from 1024 input units to 10 outputs
它要求您在函数中定义一个模型

# define model
def complex_model(X,y,is_training):
    pass
就像他们在伦敦做的那样

def simple_model(X,y):
    # define our weights (e.g. init_two_layer_convnet)

    # setup variables
    Wconv1 = tf.get_variable("Wconv1", shape=[7, 7, 3, 32])
    bconv1 = tf.get_variable("bconv1", shape=[32])
    W1 = tf.get_variable("W1", shape=[5408, 10])
    b1 = tf.get_variable("b1", shape=[10])

    # define our graph (e.g. two_layer_convnet)
    a1 = tf.nn.conv2d(X, Wconv1, strides=[1,2,2,1], padding='VALID') + bconv1
    h1 = tf.nn.relu(a1)
    h1_flat = tf.reshape(h1,[-1,5408])
    y_out = tf.matmul(h1_flat,W1) + b1
    return y_out
希望这有帮助