Skip to main content
 首页 » 编程设计

python之简单的 Tensorflow 示例在 Jupyter Notebook 中不起作用

2024年10月01日4txw1958

import tensorflow as tf 
 
x = tf.constant(1.0) 
w = tf.Variable(0.8) 
y = w * x 
y_ = tf.constant(0.0) 
loss = (y - y_)**2 
optim = tf.train.GradientDescentOptimizer(learning_rate=0.025) 
grads_and_vars = optim.compute_gradients(loss) 
 
with tf.Session() as sess: 
    sess.run(tf.global_variables_initializer()) 
    print(sess.run(grads_and_vars)) 

我在 Jupyter Notebook 的一个单元格中运行这个 Tensorflow 示例。 当我第一次运行代码时,它运行良好。但是,当我重新运行该单元格时,它会出现以下错误,我已经尝试了很多但似乎无法找出错误:

--------------------------------------------------------------------------- 
TypeError                                 Traceback (most recent call last) 
<ipython-input-2-6883a1591d9c> in <module>() 
     13 with tf.Session() as sess: 
     14     sess.run(tf.global_variables_initializer()) 
---> 15     print(sess.run(grads_and_vars)) 
 
/Users/ansh/Installations/anaconda/lib/python3.5/site-packages/tensorflow/python/client/session.py in run(self, fetches, feed_dict, options, run_metadata) 
    764     try: 
    765       result = self._run(None, fetches, feed_dict, options_ptr, 
--> 766                          run_metadata_ptr) 
    767       if run_metadata: 
    768         proto_data = tf_session.TF_GetBuffer(run_metadata_ptr) 
 
/Users/ansh/Installations/anaconda/lib/python3.5/site-packages/tensorflow/python/client/session.py in _run(self, handle, fetches, feed_dict, options, run_metadata) 
    949  
    950     # Create a fetch handler to take care of the structure of fetches. 
--> 951     fetch_handler = _FetchHandler(self._graph, fetches, feed_dict_string) 
    952  
    953     # Run request and get response. 
 
/Users/ansh/Installations/anaconda/lib/python3.5/site-packages/tensorflow/python/client/session.py in __init__(self, graph, fetches, feeds) 
    405     """ 
    406     with graph.as_default(): 
--> 407       self._fetch_mapper = _FetchMapper.for_fetch(fetches) 
    408     self._fetches = [] 
    409     self._targets = [] 
 
/Users/ansh/Installations/anaconda/lib/python3.5/site-packages/tensorflow/python/client/session.py in for_fetch(fetch) 
    228     elif isinstance(fetch, (list, tuple)): 
    229       # NOTE(touts): This is also the code path for namedtuples. 
--> 230       return _ListFetchMapper(fetch) 
    231     elif isinstance(fetch, dict): 
    232       return _DictFetchMapper(fetch) 
 
/Users/ansh/Installations/anaconda/lib/python3.5/site-packages/tensorflow/python/client/session.py in __init__(self, fetches) 
    335     """ 
    336     self._fetch_type = type(fetches) 
--> 337     self._mappers = [_FetchMapper.for_fetch(fetch) for fetch in fetches] 
    338     self._unique_fetches, self._value_indices = _uniquify_fetches(self._mappers) 
    339  
 
/Users/ansh/Installations/anaconda/lib/python3.5/site-packages/tensorflow/python/client/session.py in <listcomp>(.0) 
    335     """ 
    336     self._fetch_type = type(fetches) 
--> 337     self._mappers = [_FetchMapper.for_fetch(fetch) for fetch in fetches] 
    338     self._unique_fetches, self._value_indices = _uniquify_fetches(self._mappers) 
    339  
 
/Users/ansh/Installations/anaconda/lib/python3.5/site-packages/tensorflow/python/client/session.py in for_fetch(fetch) 
    228     elif isinstance(fetch, (list, tuple)): 
    229       # NOTE(touts): This is also the code path for namedtuples. 
--> 230       return _ListFetchMapper(fetch) 
    231     elif isinstance(fetch, dict): 
    232       return _DictFetchMapper(fetch) 
 
/Users/ansh/Installations/anaconda/lib/python3.5/site-packages/tensorflow/python/client/session.py in __init__(self, fetches) 
    335     """ 
    336     self._fetch_type = type(fetches) 
--> 337     self._mappers = [_FetchMapper.for_fetch(fetch) for fetch in fetches] 
    338     self._unique_fetches, self._value_indices = _uniquify_fetches(self._mappers) 
    339  
 
/Users/ansh/Installations/anaconda/lib/python3.5/site-packages/tensorflow/python/client/session.py in <listcomp>(.0) 
    335     """ 
    336     self._fetch_type = type(fetches) 
--> 337     self._mappers = [_FetchMapper.for_fetch(fetch) for fetch in fetches] 
    338     self._unique_fetches, self._value_indices = _uniquify_fetches(self._mappers) 
    339  
 
/Users/ansh/Installations/anaconda/lib/python3.5/site-packages/tensorflow/python/client/session.py in for_fetch(fetch) 
    225     if fetch is None: 
    226       raise TypeError('Fetch argument %r has invalid type %r' % 
--> 227                       (fetch, type(fetch))) 
    228     elif isinstance(fetch, (list, tuple)): 
    229       # NOTE(touts): This is also the code path for namedtuples. 
 
TypeError: Fetch argument None has invalid type <class 'NoneType'> 

请您参考如下方法:

优化的结果是“无”,因此您无法打印它。如果你想在优化步骤后打印损失,你可以这样做

loss,_ = sess.run([loss,grads_and_vars]); 
print(loss)