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

标签 python tensorflow

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)

关于python - 简单的 Tensorflow 示例在 Jupyter Notebook 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41988267/

相关文章:

python - 如何从字典列表中过滤掉具有重复键和不同值的元素?

python - 简化if语句python

python - 图中轴矢量场的缩放颤动图

python - 加载 tfrecord 文件时出现 InvalidArgumentError

运行自动编码器时,Tensorflow set_seed错误

python - Keras 不使用 GPU - 如何排除故障?

python - 如何使用 Python 检查磁盘上还有多少可用空间?

Python 脚本 - 帐户生成器

tensorflow - "rewind" tensorflow 训练步骤

android - 如何从 Tensorflow Java API 访问 tflite 模型?