基于Anaconda3下安装TensorFlow-gpu的方法和遇到的坑

前提条件:已经装好了anaconda3,CUDA和CUDNN,只针对TensorFlow本身的坑。

首先看自己电脑能装哪个版本的TensorFlow。

  1. 首先创建python环境,给这个python环境取名叫TensorFlow。 conda create -n tensorflow python=3.6(后文我的环境叫mytensorflow是因为是第二次安装)
    再这之前建议配置一下从镜像源下载,不然即使是科学上网也顶不住。
    直接打开anaconda的命令行输入即可。
    conda config --add channels https://mirrors.ustc.edu.cn/anaconda/pkgs/free/  
    conda config --set show_channel_urls yes  
    
  2. 同样一打开anaconda就可以执行命令anaconda search -t conda tensorflow会显示出目前可供选择的tensorflow版本,按平台选择版本

在这里插入图片描述

  1. 执行命令anaconda show conda-forge/tensorflow获取对应版本的安装命令conda-forge/tensorflow为选择的版本名称
    在这里插入图片描述
  2. 因为官方源太慢了,即使我科学上网也很慢,所以用了中科大的镜像源。选择的是我能使用的最新的版本。这里贴出来的表是windows的。

https://blog.csdn.net/oMoDao1/article/details/83241074#commentBox
这里面有很全的版本对应关系。

pip install tensorflow-gpu==1.10.0
  1. 然后输入测试代码就可以了!
    一个随便拿来的测试代码。
import tensorflow as tf

with tf.device('/cpu:0'):
    a = tf.constant([1.0, 2.0, 3.0], shape=[3], name='a')
    b = tf.constant([1.0, 2.0, 3.0], shape=[3], name='b')
with tf.device('/gpu:1'):
    c = a + b

# 注意:allow_soft_placement=True表明:计算设备可自行选择,如果没有这个参数,会报错。
# 因为不是所有的操作都可以被放在GPU上,如果强行将无法放在GPU上的操作指定到GPU上,将会报错。
sess = tf.Session(config=tf.ConfigProto(allow_soft_placement=True, log_device_placement=True))
# sess = tf.Session(config=tf.ConfigProto(log_device_placement=True))
sess.run(tf.global_variables_initializer())
print(sess.run(c))

真正结果前有一堆东西是正常的,是对用到了哪块GPU的描述。(注意这里是pycharm里面的。)
在这里插入图片描述
然后一个在线编译的平台。

install jupyter

接下来安装Keras

首先安装MinGW包:conda install mingw libpython
然后配置环境变量。在TensorFlow的虚拟环境下找。
在这里插入图片描述
然后安装theano(Theano是一个Python库,可以在CPU或GPU上运行快速数值计算。 这是Python深度学习中的一个关键基础库,你可以直接用它来创建深度学习模型或包装库,大大简化了程序。), 继续输入命令:pip install theano完成后在python环境下>>>import theano测试一下。
在这里插入图片描述安装keras,在命令行输入: pip install keras
在这里插入图片描述

![在这里插入图片描述](https://img-blog.csdnimg.cn/20190130181540838.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQxMzM3MTAw,size_16,color_FFFFFF,t_70

我遇到过的坑

在这里插入图片描述错误原因:和CUDA版本不匹配。附一张表。

在这里插入图片描述我的是CUDA9.0+CUDAA7.0,版本肯定是12肯定太新了。
查看TensorFlow版本方式:
在这里插入图片描述方法:首先进入环境。

activate tensorflow

接下来在TensorFlow环境下打开python

python
>>import tensorflow as tf

查询tensorflow版本:

>>tf.__version__

查询tensorflow安装路径为:

>>tf.__path_

在这里插入图片描述

那我肯定是卸载先啦,结果居然不能卸载,因为没有安装???
Cannot uninstall requirement tensorflow, not installed
当然啦,因为我本来装得是GPU版本吗

pip uninstall tensorflow-gpu

等吧,然后开始安装正确的版本。注意是pip3,是==。

pip install tensorflow-gpu==1.7

安装完之后在这个环境之下

conda install anaconda

但是这个实在是太慢了太慢了,我还是像镜像源屈服了。
关掉退出,再次打开激活TensorFlow环境后,使用中科大的镜像源。

conda config --add channels https://mirrors.ustc.edu.cn/anaconda/pkgs/free/  
conda config --set show_channel_urls yes  
conda install anaconda

为了防止以后出问题,最好再移出这个镜像源,将把仓库和源都删除。
用知乎的方法不行。

https://zhuanlan.zhihu.com/p/36948027

注意他使用的是清华镜像源,改成我自己用的中科大的源就好了。

conda config --remove channels 'https://mirrors.ustc.edu.cn/anaconda/pkgs/free/'
conda config --remove channels 'https://mirrors.ustc.edu.cn/anaconda/pkgs/main/'

在这里插入图片描述但是不行。
在这里插入图片描述
所以最后直接换成默认源。换完之后记得要查看是否换成了默认的。

conda config --remove-key channels
conda config --show

这里据说是更新numpy。
更新方法。

pip install -U <包名> 
或: pip install <包名> –upgrade

但是输入后却显示已经是最新版本了。
在这里插入图片描述终于在一个博客找到答案。

https://blog.csdn.net/u012145252/article/details/82628419

进入D:\Anaconda3\envs\tensorflow\Lib\site-packages,根据之前的报错搜索16,删掉,可以安装了。
在这里插入图片描述
排除问题:Python是32位的

>>> import platform
>>> platform.architecture()

输出结果:
在这里插入图片描述

发布了38 篇原创文章 · 获赞 3 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/qq_41337100/article/details/86710551