Tensorflow 安装踩坑

最近在琢磨琢磨TensorFlow,花了很多时间才搭建成功,在此记下自己碰到的问题

下载Python

注意: 1、机器必须是64位的,TensorFlow不支持32位!TensorFlow不支持32位!TensorFlow不支持32位! 2、Python必须装64位的!

这个Tensorflow在官网对系统要求
Python 3.5-3.7
pip 19.0 或更高版本(需要 manylinux2010 支持)
而Keras 兼容的 Python 版本: Python 2.7-3.6。
所以下载的版本我就选择了python 3.6.0 64位

现在的Python已经更新到了3.8.3版本了,更新太快了,但是新的不一定都支持。

下载完了之后,安装,点击那个Add to Path选项,如果可以直接python就可以忽略下面了。
调环境变量,在菜鸟教程上都有,只用把上面用户的系统的系统变量调对了就可以。同时要在PyCharm调整编译器,在这里也有教程。

更新pip和virtualenv

pip一定要更新到19.0版本以后,现在最新的是20.0
virtualenv没看出来有什么用处,我好像虚拟环境用,但我直接在系统环境里装的

下载Tensorflow

如果直接使用 pip install –upgrade tensorflow由于国内网络问题,会导致下载不下来,根本不可能安装上。
所以可以在国内镜像去下载,我这里选的阿里云的镜像。需要手动下载的,在这里直接Ctrl + F搜索 tensorflow就可以找到下载地方了。

安装Tensorflow

安装命令pip install建议在PowerShell里面执行,Powershell的打开方式是在目标文件夹下按住shift+鼠标右键,选择Window PowerShell,就打开了。
直接在下载到的TensorFlow目录下(就是你刚才把Tensorflow下载的地方)执行pip install tensorflow-2.3.0-cp36-cp36m-win_amd64.whl 期间会安装一些TensorFLow的依赖,同样的,可能安装失败。因为文件一大就下载不下来,所以解决的最好办法就是手动下载,再安装。
如我的numpy安装失败了

1
2
3
4
5
6
7
8
Collecting numpy>=1.11.0 (from tensorflow==1.0.0)
Downloading numpy-1.12.0-cp35-none-win_amd64.whl (7.7MB)
Exception:
Traceback (most recent call last):
File "c:\python35-3\lib\site-packages\pip\_vendor\requests\packages\urllib3\response.py", line 232, in _error_catcher
yield
File "c:\python35-3\lib\site-packages\pip\_vendor\requests\packages\urllib3\response.py", line 314, in read
data = self._fp.read(amt)

这里同样搜索numpy,下载在手动安装,这要注意依赖的版本,直接复制numpy-1.12.0-cp35-none-win_amd64.whl,这个文件在你下载的地方会显示,直接复制查找下载就好,搜索一下就行:
现在都是竖着排列的,很方便查看了。

在PowerShell输入pip install numpy-1.12.0-cp35-none-win_amd64.whl
手动安装成功:

1
2
3
4
pip install numpy-1.12.0-cp35-none-win_amd64.whl
Processing f:\tensorflow\numpy-1.12.0-cp35-none-win_amd64.whl
Installing collected packages: numpy
Successfully installed numpy-1.12.0

其他的包安装方法同理。

测试安装结果

在PyCharm里输入

1
2
3
4
5
import tensorflow as tf
tf.compat.v1.disable_eager_execution()
hello = tf.constant('hello')
sess= tf.compat.v1.Session()
print(sess.run(hello))

