学习新技术有两座大山,一座是安装配置环境,另一座是调试bug。
对于想学习人工智能开发的人来说,安装配置tensorflow是必不可少的一步,这个过程对于初次接触的人来说,到处都是火坑。下面大家跟我一起,看看都会踩到哪些雷!
一、安装tensorflow
如果安装tensorflow CPU版本,可以用这个命令:
pip install tensorflow
如果安装tensorflow GPU版本,可以用这个命令:
pip install tensorflow-gpu
但这样的话,因为访问的是国外的网站,速度慢的会让你怀疑人生!
所以我强烈建议大家用国内清华源的镜像
如果安装CPU版:
pip install -i https://pypi.tuna./simple tensorflow
如果安装GPU版:
pip install -i https://pypi.tuna./simple tensorflow-gpu
但有时使用清华源的地址,网络总是莫名其妙的中断,碰到这种情况可以换豆瓣源的镜像地址
/simple
试试,一般就OK了。
二、测试tensorflow安装是否成功
打印一下版本号,看一下安装是否成功
import tensorflow as tfprint(tf.__version__)
如果正常显示tensorflow的版本号,则一切OK;但如果出现下面的错误提示:
Failed to load the native TensorFlow runtime.
Traceback (most recent call last):File "<ipython-input-6-64156d691fe5>", line 1, in <module>import tensorflow as tfFile "C:UsersHonorVanAppDataRoamingPythonPython36site-packagestensorflow__init__.py", line 24, in <module>from tensorflow.python import pywrap_tensorflow # pylint: disable=unused-importFile "C:UsersHonorVanAppDataRoamingPythonPython36site-packagestensorflowpython__init__.py", line 49, in <module>from tensorflow.python import pywrap_tensorflowFile "C:UsersHonorVanAppDataRoamingPythonPython36site-packagestensorflowpythonpywrap_tensorflow.py", line 74, in <module>raise ImportError(msg)ImportError: Traceback (most recent call last):File "C:UsersHonorVanAppDataRoamingPythonPython36site-packagestensorflowpythonpywrap_tensorflow.py", line 58, in <module>from tensorflow.python.pywrap_tensorflow_internal import *File "C:UsersHonorVanAppDataRoamingPythonPython36site-packagestensorflowpythonpywrap_tensorflow_internal.py", line 28, in <module>_pywrap_tensorflow_internal = swig_import_helper()File "C:UsersHonorVanAppDataRoamingPythonPython36site-packagestensorflowpythonpywrap_tensorflow_internal.py", line 24, in swig_import_helper_mod = imp.load_module('_pywrap_tensorflow_internal', fp, pathname, description)File "D:ProgramDataAnaconda3libimp.py", line 243, in load_modulereturn load_dynamic(name, filename, file)File "D:ProgramDataAnaconda3libimp.py", line 343, in load_dynamicreturn _load(spec)ImportError: DLL load failed: 找不到指定的模块。Failed to load the native TensorFlow runtime.See /install/errorsfor some common reasons and solutions. Include the entire stack traceabove this error message when asking for help.
有人说是因为python版本和tensorflow版本不匹配,要么降低python版本,要么降低tensorflow版本。
如果不是python版本和tensorflow版本不匹配,比如我的当前环境为python3.6.5,安装tensorflow的版本为2.2,win10 64位,pip 20.0,版本完全匹配呀~ 那是为什么呢?
如果你前面配置的是CPU版,可能是缺少类库MSVCP140.DLL。
那么在这个地址/en-us/download/details.aspx?id=53587下载安装即可。
这样就能搞定CPU版的tensorflow。
三、搞定tensorflow GPU版的问题
如果你安装的是GPU版,那么接着安装cuda和cudnn,正常next就可以了,只需要注意环境变量path配一下,把cudnn的文件复制到cuda的目录里就OK了。
但是tensorflow对cuda和cudnn的版本也有要求。
tensorflow2.2,通过官网了解只能对应装10.1版本的cuda,10.0的还不行。
然后再找10.1cuda对应的cudnn7.6.5。这个显卡加速库就算是安装成功了。
但如果报驱动和运行版本不对应的错误:
CUDA driver version is insufficient for CUDA runtime versionailed.
可能需要卸载工具包,再安装对应的版本。
pip uninstall cudnn ; pip uninstall cudatoolkit
但在卸载前要先用pip list 命令看看有没有这些东东。如果没有,你可以尝试更新显卡驱动,到英伟达官网/Download/index.aspx?lang=cn选择对应的显卡型号更新即可。
比如我的显卡是老掉渣的GTX1060,最后显示一切OK。
I tensorflow/stream_executor/platform/default/:44] Successfully opened dynamic library cudart64_101.dllI tensorflow/stream_executor/platform/default/:44] Successfully opened dynamic library nvcuda.dllI tensorflow/core/common_runtime/gpu/:1561] Found device 0 with properties: pciBusID: 0000:01:00.0 name: GeForce GTX 1060 computeCapability: 6.1coreClock: 1.6705GHz coreCount: 10 deviceMemorySize: 6.00GiB deviceMemoryBandwidth: 178.99GiB/sI tensorflow/stream_executor/platform/default/:44] Successfully opened dynamic library cudart64_101.dllI tensorflow/stream_executor/platform/default/:44] Successfully opened dynamic library cublas64_10.dllI tensorflow/stream_executor/platform/default/:44] Successfully opened dynamic library cufft64_10.dllI tensorflow/stream_executor/platform/default/:44] Successfully opened dynamic library curand64_10.dllI tensorflow/stream_executor/platform/default/:44] Successfully opened dynamic library cusolver64_10.dllI tensorflow/stream_executor/platform/default/:44] Successfully opened dynamic library cusparse64_10.dllI tensorflow/stream_executor/platform/default/:44] Successfully opened dynamic library cudnn64_7.dllI tensorflow/core/common_runtime/gpu/:1703] Adding visible gpu devices: 0I tensorflow/core/platform/:143] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2I tensorflow/compiler/xla/service/:168] XLA service 0x22345614f70 initialized for platform Host (this does not guarantee that XLA will be used). Devices:I tensorflow/compiler/xla/service/:176] StreamExecutor device (0): Host, Default VersionI tensorflow/core/common_runtime/gpu/:1561] Found device 0 with properties: pciBusID: 0000:01:00.0 name: GeForce GTX 1060 computeCapability: 6.1coreClock: 1.6705GHz coreCount: 10 deviceMemorySize: 6.00GiB deviceMemoryBandwidth: 178.99GiB/sI tensorflow/stream_executor/platform/default/:44] Successfully opened dynamic library cudart64_101.dllI tensorflow/stream_executor/platform/default/:44] Successfully opened dynamic library cublas64_10.dllI tensorflow/stream_executor/platform/default/:44] Successfully opened dynamic library cufft64_10.dllI tensorflow/stream_executor/platform/default/:44] Successfully opened dynamic library curand64_10.dllI tensorflow/stream_executor/platform/default/:44] Successfully opened dynamic library cusolver64_10.dllI tensorflow/stream_executor/platform/default/:44] Successfully opened dynamic library cusparse64_10.dllI tensorflow/stream_executor/platform/default/:44] Successfully opened dynamic library cudnn64_7.dllI tensorflow/core/common_runtime/gpu/:1703] Adding visible gpu devices: 0I tensorflow/core/common_runtime/gpu/:1102] Device interconnect StreamExecutor with strength 1 edge matrix:I tensorflow/core/common_runtime/gpu/:1108]0 I tensorflow/core/common_runtime/gpu/:1121] 0: N I tensorflow/core/common_runtime/gpu/:1247] Created TensorFlow device (/job:localhost/replica:0/task:0/device:GPU:0 with 4700 MB memory) -> physical GPU (device: 0, name: GeForce GTX 1060, pci bus id: 0000:01:00.0, compute capability: 6.1)I tensorflow/compiler/xla/service/:168] XLA service 0x223673b0e90 initialized for platform CUDA (this does not guarantee that XLA will be used). Devices:I tensorflow/compiler/xla/service/:176] StreamExecutor device (0): GeForce GTX 1060, Compute Capability 6.1
总的来说CPU版比较简单,GPU版坑比较多。