环境部署|Pytorch和Tensorflow安装(Win和Linux)

802 阅读3分钟

Ubuntu/win安装Pytorch和Tensorflow

说明:

这两种框架的搭建,均基于Anaconda进行搭建。先在系统中安装Anaconda软件。

一、Pytorch的搭建

windows安装

(1)搭建参考官网给的命令,pytorch官网

(2)下载地址:download.pytorch.org/whl/torch_s…

从上述地址自行下载对应的版本(要求符合python的版本,cWuda 的版本信息)

比如我的CUDA是11.2 ,python是3.7版本,在Windows系统下

那么下载cu111/torch-1.8.1%2Bcu111-cp36-cp36m-win_amd64.whl

下载完成后,在cmd界面跳转到文件所在的目录下面,然后执行安装

pip install cu111/torch-1.8.1%2Bcu111-cp36-cp36m-win_amd64.whl

Linux安装

(1)创建虚拟环境

(2)安装pytorch

我们改变策略 。记录好需要安装的包,直接用pip从豆瓣源下载。根据官方命令,我们需要安装的是torch==1.8.1版本,torchvision==0.9.1版本,torchaudio==0.8.1版本。

安装torch==1.8.1

pip install torch==1.8.1 -i https://pypi.doubanio.com/simple --trusted-host pypi.doubanio.com

安装torchvision==0.9.1

pip install torchvision==0.9.1 -i https://pypi.douban.com/simple --trusted-host pypi.douban.com

安装torchaudio==0.8.1

pip install torchaudio==0.8.1 -i https://pypi.douban.com/simple --trusted-host pypi.douban.com

关于版本说明

torchtorchvision对应关系,来源:官方Repo: vision

torchtorchvisionpython
main / nightlymain / nightly>=3.8, <=3.10
2.0.00.15.1>=3.8, <=3.10
1.13.00.14.0>=3.7.2, <=3.10
1.12.00.13.0>=3.7, <=3.10
1.11.00.12.3>=3.7, <=3.10
1.10.20.11.3>=3.6, <=3.9
1.10.10.11.2>=3.6, <=3.9
1.10.00.11.1>=3.6, <=3.9
1.9.10.10.1>=3.6, <=3.9
1.9.00.10.0>=3.6, <=3.9
1.8.20.9.2>=3.6, <=3.9
1.8.10.9.1>=3.6, <=3.9
1.8.00.9.0>=3.6, <=3.9
1.7.10.8.2>=3.6, <=3.9
1.7.00.8.1>=3.6, <=3.8
1.7.00.8.0>=3.6, <=3.8
1.6.00.7.0>=3.6, <=3.8
1.5.10.6.1>=3.5, <=3.8
1.5.00.6.0>=3.5, <=3.8
1.4.00.5.0==2.7, >=3.5, <=3.8
1.3.10.4.2==2.7, >=3.5, <=3.7
1.3.00.4.1==2.7, >=3.5, <=3.7
1.2.00.4.0==2.7, >=3.5, <=3.7
1.1.00.3.0==2.7, >=3.5, <=3.7
<=1.0.10.2.2==2.7, >=3.5, <=3.7

torchtorchaudio对应关系,来源:官方Repo: audio

torchtorchaudiopython
main / nightlymain / nightly>=3.8, <=3.10
2.0.02.0.1>=3.8, <=3.10
1.13.10.13.1>=3.7, <=3.10
1.13.00.13.0>=3.7, <=3.10
1.12.00.12.0>=3.7, <=3.10
1.11.00.11.0>=3.7, <=3.9
1.10.00.10.0>=3.6, <=3.9
1.9.10.9.1>=3.6, <=3.9
1.9.00.9.0>=3.6, <=3.9
1.8.20.8.2>=3.6, <=3.9
1.8.10.8.1>=3.6, <=3.9
1.8.00.8.0>=3.6, <=3.9
1.7.10.7.2>=3.6, <=3.9
1.7.00.7.0>=3.6, <=3.8
1.6.00.6.0>=3.6, <=3.8
1.5.00.5.0>=3.5, <=3.8
1.4.00.4.0==2.7, >=3.5, <=3.8

Tips: 当已经装好torch包时,pip install torchvision torchaudio会自动寻找对应的版本安装。 等待再次验证是否有效。

测试Pytorch

执行命令,检查pytorch是否可以调用gpu

#!/usr/bin/env python
# -*- coding:utf-8 -*-  