如果最后能输出结果b’hello则成功

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
2020-08-25 00:01:00.435756: I tensorflow/stream_executor/platform/default/dso_loader.cc:48] Successfully opened dynamic library cudart64_101.dll
2020-08-25 00:01:03.200442: I tensorflow/stream_executor/platform/default/dso_loader.cc:48] Successfully opened dynamic library nvcuda.dll
2020-08-25 00:01:03.875290: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1716] Found device 0 with properties:
pciBusID: 0000:01:00.0 name: GeForce MX150 computeCapability: 6.1
coreClock: 1.0375GHz coreCount: 3 deviceMemorySize: 2.00GiB deviceMemoryBandwidth: 37.33GiB/s
2020-08-25 00:01:03.875660: I tensorflow/stream_executor/platform/default/dso_loader.cc:48] Successfully opened dynamic library cudart64_101.dll
2020-08-25 00:01:03.877846: W tensorflow/stream_executor/platform/default/dso_loader.cc:59] Could not load dynamic library 'cublas64_10.dll'; dlerror: cublas64_10.dll not found
2020-08-25 00:01:03.993896: I tensorflow/stream_executor/platform/default/dso_loader.cc:48] Successfully opened dynamic library cufft64_10.dll
2020-08-25 00:01:04.000945: I tensorflow/stream_executor/platform/default/dso_loader.cc:48] Successfully opened dynamic library curand64_10.dll
2020-08-25 00:01:04.062328: I tensorflow/stream_executor/platform/default/dso_loader.cc:48] Successfully opened dynamic library cusolver64_10.dll
2020-08-25 00:01:04.063793: W tensorflow/stream_executor/platform/default/dso_loader.cc:59] Could not load dynamic library 'cusparse64_10.dll'; dlerror: cusparse64_10.dll not found
2020-08-25 00:01:04.064736: W tensorflow/stream_executor/platform/default/dso_loader.cc:59] Could not load dynamic library 'cudnn64_7.dll'; dlerror: cudnn64_7.dll not found
2020-08-25 00:01:04.064970: W tensorflow/core/common_runtime/gpu/gpu_device.cc:1753] Cannot dlopen some GPU libraries. Please make sure the missing libraries mentioned above are installed properly if you would like to use GPU. Follow the guide at https://www.tensorflow.org/install/gpu for how to download and setup the required libraries for your platform.
Skipping registering GPU devices...
2020-08-25 00:01:04.066352: I tensorflow/core/platform/cpu_feature_guard.cc:142] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN)to use the following CPU instructions in performance-critical operations: AVX2
To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.
2020-08-25 00:01:04.077843: I tensorflow/compiler/xla/service/service.cc:168] XLA service 0x1f2523ef360 initialized for platform Host (this does not guarantee that XLA will be used). Devices:
2020-08-25 00:01:04.078174: I tensorflow/compiler/xla/service/service.cc:176] StreamExecutor device (0): Host, Default Version
2020-08-25 00:01:04.078547: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1257] Device interconnect StreamExecutor with strength 1 edge matrix:
2020-08-25 00:01:04.078731: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1263]
b'hello'

Process finished with exit code 0

一些问题

如果版本不行就更新tensorflow的版本,好像说1.15的版本支持比较好

tensorflow出现 ImportError: DLL load failed: 找不到指定的程序

1
2
3
4
5
6
7
8
9
10
11
12
Traceback (most recent call last): 
File “D:/pyfile/word4/yy.py”, line 3, in
import tensorflow as tf
File “D:\pyfile\word4\venv\lib\site-packages\tensorflow__init__.py”, line 24, in
from tensorflow.python import pywrap_tensorflow # pylint: disable=unused-import
File “D:\pyfile\word4\venv\lib\site-packages\tensorflow\python__init__.py”, line 52, in
from tensorflow.core.framework.graph_pb2 import *
File “D:\pyfile\word4\venv\lib\site-packages\tensorflow\core\framework\graph_pb2.py”, line 6, in
from google.protobuf import descriptor as _descriptor
File “D:\pyfile\word4\venv\lib\site-packages\google\protobuf\descriptor.py”, line 47, in
from google.protobuf.pyext import _message
ImportError: DLL load failed: 找不到指定的程序。

解决方法
没有注意到protobuf已经更新。 在自动安装了tensorflow时, protobuf安装的是最新版本3.6.1, 出现了不兼容的问题。
更换为 protobuf 3.6.0即可

在刚才的PowerShell里输入
pip install protobuf==3.6.0
问题解决
参考内容(https://blog.csdn.net/lijil168/article/details/82289596)

解决 tensorflow-gpu 2.1 出现错误 “Could not load dynamic library ‘cudart64_101.dll’

1
2
3
>>> import tensorflow as tf
2020-04-22 14:19:56.862944: W tensorflow/stream_executor/platform/default/dso_loader.cc:55] Could not load dynamic library 'cudart64_101.dll'; dlerror: cudart64_101.dll not found
2020-04-22 14:19:56.867965: I tensorflow/stream_executor/cuda/cudart_stub.cc:29] Ignore above cudart dlerror if you do not have a GPU set up on your machine.

原因
cudart64_101.dll”是cuda10.1这个版本中才含有的,而我安装的cuda的不是,如果你是10.0或者10.2或者11.0等都可以用这个解决。
解决方法
将“cudart64_101.dll”添加到“C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.2\bin”中去,问题解决。
cudart64_101.dll的下载地址:cudart64_101.dll
参考内容(https://blog.csdn.net/wjhua_223/article/details/105681521)

出现很多I的警告

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
(tensorflow1) C:\Users\Administrator>python
Python 3.7.7 (default, May 6 2020, 11:45:54) [MSC v.1916 64 bit (AMD64)] :: Anaconda, Inc. on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import tensorflow as tf
2020-06-21 04:02:23.205171: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cudart64_101.dll
>>> tf.compat.v1.disable_eager_execution()
>>> hello = tf.constant('hello,tensorflow')
>>> sess= tf.compat.v1.Session()
2020-06-21 04:02:25.472006: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library nvcuda.dll
2020-06-21 04:02:25.580727: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1555] Found device 0 with properties:
pciBusID: 0000:00:1e.0 name: Tesla T4 computeCapability: 7.5
coreClock: 1.59GHz coreCount: 40 deviceMemorySize: 14.85GiB deviceMemoryBandwidth: 298.08GiB/s
2020-06-21 04:02:25.589145: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cudart64_101.dll
2020-06-21 04:02:25.600717: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cublas64_10.dll
2020-06-21 04:02:25.614987: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cufft64_10.dll
2020-06-21 04:02:25.622689: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library curand64_10.dll
2020-06-21 04:02:25.637295: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cusolver64_10.dll
2020-06-21 04:02:25.645685: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cusparse64_10.dll
2020-06-21 04:02:25.673263: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cudnn64_7.dll
2020-06-21 04:02:25.678463: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1697] Adding visible gpu devices: 0
2020-06-21 04:02:25.683543: I tensorflow/core/platform/cpu_feature_guard.cc:142] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX AVX2
2020-06-21 04:02:25.690563: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1555] Found device 0 with properties:
pciBusID: 0000:00:1e.0 name: Tesla T4 computeCapability: 7.5
coreClock: 1.59GHz coreCount: 40 deviceMemorySize: 14.85GiB deviceMemoryBandwidth: 298.08GiB/s
2020-06-21 04:02:25.701724: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cudart64_101.dll
2020-06-21 04:02:25.705676: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cublas64_10.dll
2020-06-21 04:02:25.711700: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cufft64_10.dll
2020-06-21 04:02:25.716021: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library curand64_10.dll
2020-06-21 04:02:25.720220: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cusolver64_10.dll
2020-06-21 04:02:25.724584: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cusparse64_10.dll
2020-06-21 04:02:25.732739: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cudnn64_7.dll
2020-06-21 04:02:25.738786: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1697] Adding visible gpu devices: 0
2020-06-21 04:02:26.578678: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1096] Device interconnect StreamExecutor with strength 1 edge matrix:
2020-06-21 04:02:26.583080: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1102] 0
2020-06-21 04:02:26.586153: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1115] 0: N
2020-06-21 04:02:26.591704: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1241] Created TensorFlow device (/job:localhost/replica:0/task:0/device:GPU:0 with 14239 MB memory) -> physical GPU (device: 0, name: Tesla T4, pci bus id: 0000:00:1e.0, compute capability: 7.5)
>>> print(sess.run(hello))
b'hello,tensorflow'

这个不是问题

1
2
3
4
5
import tensorflow as tf
tf.compat.v1.disable_eager_execution()
hello = tf.constant('hello,tensorflow')
sess= tf.compat.v1.Session()
print(sess.run(hello))

这里需要注意的是,由于本文使用的Tensorflow版本是2.1,原来1.1版本中的Session方法已经过期,因此在2.x上调用Session的方法需要修改为compat.v1.Session。否则会报告方法不可用。
只要最后打出了hello表示配置成功。
参考内容(https://blog.bitipcman.com/install-tensorflow-on-windows/)


参考文献

菜鸟教程
国内镜像
PiP Install TensorFlow 报错*** is not a supported wheel on this platform.
tensorflow出现 ImportError: DLL load failed: 找不到指定的程序
解决 tensorflow-gpu 2.1 出现错误 “Could not load dynamic library ‘cudart64_101.dll’
cudart64_101.dll.html文件下载
在Windows下安装Tensorflow