conda安装tensorflow-gpu=1.13.1(一条命令解决!!!附带condarc文件,1.x版本应该都可)

引言

网上一大堆安装tenflow-gpu版本的教程,无外乎说的都需要在NVIDIA官网上安装对应版本cuda和cudnn,然后配置环境什么的,显得十分繁琐,而且不容易安装成功,而且TensorFlow官网上好像也没有1.13.1版本的gpu安装
tensorflow-gpu对应版本
由于我当时需要搭建一个1.13.1环境TensorFlow-gpu,所以我试验了一下,参考相关文章,尝试了一天(求赞求赞,别下次一定了)!!!终于把TensorFlow-gpu==1.13.1版本的环境给安装好了

我的环境

  • Windows10(RTX 3060, i7-10700 CPU @ 2.90GHz)
  • Anaconda3
  • Python3.6

一条命令解决

你们的conda如果经常出现找不到某个python包,或者比较慢(那个斜杠一直在那里转转转!!)等等一些问题,大概率是你们的源没有配置好或者选择好,这里我给出一份.condarc文件(目前我试了之后是可以成功安装包,以后能不能用我说不好)

这里我参考的是这篇文章:https://blog.csdn.net/strawberry47/article/details/109519516
这个源比我之前使用的清华源要快

channels:
  - defaults
show_channel_urls: true
channel_alias: https://mirrors.bfsu.edu.cn/anaconda
default_channels:
  - https://mirrors.bfsu.edu.cn/anaconda/pkgs/main
  - https://mirrors.bfsu.edu.cn/anaconda/pkgs/free
  - https://mirrors.bfsu.edu.cn/anaconda/pkgs/r
  - https://mirrors.bfsu.edu.cn/anaconda/pkgs/pro
  - https://mirrors.bfsu.edu.cn/anaconda/pkgs/msys2
custom_channels:
  conda-forge: https://mirrors.bfsu.edu.cn/anaconda/cloud
  msys2: https://mirrors.bfsu.edu.cn/anaconda/cloud
  bioconda: https://mirrors.bfsu.edu.cn/anaconda/cloud
  menpo: https://mirrors.bfsu.edu.cn/anaconda/cloud
  pytorch: https://mirrors.bfsu.edu.cn/anaconda/cloud
  simpleitk: https://mirrors.bfsu.edu.cn/anaconda/cloud
  
创建环境

conda create -n tf1 python=3.6

一条命令安装tensorflow-gpu

conda install tensorflow-gpu==1.13.1

这里需要说明的是,使用GPU版本的TensorFlow需要安装cudatoolkit包(cuda版本)和cudnn包(cudnn版本),在1.x版本的gpu中,官网给的版本一般都是cudNN为7.4、CUDA为10.0,但是遗憾是的很多conda国内源都没有7.4的cudnn安装包,所以很多人就卡这里了。

但是但是为什么之前我说一条命令就可以解决安装GPU版本的TensorFlow,其实上面那个conda命令就可以附带安装cudatoolkit和cudnn包了(虽然不是官网指定版本的,但是可以使用,亲自试过了),是不是很神奇!!!

下面是我安装结果,可以参考一下:

(tf1) D:\project\saliency-master-8.9>python -V
Python 3.6.13 :: Anaconda, Inc.

(tf1) D:\project\saliency-master-8.9>conda list tensor
# packages in environment at C:\Users\pengshuai\anaconda3\envs\tf1:
#
# Name                    Version                   Build  Channel
tensorboard               1.13.1           py36h33f27b4_0    defaults
tensorflow                1.13.1          gpu_py36h9006a92_0    defaults
tensorflow-base           1.13.1          gpu_py36h871c8ca_0    defaults
tensorflow-estimator      1.13.0                     py_0    defaults
tensorflow-gpu            1.13.1               h0d30ee6_0    defaults

(tf1) D:\project\saliency-master-8.9>conda list cud
# packages in environment at C:\Users\pengshuai\anaconda3\envs\tf1:
#
# Name                    Version                   Build  Channel
cudatoolkit               10.0.130                      0    defaults
cudnn                     7.6.5                cuda10.0_0    defaults

