参考链接Anaconda安装Tensorflow-gpu
需要注意的几点
-
已经通过anaconda安装过tensorflowCPU的不要卸载它,因为tensorflow-CPU是一个独立的编译环境,对任何编译环境都没有影响。
-
也不需要卸载其他的Python版本,因为每个虚拟环境下的Python编辑器都是独立的,直接在虚拟环境中安装Python即可
conda create -n GPU python=3.7 //新建一个python3.7的环境,命名为GPU
-
代码中
conda create -n GPU python=3.7 //新建一个python3.7的环境,命名为GPU
的GPU是虚拟环境的名称,可以根据自己的喜好命名 -
安装完毕tensorflow-GPU后,我通过
conda isntall matplatlib
指令安装其他库,发现报错。这是因为,我还在清华镜像源中,因为前面执行了这段指令: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
于是我重启电脑,重新进入anaconda prompt中,activate虚拟环境,在虚拟环境下通过
pip install matplotlib
安装相应的库。 -
安装完毕只有如何验证是否安装成功?
import tensorflow as tf print(tf.__version__) print("Num GPUs Available: ", len(tf.config.experimental.list_physical_devices('GPU')))
-
执行代码时发现找不到GPU库,我添加以下代码可以解决问题(指定GPU为0):
import os os.environ["CUDA_VISIBLE_DEVICES"] = "0"