import torch
print(torch.__version__)
print(torch.cuda.is_available())
print(torch.cuda.get_device_name(0))
print(torch.rand(2,2).cuda())

当行“torch.cuda.is_available()”显示为True时,即可正常调用GPU

二、Tensorflow的搭建

其他安装办法:tensorflow1.4的详细安装教程_潘旭阳的博客-CSDN博客_tensorflow1.4

注:windows与Linux以下命令均可用

(1)创建虚拟环境

(2)安装tensorflow

在创建好的虚拟环境中安装tensorflow的gpu版本

pip install tensorflow-gpu==2.5.0 -i https://pypi.douban.com/simple --trusted-host pypi.douban.com 

(3)测试tensorflow

安装完tensorflow后,我们执行命令来进行测试(从如下文件中提取 简单测试一些命令即可)

进入创建好的测试代码文件夹lab cd ~/lab

新建测试文件"tf_test.py" gedit tf_test.py

代码如下

#!/usr/bin/env python
# -*- coding:utf-8 -*-  


import tensorflow as tf
import time

#查看是否有GPU
print('***********查看是否有GPU********************')
gpu_device_name = tf.test.gpu_device_name()
print('*********************************')
print(gpu_device_name)
print('*********************************')
time.sleep(3)
print('\n')
#查看GPU是否可用
print("@@@@@@@@@@@@@查看GPU是否可用@@@@@@@@@@@@@@")
print('@@@@@@@@@@@@@@@@@@@@@@@@@@')
print(tf.config.list_physical_devices('GPU'))
print('@@@@@@@@@@@@@@@@@@@@@@@@@@')
time.sleep(3)
print('\n')
print('#############开始进行多GPU计算测试#################')
#多GPU计算测试
# 创建计算图
c = []
for d in ['/device:GPU:0', '/device:GPU:1']:
 with tf.device(d):
   a = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[2, 3])
   b = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[3, 2])
   c.append(tf.matmul(a, b))
with tf.device('/cpu:0'):
 sum = tf.add_n(c)

print(sum)

运行测试文件

python tf_test.py

本文转自 zhuanlan.zhihu.com/p/377184102,如有侵权,请联系删除。

TensorFlow--CUDA版本以及Cudnn的对应关系

1.1 对应表格相应的网址为:

www.tensorflow.org/install/sou…

www.tensorflow.org/install/sou…

最新的TensorFlow版本对应:Tensorflow-gpu与cuda版本匹配,cuda与cudnn版本匹配 - 知乎 (zhihu.com)

版本Python 版本编译器编译工具cuDNNCUDA
tensorflow_gpu-2.0.0-alpha02.7、3.3-3.6GCC 4.8Bazel 0.19.27.4.1以及更高版本CUDA 10.0 (需要 410.x 或更高版本)
tensorflow_gpu-1.13.02.7、3.3-3.6GCC 4.8Bazel 0.19.27.410.0
tensorflow_gpu-1.12.02.7、3.3-3.6GCC 4.8Bazel 0.15.079
tensorflow_gpu-1.11.02.7、3.3-3.6GCC 4.8Bazel 0.15.079
tensorflow_gpu-1.10.02.7、3.3-3.6GCC 4.8Bazel 0.15.079
tensorflow_gpu-1.9.02.7、3.3-3.6GCC 4.8Bazel 0.11.079
tensorflow_gpu-1.8.02.7、3.3-3.6GCC 4.8Bazel 0.10.079
tensorflow_gpu-1.7.02.7、3.3-3.6GCC 4.8Bazel 0.9.079
tensorflow_gpu-1.6.02.7、3.3-3.6GCC 4.8Bazel 0.9.079
tensorflow_gpu-1.5.02.7、3.3-3.6GCC 4.8Bazel 0.8.079
tensorflow_gpu-1.4.02.7、3.3-3.6GCC 4.8Bazel 0.5.468
tensorflow_gpu-1.3.02.7、3.3-3.6GCC 4.8Bazel 0.4.568
tensorflow_gpu-1.2.02.7、3.3-3.6GCC 4.8Bazel 0.4.55.18
tensorflow_gpu-1.1.02.7、3.3-3.6GCC 4.8Bazel 0.4.25.18
tensorflow_gpu-1.0.02.7、3.3-3.6GCC 4.8Bazel 0.4.25.18

现在NVIDIA的显卡驱动程序已经更新到 10.1版本,最新的支持CUDA 10.1版本的cuDNN为7.5.0