安装和 运行 Tensorflow 时出现 ValueError

ValueError while installing and running Tensorflow

为了了解深度学习和神经网络,我尝试在我的笔记本电脑(Dell Inspiron 5559、4GB RAM,Windows 10)上安装 Tensorflow,使用 conda 4.9.1(最初包含 Python 3.8.3).为此,我使用了以下命令:

conda create -n tf tensorflow

这个 运行 成功安装了 Python 3.7.9tensorflow 2.1.0 和许多其他模块。现在我想测试它是否有效,所以在 Python 终端中,导入了一些模块。 mathnumpy 导入成功。但是当我导入 tensorflow 时,该过程失败并显示以下内容:

>>> import tensorflow      

Traceback (most recent call last):                                                                                                                                            
File "<stdin>", line 1, in <module>                                                                                                             
File "D:\Program Files\Anaconda3\envs\tf\lib\site-packages\tensorflow\__init__.py", line 101, in 
<module>                 from tensorflow_core import *                                                                                         
File "D:\Program Files\Anaconda3\envs\tf\lib\site-packages\tensorflow_core\__init__.py", line 40, in    
<module>             from tensorflow.python.tools import module_util as _module_util                                                       
File "<frozen importlib._bootstrap>", line 983, in _find_and_load                                                       
File "<frozen importlib._bootstrap>", line 959, in _find_and_load_unlocked                                              
File "D:\Program Files\Anaconda3\envs\tf\lib\site-packages\tensorflow\__init__.py", line 50, in 
__getattr__               module = self._load()                                                                                                 
File "D:\Program Files\Anaconda3\envs\tf\lib\site-packages\tensorflow\__init__.py", line 44, in _load                     
module = _importlib.import_module(self.__name__)                                                                      
File "D:\Program Files\Anaconda3\envs\tf\lib\importlib\__init__.py", line 127, in import_module                           
return _bootstrap._gcd_import(name[level:], package, level)                                                           
File "D:\Program Files\Anaconda3\envs\tf\lib\site-packages\tensorflow_core\python\__init__.py", line 
49, in <module>      from tensorflow.python import pywrap_tensorflow                                                                       
File "D:\Program Files\Anaconda3\envs\tf\lib\site- 
packages\tensorflow_core\python\pywrap_tensorflow.py", line 58, in <module>                                                                                                                     
from tensorflow.python.pywrap_tensorflow_internal import *                                                            
File "D:\Program Files\Anaconda3\envs\tf\lib\site- 
packages\tensorflow_core\python\pywrap_tensorflow_internal.py", line 28, in <module>                                                                                                            
_pywrap_tensorflow_internal = swig_import_helper()                                                                    
File "D:\Program Files\Anaconda3\envs\tf\lib\site- 
packages\tensorflow_core\python\pywrap_tensorflow_internal.py", line 15, in swig_import_helper                                                                                                  
import imp                                                                                                          
ValueError: source code string cannot contain null bytes

即使我 运行 导入 Tensorflow 的程序也会出现此错误。

我检查了大多数其他教程,似乎没有人遇到过这个错误。我也尝试使用较低版本的 Python 和 Tensorflow,但出现同样的错误。

任何人都可以找到正在发生的事情吗?我该怎么办?

我认为你的问题是你用 conda 安装了 tensorflow,conda 构建有一些问题,尝试 运行在你的 conda env

中调用 pip

conda remove tensorflow

然后 运行

pip install --upgrade pip
pip install tensorflow

我发现有时很难安装和导入非 3.python 版本的 tensorflow。6.X,我建议使用 python 3.6.X,就简单多了

一个可能的解决方案是将 TensorFlow 降级到 2.00。首先,使用 conda remove tensorflow

从 Anaconda 中删除 TensorFlow

然后降级。

pip install --upgrade pip
pip install tensorflow==2.0