【AI】在win10上安装TensorFlow2,安装成功,但是import tensorflow时报错:pywrap_tensorflow.py“, line 58
目錄
- 一、問題描述
- 二、原因分析
- 三、處理過程
- 四、解決方法
- 五、安裝2.1和2.0命令的不同點
- 1、TensorFlow2.0
- 2、TensorFlow2.1
- 六、使用TenforFlow2.0-GPU時,報錯:cudart64_100.dll not found
- 1、錯誤信息如下
- 2、原因分析
- 3、解決方法
- 七、測試TensorFlow是否支持GPU
- 1、測試對cuda的支持
- 2、測試對GPU的支持
一、問題描述
在win10上安裝TensorFlow2成功,但是在使用時(import tensorflow )報錯。這個問題困擾了我兩天兩夜,熬了兩宿終于解決了,趕緊記錄下來。
錯誤信息如下:
import tensorflow as tf
Traceback (most recent call last):File "C:\Users\gwlbl\Anaconda3\envs\tf2.1cpu\lib\site-packages\tensorflow_core\python\pywrap_tensorflow.py", line 58, in <module>from tensorflow.python.pywrap_tensorflow_internal import *File "C:\Users\gwlbl\Anaconda3\envs\tf2.1cpu\lib\site-packages\tensorflow_core\python\pywrap_tensorflow_internal.py", line 28, in <module>_pywrap_tensorflow_internal = swig_import_helper()File "C:\Users\gwlbl\Anaconda3\envs\tf2.1cpu\lib\site-packages\tensorflow_core\python\pywrap_tensorflow_internal.py", line 24, in swig_import_helper_mod = imp.load_module('_pywrap_tensorflow_internal', fp, pathname, description)File "C:\Users\gwlbl\Anaconda3\envs\tf2.1cpu\lib\imp.py", line 243, in load_modulereturn load_dynamic(name, filename, file)File "C:\Users\gwlbl\Anaconda3\envs\tf2.1cpu\lib\imp.py", line 343, in load_dynamicreturn _load(spec)
ImportError: DLL load failed: 找不到指定的模塊。During handling of the above exception, another exception occurred:Traceback (most recent call last):File "<stdin>", line 1, in <module>File "C:\Users\gwlbl\Anaconda3\envs\tf2.1cpu\lib\site-packages\tensorflow\__init__.py", line 101, in <module>from tensorflow_core import *File "C:\Users\gwlbl\Anaconda3\envs\tf2.1cpu\lib\site-packages\tensorflow_core\__init__.py", line 40, in <module>from tensorflow.python.tools import module_util as _module_utilFile "C:\Users\gwlbl\Anaconda3\envs\tf2.1cpu\lib\site-packages\tensorflow\__init__.py", line 50, in __getattr__module = self._load()File "C:\Users\gwlbl\Anaconda3\envs\tf2.1cpu\lib\site-packages\tensorflow\__init__.py", line 44, in _loadmodule = _importlib.import_module(self.__name__)File "C:\Users\gwlbl\Anaconda3\envs\tf2.1cpu\lib\importlib\__init__.py", line 126, in import_modulereturn _bootstrap._gcd_import(name[level:], package, level)File "C:\Users\gwlbl\Anaconda3\envs\tf2.1cpu\lib\site-packages\tensorflow_core\python\__init__.py", line 49, in <module>from tensorflow.python import pywrap_tensorflowFile "C:\Users\gwlbl\Anaconda3\envs\tf2.1cpu\lib\site-packages\tensorflow_core\python\pywrap_tensorflow.py", line 74, in <module>raise ImportError(msg)
ImportError: Traceback (most recent call last):File "C:\Users\gwlbl\Anaconda3\envs\tf2.1cpu\lib\site-packages\tensorflow_core\python\pywrap_tensorflow.py", line 58, in <module>from tensorflow.python.pywrap_tensorflow_internal import *File "C:\Users\gwlbl\Anaconda3\envs\tf2.1cpu\lib\site-packages\tensorflow_core\python\pywrap_tensorflow_internal.py", line 28, in <module>_pywrap_tensorflow_internal = swig_import_helper()File "C:\Users\gwlbl\Anaconda3\envs\tf2.1cpu\lib\site-packages\tensorflow_core\python\pywrap_tensorflow_internal.py", line 24, in swig_import_helper_mod = imp.load_module('_pywrap_tensorflow_internal', fp, pathname, description)File "C:\Users\gwlbl\Anaconda3\envs\tf2.1cpu\lib\imp.py", line 243, in load_modulereturn load_dynamic(name, filename, file)File "C:\Users\gwlbl\Anaconda3\envs\tf2.1cpu\lib\imp.py", line 343, in load_dynamicreturn _load(spec)
ImportError: DLL load failed: 找不到指定的模塊。Failed to load the native TensorFlow runtime.See https://www.tensorflow.org/install/errorsfor some common reasons and solutions. Include the entire stack trace
above this error message when asking for help.
二、原因分析
導致錯誤的本質原因沒有找到,在嘗試安裝v2.0版本后,才沒有出現這個錯誤。
三、處理過程
截至今天(2020-2-12)TensorFlow的最新穩定版本是v2.1,在win10 + python3.6 + pip2.0環境下默認安裝的就是最新的版本v2.1。安裝過程沒有異常,但是在使用時就會報上述錯誤。導致錯誤的本質原因沒有找到,在嘗試安裝v2.0版本后,才沒有出現這個錯誤。
其它嘗試都會報錯,如:
將CUDA版本改為10.1、10.2;
嘗試安裝v2.1的cpu和gpu版本;
嘗試Python3.7和Python3.6;
嘗試刪除protobuf
四、解決方法
使用pip安裝時指定版本:
pip install tensorflow==2.0.0
或
pip install tensorflow-gpu==2.0.0
五、安裝2.1和2.0命令的不同點
在使用命令pip install tensorflow安裝TensorFlow2.1時,將會同時安裝cpu和gpu;
安裝TensorFlow2.1或2.0的命令略有不用,匯總如下
1、TensorFlow2.0
單獨安裝CPU:pip install tensorflow ==2.0.0
單獨安裝GPU:pip install tensorflow-gpu ==2.0.0
2、TensorFlow2.1
單獨安裝CPU:pip install tensorflow-cpu == 2.1.0
單獨安裝GPU:pip install tensorflow-gpu== 2.1.0
同時安裝CPU和GPU:pip install tensorflow==2.1.0
六、使用TenforFlow2.0-GPU時,報錯:cudart64_100.dll not found
1、錯誤信息如下
import tensorflow as tf
2020-02-12 01:12:14.047720: W tensorflow/stream_executor/platform/default/dso_loader.cc:55] Could not load dynamic library 'cudart64_100.dll'; dlerror: cudart64_100.dll not found
2、原因分析
cudart64_100.dll是CUDA10.0版本中的庫,而我安裝是最新的(截至2020-2-12)CUDA10.2,這個版本中cudart64庫的名字是cudart64_102.dll,因此報錯沒有找到cudart64_100.dll。
3、解決方法
一種解決方法是重新安裝CUDA10.0的版本;
另外一種簡單方法是,直接找到cudart64_102.dll將它修改為cudart64_100.dll(這個方法不太嚴謹,不知道以后會不會報錯)
更改后的打印信息
>>> import tensorflow as tf
2020-02-12 01:15:35.685062: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cudart64_100.dll
七、測試TensorFlow是否支持GPU
1、測試對cuda的支持
>>> tf.test.is_built_with_cuda()
True
2、測試對GPU的支持
>>> tf.test.is_gpu_available(cuda_only=False,min_cuda_compute_capability=None)
2020-02-12 01:17:21.667124: I tensorflow/core/platform/cpu_feature_guard.cc:142] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2
2020-02-12 01:17:21.681120: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library nvcuda.dll
2020-02-12 01:17:22.159708: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1618] Found device 0 with properties:
name: GeForce 940MX major: 5 minor: 0 memoryClockRate(GHz): 1.189
pciBusID: 0000:01:00.0
2020-02-12 01:17:22.208181: I tensorflow/stream_executor/platform/default/dlopen_checker_stub.cc:25] GPU libraries are statically linked, skip dlopen check.
2020-02-12 01:17:22.246881: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1746] Adding visible gpu devices: 0
2020-02-12 01:17:22.579081: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1159] Device interconnect StreamExecutor with strength 1 edge matrix:
2020-02-12 01:17:22.587504: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1165] 0
2020-02-12 01:17:22.593051: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1178] 0: N
2020-02-12 01:17:22.601454: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1304] Created TensorFlow device (/device:GPU:0 with 1464 MB memory) -> physical GPU (device: 0, name: GeForce 940MX, pci bus id: 0000:01:00.0, compute capability: 5.0)
True
總結
以上是生活随笔為你收集整理的【AI】在win10上安装TensorFlow2,安装成功,但是import tensorflow时报错:pywrap_tensorflow.py“, line 58的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【经验】配置Anaconda源
- 下一篇: 【经验】提高github的下载(克隆)速