【发布时间】:2021-08-19 07:16:52
【问题描述】:
在我正在学习的机器学习课程中,我们详细了解了如何使用 TensorFlow 创建模型。通常这是在远程虚拟环境上运行的,但为了我自己的启发,我认为我应该能够在我自己的机器上运行 TensorFlow。我目前正在运行 Python 3.8.6,我们的指令专门针对 TensorFlow 2.1.0 或更高版本。
首先,安装 TensorFlow 的最直接方法是在他们的 homepage 和许多其他网站上提供。当我尝试以这种方式安装 TensorFlow 时(无论我指定什么版本),我会遇到如下错误:
D:\Documents>pip install --upgrade tensorflow==2.0.0
WARNING: Ignoring invalid distribution -ip (d:\program files (x86)\python38-32\lib\site-packages)
WARNING: Ignoring invalid distribution - (d:\program files (x86)\python38-32\lib\site-packages)
ERROR: Could not find a version that satisfies the requirement tensorflow==2.0.0 (from versions: none)
ERROR: No matching distribution found for tensorflow==2.0.0
似乎所有在线建议似乎都表明 tensorflow 不适用于 Python 3.7 或更高版本,因此我应该降级或卸载 Python 以使其工作。除了我真的不知道如何降级 python 之外,我不会为了运行一个包而更改我的 Python 安装。 this answer here 中给出了我能找到的唯一可行的解决方案。
pip install https://storage.googleapis.com/tensorflow/mac/cpu/tensorflow-1.8.0-py3-none-any.whl
但是,一旦我这样做了,我发现运行 Tensorflow 实际上并不能正常工作:
D:\Documents>python
Python 3.8.6 (tags/v3.8.6:db45529, Sep 23 2020, 15:37:30) [MSC v.1927 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import tensorflow
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Users\nsg3\AppData\Roaming\Python\Python38\site-packages\tensorflow\__init__.py", line 22, in <module>
from tensorflow.python import pywrap_tensorflow # pylint: disable=unused-import
File "C:\Users\nsg3\AppData\Roaming\Python\Python38\site-packages\tensorflow\python\__init__.py", line 49, in <module>
from tensorflow.python import pywrap_tensorflow
File "C:\Users\nsg3\AppData\Roaming\Python\Python38\site-packages\tensorflow\python\pywrap_tensorflow.py", line 58, in <module>
from tensorflow.python.pywrap_tensorflow_internal import *
File "C:\Users\nsg3\AppData\Roaming\Python\Python38\site-packages\tensorflow\python\pywrap_tensorflow_internal.py", line 114
def TFE_ContextOptionsSetAsync(arg1, async):
^
SyntaxError: invalid syntax
这是因为async 是保留字。所以我找到了有问题的文件,编辑了实际的 Tensorflow 包源代码,然后再试一次:
D:\Documents>python
Python 3.8.6 (tags/v3.8.6:db45529, Sep 23 2020, 15:37:30) [MSC v.1927 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import tensorflow
Traceback (most recent call last):
File "C:\Users\nsg3\AppData\Roaming\Python\Python38\site-packages\tensorflow\python\pywrap_tensorflow_internal.py", line 18, in swig_import_helper
fp, pathname, description = imp.find_module('_pywrap_tensorflow_internal', [dirname(__file__)])
File "D:\Program Files (x86)\Python38-32\lib\imp.py", line 296, in find_module
raise ImportError(_ERR_MSG.format(name), name=name)
ImportError: No module named '_pywrap_tensorflow_internal'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\nsg3\AppData\Roaming\Python\Python38\site-packages\tensorflow\python\pywrap_tensorflow.py", line 58, in <module>
from tensorflow.python.pywrap_tensorflow_internal import *
File "C:\Users\nsg3\AppData\Roaming\Python\Python38\site-packages\tensorflow\python\pywrap_tensorflow_internal.py", line 28, in <module>
_pywrap_tensorflow_internal = swig_import_helper()
File "C:\Users\nsg3\AppData\Roaming\Python\Python38\site-packages\tensorflow\python\pywrap_tensorflow_internal.py", line 20, in swig_import_helper
import _pywrap_tensorflow_internal
ModuleNotFoundError: No module named '_pywrap_tensorflow_internal'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Users\nsg3\AppData\Roaming\Python\Python38\site-packages\tensorflow\__init__.py", line 22, in <module>
from tensorflow.python import pywrap_tensorflow # pylint: disable=unused-import
File "C:\Users\nsg3\AppData\Roaming\Python\Python38\site-packages\tensorflow\python\__init__.py", line 49, in <module>
from tensorflow.python import pywrap_tensorflow
File "C:\Users\nsg3\AppData\Roaming\Python\Python38\site-packages\tensorflow\python\pywrap_tensorflow.py", line 74, in <module>
raise ImportError(msg)
ImportError: Traceback (most recent call last):
File "C:\Users\nsg3\AppData\Roaming\Python\Python38\site-packages\tensorflow\python\pywrap_tensorflow_internal.py", line 18, in swig_import_helper
fp, pathname, description = imp.find_module('_pywrap_tensorflow_internal', [dirname(__file__)])
File "D:\Program Files (x86)\Python38-32\lib\imp.py", line 296, in find_module
raise ImportError(_ERR_MSG.format(name), name=name)
ImportError: No module named '_pywrap_tensorflow_internal'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\nsg3\AppData\Roaming\Python\Python38\site-packages\tensorflow\python\pywrap_tensorflow.py", line 58, in <module>
from tensorflow.python.pywrap_tensorflow_internal import *
File "C:\Users\nsg3\AppData\Roaming\Python\Python38\site-packages\tensorflow\python\pywrap_tensorflow_internal.py", line 28, in <module>
_pywrap_tensorflow_internal = swig_import_helper()
File "C:\Users\nsg3\AppData\Roaming\Python\Python38\site-packages\tensorflow\python\pywrap_tensorflow_internal.py", line 20, in swig_import_helper
import _pywrap_tensorflow_internal
ModuleNotFoundError: No module named '_pywrap_tensorflow_internal'
Failed to load the native TensorFlow runtime.
See https://www.tensorflow.org/install/install_sources#common_installation_problems
for some common reasons and solutions. Include the entire stack trace
above this error message when asking for help.
这就是事情变得热闹的地方。我在互联网上任何地方都能找到的所有建议都说解决这个错误的唯一方法是重新安装 Tensorflow(就好像我一开始就可以安装它一样)。是的,跟踪日志中给出的 URL leads to a 404 error page。
在被告知如何使用 Tensorflow 进行机器学习之后,我实际上无法运行它,这似乎有点令人失望?是否有实际的解决方法,或者这门课是否给了我误导或过时的信息?
谢谢, 内森
编辑: 如前所述,我使用什么版本的 Tensorflow 并不重要。指定 2.2.0 或更高版本会得到相同的结果:
D:\Documents>pip install --upgrade tensorflow==2.2.0
WARNING: Ignoring invalid distribution -ip (d:\program files (x86)\python38-32\lib\site-packages)
WARNING: Ignoring invalid distribution - (d:\program files (x86)\python38-32\lib\site-packages)
ERROR: Could not find a version that satisfies the requirement tensorflow==2.2.0 (from versions: none)
ERROR: No matching distribution found for tensorflow==2.2.0
这也适用于简单地给出tensorflow 而没有任何版本号
【问题讨论】:
-
你为什么使用 32 位 python? TensorFlow 仅适用于 64 位平台。
标签: python python-3.x tensorflow installation pip