Windows10 配置 Anaconda+ Python3.6+ TensorFlow+ PyCharm
过去的一段时间,我一直在研究深度学习相关。在这个领域目前最常见的语言和事实上的标准是Python。与任何其他编程语言和工具一样,总是需要学习和熟悉的众多工具和框架,以便充分利用它们。
Anaconda的一个小问题
虽然Anaconda很好,但是目前它和TensorFlow配合上有一点小问题:
目前官网最新的Anaconda是集成Python 3.7的环境,注意是Python 3.7。
而Python 3.7和当前最新的TensorFlow2.2.0 稳定版是不兼容的!
所以这里我将以自己利用Anaconda安装TensorFlow的经验分享于你, 介绍如何在集成Python3.7的Anaconda安装Python 3.6和TensorFlow-GPU。
电脑配置(供参考)
这里是我的电脑配置,供参考。 当然这只是一个入门级的配置。
- Windows 10 (家庭版)
- Intel® Core™ i7-9700 CPU @ 3.20 GHz
- 8 GB Memory
- 1 TB Hard Disk
- NVIDIA GeForce GTX 1650
下面进入正题,建议按照这个顺序来安装是比较合理的。
一、创建Python 3.6环境
右键管理员启动anaconda Prompt
创建python36
这个环境:
conda create --name python36 python=3.6
进入python36
这个环境:
conda activate python36
如果有需要回到 Python 3.7:
conda deactivate
二、安装 tensorflow-gpu
确认已经安装显卡驱动
注意:
我们并不需要单独安装CUDA Toolkit 或者cuDNN。因为在安装TensorFlow-GPU的时候,这两个libraries会被自动安装。
在安装TensorFlow-GPU之后,用conda list
查看,可以看到cudatoolit和cudnn的版本,我的如下:cudatoolkit 10.0、cudnn 7.6.1。
开始装TensorFlow-gpu
先确保是在python36
这个环境下:
conda activate python36
开始安装:
(建议修改为清华源:
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge/
conda config --set show_channel_urls yes
)
conda install tensorflow-gpu
检查TensorFlow是否已安装好
在python36里,进入python,然后输入:
import tensorflow as tf
hello = tf.constant('Hello , Tensorflow! ')
sess = tf.Session()
print(sess.run(hello))
输出应为:
b'Hello , Tensorflow! '
Disable GPU的Log信息
可能会有一堆的Warning,例如:
2020-06-14 17:25:51.878252: I tensorflow/core/platform/cpu_feature_guard.cc:141] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX AVX2
2020-06-14 17:25:52.221768: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1432] Found device 0 with properties:
name: GeForce GTX 1650 major: 6 minor: 1 memoryClockRate(GHz): 1.683
pciBusID: 0000:01:00.0
totalMemory: 8.00GiB freeMemory: 6.64GiB
2020-06-14 17:25:52.227274: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1511] Adding visible gpu devices: 0
2020-06-14 17:25:52.655576: I tensorflow/core/common_runtime/gpu/gpu_device.cc:982] Device interconnect StreamExecutor with strength 1 edge matrix:
2020-06-14 17:25:52.658983: I tensorflow/core/common_runtime/gpu/gpu_device.cc:988] 0
2020-06-14 17:25:52.660728: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1001] 0: N
2020-06-14 17:25:52.662681: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1115] Created TensorFlow device (/job:localhost/replica:0/task:0/device:GPU:0 with 6396 MB memory) -> physical GPU (device: 0, name: GeForce GTX 1070, pci bus id: 0000:01:00.0, compute
解决办法:
在Python Code前设置log等级:
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
TensorFlow2.0版本修改了许多函数名字
tf.sub()更改为tf.subtract()
tf.mul()更改为tf.multiply()
tf.types.float32更改为tf.float32
tf.pact()更改为tf.stact()
tf.Session()更改为tf.compat.v1.Session()
将Anaconda创建的环境加入到Pycharm中
- 在PyCharm中新建Project
- 在“Project Interpreter”中选择“Existing interpreter”
- 选择Python36这个路径下的python.exe作为interpreter