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

博主分享了一种在Windows10环境下,使用Anaconda快速创建并安装TensorFlow-GPU1.13.1的方法,只需一条命令即可解决所有依赖,包括cudatoolkit和cudnn。通过调整conda配置文件,使用新的国内源,解决了包找不到或下载慢的问题。安装完成后,测试表明TensorFlow-GPU能正常识别RTX3060显卡。此外,还介绍了如何降低numpy版本以避免导入TensorFlow时的警告,并提供了测试代码验证安装成功。
该文章已生成可运行项目,

引言

网上一大堆安装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.youkuaiyun.com/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.youkuaiyun.com/weixin_45092662/article/details/106980282

本文章已经生成可运行项目
conda虚拟环境中安装特定版本的 `tensorflow-gpu` 和 `keras`(如 `tensorflow-gpu==1.13.1` 和 `keras==2.1.5`)需要确保环境中的 Python 版本、CUDA 工具包和 cuDNN 版本与目标 TensorFlow 版本兼容。以下是详细的安装步骤: ### 创建 Conda 虚拟环境 首先,创建一个具有合适 Python 版本的虚拟环境。TensorFlow 1.13.1 推荐使用 Python 3.6.x。 ```bash conda create -n tf131 python=3.6 ``` 激活虚拟环境: ```bash conda activate tf131 ``` ### 安装 tensorflow-gpu 1.13.1 由于 TensorFlow 1.13.1 不再默认出现在 conda 的官方频道中,可以使用 pip 进行安装: ```bash pip install tensorflow-gpu==1.13.1 ``` 如果希望从本地 `.whl` 文件安装(例如在 NVIDIA Jetson Nano 上),可以使用如下命令: ```bash pip install tensorflow_gpu-1.13.1+nv19.3-cp36-cp36m-linux_aarch64.whl --user ``` ### 安装 keras 2.1.5 接下来,安装 Keras 并指定其版本为 2.1.5: ```bash pip install keras==2.1.5 ``` ### 验证安装 安装完成后,可以通过运行以下 Python 脚本来验证是否成功启用了 GPU 支持: ```python import tensorflow as tf a = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[2, 3], name='a') b = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[3, 2], name='b') c = tf.matmul(a, b) sess = tf.Session(config=tf.ConfigProto(log_device_placement=True)) print(sess.run(c)) ``` 如果输出中包含类似 `/device:GPU:0` 的信息,则说明 TensorFlow 成功识别并使用了 GPU[^5]。 ### 兼容性注意事项 1. **Python 版本**:确保使用 Python 3.6.x,因为 TensorFlow 1.x 对 Python 3.7 及以上版本的支持有限。 2. **NumPy 版本**:TensorFlow 1.13.1 可能会与较新版本的 NumPy 冲突,建议安装 NumPy 1.16.x: ```bash pip install numpy==1.16.4 ``` 3. **CUDA 和 cuDNN**:若手动安装 CUDA 和 cuDNN,需确保使用 CUDA 10.0 和 cuDNN 7.4.1.5,以匹配 TensorFlow 1.13.1 的依赖要求[^1]。 4. **Conda 环境配置**:对于 Linux 用户,可能需要手动配置环境变量以确保系统能够正确找到 Conda 安装路径: ```bash export PATH=/XXX/anaconda3/bin:$PATH source /etc/profile ``` 替换 `/XXX/anaconda3` 为实际安装路径[^4]。 ---
评论 19
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值