(tf1) D:\VR_project\saliency-master-8.9>

看到了吗,其中cudatoolkit是10.0版本,但是cudnn是7.6版本的,可以简单test一下:

(tf1) D:\VR_project\saliency-master-8.9>ipython
Python 3.6.13 |Anaconda, Inc.| (default, Mar 16 2021, 11:37:27) [MSC v.1916 64 bit (AMD64)]
Type 'copyright', 'credits' or 'license' for more information
IPython 6.1.0 -- An enhanced Interactive Python. Type '?' for help.

In [1]: import tensorflow as tf

In [2]: tf.test.is_gpu_available()
2021-08-11 20:52:56.372460: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1512] Adding visible gpu devices: 0
2021-08-11 20:52:56.372640: I tensorflow/core/common_runtime/gpu/gpu_device.cc:984] Device interconnect StreamExecutor with strength 1 edge matrix:
2021-08-11 20:52:56.372753: I tensorflow/core/common_runtime/gpu/gpu_device.cc:990]      0
2021-08-11 20:52:56.372866: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1003] 0:   N
2021-08-11 20:52:56.373009: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1115] Created TensorFlow device (/device:GPU:0 with 10627 MB memory) -> physical GPU (device: 0, name: NVIDIA GeForce RTX 3060, pci bus id: 0000:01:00.0, compute capability: 8.6)
Out[2]: True
小插曲:降低numpy版本

如果你按照上面的步骤安装tensorflow-gpu,好像会默认安装numpy1.19.x版本的,所以
import tensorflow出现一些警告的话,你需要降低一下numpy版本,这里我降低到1.16.0,也是一条命令解决战斗!!!conda install numpy=1.16.0

参考:

  1. 解决python调用TensorFlow时出现FutureWarning: Passing (type, 1) or ‘1type’ as a synonym of type is deprecate
  2. Anaconda package版本降级

测试代码

参考:

  1. tensorflow-gpu=2.3.0安装和加速测试
  2. TensorFlow2.x,GPU代码测试
# !nvidia-smi
import tensorflow as tf
import timeit
import os

os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'

with tf.device('/cpu:0'):
    cpu_a = tf.random.normal([100000, 10000])
    cpu_b = tf.random.normal([10000, 20000])
    print(cpu_a.device, cpu_b.device)

with tf.device('/gpu:0'):
    gpu_a = tf.random.normal([100000, 10000])
    gpu_b = tf.random.normal([10000, 20000])
    print(gpu_a.device, gpu_b.device)


def cpu_run():
    with tf.device('/cpu:0'):
        c = tf.matmul(cpu_a, cpu_b)
    return c


def gpu_run():
    with tf.device('/gpu:0'):
        c = tf.matmul(gpu_a, gpu_b)
    return c


# warm up
cpu_time = timeit.timeit(cpu_run, number=10)
gpu_time = timeit.timeit(gpu_run, number=10)
print('warmup:', cpu_time, gpu_time)

cpu_time = timeit.timeit(cpu_run, number=1000)
gpu_time = timeit.timeit(gpu_run, number=1000)
print('run time:', cpu_time, gpu_time)

print('GPU', tf.test.is_gpu_available())

最后我的输出是:

/device:CPU:0 /device:CPU:0
/device:GPU:0 /device:GPU:0
warmup: 0.0058093 0.005724600000000001
run time: 0.5904437 0.5871983
GPU True

不过我也不知道为啥gpu和cpu差不多。。。不管了,反正安装成功了

最后

容我吐槽一下,TensorFlow这个版本对应关系可太难安装了,搞了一天😭,我可太难了,所以求三连求三连求三连👍,别下次一定下次一定下次一定了👋

另外如果需要安装tensorflow-gpu==2.2.0的可以参考一个大佬的这篇博客:https://blog.csdn.net/weixin_45092662/article/details/106980282

  • 58
    点赞
  • 56
    收藏
    觉得还不错? 一键收藏
  • 13
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 13
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值