12

tensorflow学习,python-tensorFlow安装(cpu),解决 name 'N_TOKENS' is not defined...

 3 years ago
source link: https://blog.popkx.com/tensorflow-study-install-of-cpu-type-python-tensorflow/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

tensorflow学习,python-tensorFlow安装(cpu),解决 name 'N_TOKENS' is not defined 和 cannot import name pywrap_tensorflow错误

发表于 2018-06-14 22:06:01   |   已被 访问: 1,255 次   |   分类于:   Python   |   1 条评论

TensorFlow是谷歌基于DistBelief进行研发的第二代人工智能学习系统,其命名来源于本身的运行原理。Tensor(张量)意味着N维数组,Flow(流)意味着基于数据流图的计算,TensorFlow为张量从流图的一端流动到另一端计算过程。TensorFlow是将复杂的数据结构传输至人工智能神经网中进行分析和处理过程的系统。
TensorFlow可被用于语音识别或图像识别等多项机器学习和深度学习领域,对2011年开发的深度学习基础架构DistBelief进行了各方面的改进,它可在小到一部智能手机、大到数千台数据中心服务器的各种设备上运行。TensorFlow将完全开源,任何人都可以用。

pip install tensorflow


想学习 python-tensorflow,我们首先需要安装之,python 的标准库都可以通过 pip install 命令安装,所以第一反应就是执行:

pip install tensorflow

接下来就是一系列的安装过程:

报错:NameError: name 'N_TOKENS' is not defined


似乎一切正常。安装结束后,测试能否正常 import

$ python
Python 2.7.12 (default, Dec  4 2017, 14:50:18) 
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import tensorflow as tf
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/lcc/.local/lib/python2.7/site-packages/tensorflow/__init__.py", line 24, in <module>
    from tensorflow.python import pywrap_tensorflow  # pylint: disable=unused-import
  File "/home/lcc/.local/lib/python2.7/site-packages/tensorflow/python/__init__.py", line 52, in <module>
    from tensorflow.core.framework.graph_pb2 import *
  File "/home/lcc/.local/lib/python2.7/site-packages/tensorflow/core/framework/graph_pb2.py", line 6, in <module>
    from google.protobuf import descriptor as _descriptor
  File "/home/lcc/.local/lib/python2.7/site-packages/google/protobuf/__init__.py", line 37, in <module>
    __import__('pkg_resources').declare_namespace(__name__)
  File "/home/lcc/.local/lib/python2.7/site-packages/pkg_resources/__init__.py", line 41, in <module>
    import inspect
  File "/usr/lib/python2.7/inspect.py", line 39, in <module>
    import tokenize
  File "/usr/lib/python2.7/tokenize.py", line 39, in <module>
    COMMENT = N_TOKENS
NameError: name 'N_TOKENS' is not defined

报错:ImportError: cannot import name pywrap_tensorflow

我们直接在 python 中 import tensorflow,发现依然报错,不过错误信息有所不同:

>>> import tensorflow
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/lcc/.local/lib/python2.7/site-packages/tensorflow/__init__.py", line 24, in <module>
    from tensorflow.python import pywrap_tensorflow  # pylint: disable=unused-import
  File "/home/lcc/.local/lib/python2.7/site-packages/tensorflow/python/__init__.py", line 49, in <module>
    from tensorflow.python import pywrap_tensorflow
ImportError: cannot import name pywrap_tensorflow

解决问题,成功安装 python-tensorflow

果然没有那么顺利,报错了,提示

NameError: name 'N_TOKENS' is not defined
ImportError: cannot import name pywrap_tensorflow

咋一看,还以为 pywrap_tensorflow 模块没有安装,于是执行:

pip install pywrap_tensorflow

结果提示找不到该模块,如下图:

谷歌之,得到以下答案:

意思是,我们现在运行脚本的目录和 TensorFlow 重叠了。
python误以为tensorflow目录中的tensorflow就是要导入的模块,于是切换出该目录,再测试导入,发现成功了:

$ cd ..
$ python
Python 2.7.12 (default, Dec  4 2017, 14:50:18) 
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import tensorflow
>>> import tensorflow as tf
>>> 
至此,我们解决了 name 'N_TOKENS' is not definedcannot import name pywrap_tensorflow 的错误,完成了 python-tensorflow 的安装

阅读更多:   Python


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK