(python3.7+Anaconda3.7+CUDA10.0/CUDA10.1+Cudnn裸机配置tensorflow2.0-gpu的系列坑)
安装历程
这一个月十分水逆,买来的零件自己组装主机,可是刚买来电源就是坏的,吓得自己一身冷汗,但好在京东自营的服务是真的好,于是在一周内完成了组装及点亮。作为一个刚组装的机器,用的cpu为R5 3600、显卡为RTX2060s、内存/主板/固态等等都可以忽略不计了,拿到电脑的第一刻先把VS/Pycharm2019/Python3.7下载了(后续发现可以不用单独安装Python3.7),然后跟着网上一点点尝试,最终成功的安装上并运行了Tensorflow-gpu。
所需要准备的软件
- Anaconda3.7(因为此时安装的最高版本为3.7,直接官网下载最新的就好)。
- Pycharm2019(最后发现也可以不用下载PycharmIDE,Anaconda3.7中自带编译编写Python的界面,但介于自己已经熟悉了Pycharm的书写环境还是下载下来了)。Pycharm2019破解方法
- CUDA10.0或者CUDA10.1版本(直接在官网下载,其他网友说原网址下载速度慢可以将下载链接复制粘贴到迅雷中下载,自己没遇到这个问题)。
- Cudnn版本(Cudnn一定要选择和CUDA版本相匹配的版本)。CUDA/Cudnn下载安装方法、注意版本
配置tensorflow-gpu环境
当把Anaconda3.7安装完之后,用管理员的方法打开Anaconda Prompt,首先对Anaconda下载源进行换源,加快下载速度。(都说清华和中科院的下载源停止使用了,但不知道怎么用了还是可以加速)
将下面三条指令一条一条输入到命令终端中,此时没有反馈,不要慌。
1 |
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
2 |conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/
3 |conda config --set show_channel_urls yes
换源成功后,接下来创建新的environment,输入以下指令
conda create -n tensorflow python=3.6
其中tensorflow为新environment的名称、python=3.7创建的的python的环境为3.7版本的,默认现在更新到了3.8的版本了
然后复制进入pip指令来更换pip源,同样是为了加快安装速度:
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple pip -U
这个时候你的pip换源就设置好了,然后安装tensorflow-gpu
pip install tensorflow-gpu
最后通过以下指令看看你的anaconda环境信息和位置,便于pycharm的选择以及自己的激活当前网络。
conda info -e
最后通过使用以下指令进行激活你所创建的新环境,并尝试tensorflow-gpu是否可以使用。
activate tensorflow
进行以下环境,并在当前环境输入python
指令,接着输入import tensorflow
指令,若出现以下情况则表示tensorflow-gpu环境已经搭建完成。
若tensorflow程序不能运行可以考虑可能tensorflow安装为2.0,需要在创建sess的使用使用以下两个代码尝试:
原测试代码
import tensorflow as tf
hello = tf.constant('Hello, TensorFlow!')
sess = tf.Session()
print(sess.run(hello))
报错为
Traceback (most recent call last):
File “E:/Code/python_first/hellow.py”, line 5, in
sess = tf.Session()
AttributeError: module ‘tensorflow’ has no attribute ‘Session’
此时表示安装的为tensorflow2.0的版本,需要修改指令为
import tensorflow as tf
hello = tf.constant('Hello, TensorFlow!')
sess = tf.compat.v1.Session()
print(sess.run(hello))
报错为
Traceback (most recent call last):
File “E:/Code/python_first/hellow.py”, line 7, in
print(sess.run(hello))
File “D:\pycharm\Anaconda3.7\envs\tensorflow_gpu\lib\site-packages\tensorflow_core\python\client\session.py”, line 956, in run
run_metadata_ptr)
File “D:\pycharm\Anaconda3.7\envs\tensorflow_gpu\lib\site-packages\tensorflow_core\python\client\session.py”, line 1105, in _run
raise RuntimeError('The Session graph is empty. Add operations to the ’
RuntimeError: The Session graph is empty. Add operations to the graph before calling run().
此时代码需要修改为(将动态图机制关闭)
import tensorflow as tf
tf.compat.v1.disable_eager_execution()
hello = tf.constant('Hello, TensorFlow!')
sess = tf.compat.v1.Session()
print(sess.run(hello))
D:\pycharm\Anaconda3.7\envs\tensorflow_gpu\python.exe E:/Code/python_first/hellow.py
2019-12-03 13:34:55.388825: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cudart64_100.dll
2019-12-03 13:34:56.559638: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library nvcuda.dll
2019-12-03 13:34:56.656692: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1618] Found device 0 with properties:
name: GeForce RTX 2060 SUPER major: 7 minor: 5 memoryClockRate(GHz): 1.65
pciBusID: 0000:26:00.0
2019-12-03 13:34:56.656822: I tensorflow/stream_executor/platform/default/dlopen_checker_stub.cc:25] GPU libraries are statically linked, skip dlopen check.
2019-12-03 13:34:56.657218: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1746] Adding visible gpu devices: 0
2019-12-03 13:34:56.657490: I tensorflow/core/platform/cpu_feature_guard.cc:142] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2
2019-12-03 13:34:56.658916: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1618] Found device 0 with properties:
name: GeForce RTX 2060 SUPER major: 7 minor: 5 memoryClockRate(GHz): 1.65
pciBusID: 0000:26:00.0
2019-12-03 13:34:56.659030: I tensorflow/stream_executor/platform/default/dlopen_checker_stub.cc:25] GPU libraries are statically linked, skip dlopen check.
2019-12-03 13:34:56.659430: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1746] Adding visible gpu devices: 0
2019-12-03 13:34:57.300810: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1159] Device interconnect StreamExecutor with strength 1 edge matrix:
2019-12-03 13:34:57.300897: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1165] 0
2019-12-03 13:34:57.300944: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1178] 0: N
2019-12-03 13:34:57.301530: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1304] Created TensorFlow device (/job:localhost/replica:0/task:0/device:GPU:0 with 6311 MB memory) -> physical GPU (device: 0, name: GeForce RTX 2060 SUPER, pci bus id: 0000:26:00.0, compute capability: 7.5)
b’Hello, TensorFlow!’
Process finished with exit code 0
环境就完全没有问题了。