win10系统在Anaconda下安装Tensorflow2的GPU版本+Tensorflow2.1.0+python3.8+Pycharm(最新,详细,亲测可用)

本篇是tensorflow2-gpu版本的安装指南,前面的步骤跟pytorch-gpu安装非常相似,只要镜像设置好了,后面就非常顺利

这里是不需要额外安装cudatoolkit和cudnn的,因为在使用conda安装tensorflow的时候,cuda会自动帮你安装

我的环境是:win10+Anaconda+python3.8+tensorflow2.1.0

第一步照例是查看cuda的版本,高版本的cuda是可以兼容低版本的cuda的,比如我的电脑支持cuda10.2,我就可以安装cuda9/cuda10.0/cuda10.1,但是反过来就一定运行不了!

1 查看CUDA版本

首先必须要确定自己电脑支持的CUDA版本

打开NVIDIA控制面板,点击 帮助——>系统信息——>组件,查看自己电脑支持的cuda版本

我的是cuda10.2,如下图所示

在这里插入图片描述

2 新建虚拟环境

为了将tensorflow使用的环境与其他环境区分开,需要新建一个tensorflow的虚拟环境

打开anaconda prompt,输入conda create -n tensorflow_gpu

tensorflow_gpu为新建的虚拟环境的名称,可以自定义

如图所示,信息提示如下则安装成功(我这里使用的是pytorch_gpu的截图,tensorflow_gpu也是一样的

在这里插入图片描述

然后切换到该新建的虚拟环境下,使用命令:conda activate tensorflow_gpu

3 配置清华镜像

输入如下命令,这八条命令缺一不可,我家里网差,没有vpn,我前面失败了很多次,就是因为百度上很多配置清华源都只有这前三条,没有后五条,导致后面安装cudatoolkit的时候还是一直卡死,只有这八个命令全部有之后,下载才会顺畅,这一点后面也会说。

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 --set show_channel_urls yes
  • 1
  • 2
  • 3

为了表示强调,我把后面的五个命令分开了,这后面五个命令是一定要有的,切记切记!

conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/msys2/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/bioconda/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/menpo/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/pytorch/
  • 1
  • 2
  • 3
  • 4
  • 5

4 Tensorflow下载

在anaconda prompt中输入如下命令:

conda install tensorflow-gpu==2.0.0
  • 1

输入命令后回车,出现如下界面,如果下载网址全是清华的(tsinghua.edu.cn)那么恭喜你,后面的下载速度应该就很快了,要是其中某一项,比如cudatoolkit的下载网址是default,那你大概率还是会卡在下载这里,只能去检查你前面清华源配置是不是那八个你都配置好了呢?

在这里插入图片描述

后面要做的就是等待了

5 检查是否安装成功

等待下载完成,输入python进入环境,然后依次输入

import tensorflow as tf
print(tf.__version__)
A = tf.constant([[1, 2], [3, 4]])
B = tf.constant([[5, 6], [7, 8]])
C = tf.matmul(A, B)

print(C)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

如果能够最终输出:

tf.Tensor(
[[19 22]
[43 50]], shape=(2, 2), dtype=int32)
  • 1
  • 2
  • 3

说明 TensorFlow 已安装成功!

其他

在使用tensorflow2时,可能会出现各种莫名其妙的报错,这里仅仅列了其中两个,感兴趣的可以看我另一篇文章

tensorflow2踩坑记录

1. module ‘tensorflow’ has no attribute ‘compat’

出现 module ‘tensorflow’ has no attribute ‘compat’ ,查看tensorflow版本

错误原因:tensorflow-gpu版本跟tensorflow-estimator版本不匹配,比如我的tensorflow-gpu版本是2.0.0,但tensorflow-estimator是2.2.0(有可能显示的是2.0.0,但是在程序运行时还是会报错)

在anaconda prompt中输入conda list,查看tensorflow-estimator的版本,我的使用的是tensorflow-gpu是2.0.0,但是tensorflow-estimator是2.2.0的版本,所以会报下面的错误

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-KXymuYjI-1596093914074)(C:\Users\xiaoxi\AppData\Roaming\Typora\typora-user-images\image-20200730103953894.png)]

解决方案:

重新安装 tensorflow-estimator,使用 pip 命令,不要使用 conda 命令,如果使用 conda 命令的话所有的 tensorflow 相关的包都会被卸载掉

先删除电脑上已经安装好的tensorflow-estimator版本,输入:

pip uninstall tensorflow-estimator
  • 1

然后执行:

pip install tensorflow-estimator==2.0.0
pip install tensorflow-gpu=2.0.0
  • 1
  • 2

2. tensorflow.python.framework.errors_impl.InternalError: Blas GEMV launch failed: m=2, n=10 [Op:MatMul] name: MatMul/

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-wY4uwk07-1596093914076)(C:\Users\xiaoxi\AppData\Roaming\Typora\typora-user-images\image-20200730105434255.png)]

  • 经过查找相关资料和自己试验,发现这可能和GPU的内存分配有关系;因为默认情况下在代码中使用GPU时,有把内存占满的趋势;即使有时候计算的数据量并不足以占用整个GPU。
  • 所以,我们的思路之一就是对GPU的使用模式进行设置,如下面的代码所示,我们将GPU设置为memory_growth模式,它的意思是此时我们需要多少GPU内存就使用多少,不会过多占用。
## 列出你所有的物理GPU
gpus = tf.config.experimental.list_physical_devices('GPU')
tf.config.experimental.set_memory_growth(gpus[0], True)
  • 1
  • 2
  • 3

如果你有多个GPU,那么你也可以将所有的GPU都设置成这个模式,如下面代码所示。

for gpu in gpus:
    tf.config.experimental.set_memory_growth(gpu, True)

猜你喜欢

转载自blog.csdn.net/cxin917/article/details/107829482