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
关于版本说明
torch
与torchvision
对应关系,来源:官方Repo: vision
torch | torchvision | python |
---|---|---|
main / nightly | main / nightly | >=3.8, <=3.10 |
2.0.0 | 0.15.1 | >=3.8, <=3.10 |
1.13.0 | 0.14.0 | >=3.7.2, <=3.10 |
1.12.0 | 0.13.0 | >=3.7, <=3.10 |
1.11.0 | 0.12.3 | >=3.7, <=3.10 |
1.10.2 | 0.11.3 | >=3.6, <=3.9 |
1.10.1 | 0.11.2 | >=3.6, <=3.9 |
1.10.0 | 0.11.1 | >=3.6, <=3.9 |
1.9.1 | 0.10.1 | >=3.6, <=3.9 |
1.9.0 | 0.10.0 | >=3.6, <=3.9 |
1.8.2 | 0.9.2 | >=3.6, <=3.9 |
1.8.1 | 0.9.1 | >=3.6, <=3.9 |
1.8.0 | 0.9.0 | >=3.6, <=3.9 |
1.7.1 | 0.8.2 | >=3.6, <=3.9 |
1.7.0 | 0.8.1 | >=3.6, <=3.8 |
1.7.0 | 0.8.0 | >=3.6, <=3.8 |
1.6.0 | 0.7.0 | >=3.6, <=3.8 |
1.5.1 | 0.6.1 | >=3.5, <=3.8 |
1.5.0 | 0.6.0 | >=3.5, <=3.8 |
1.4.0 | 0.5.0 | ==2.7, >=3.5, <=3.8 |
1.3.1 | 0.4.2 | ==2.7, >=3.5, <=3.7 |
1.3.0 | 0.4.1 | ==2.7, >=3.5, <=3.7 |
1.2.0 | 0.4.0 | ==2.7, >=3.5, <=3.7 |
1.1.0 | 0.3.0 | ==2.7, >=3.5, <=3.7 |
<=1.0.1 | 0.2.2 | ==2.7, >=3.5, <=3.7 |
torch
与torchaudio
对应关系,来源:官方Repo: audio
torch | torchaudio | python |
---|---|---|
main / nightly | main / nightly | >=3.8, <=3.10 |
2.0.0 | 2.0.1 | >=3.8, <=3.10 |
1.13.1 | 0.13.1 | >=3.7, <=3.10 |
1.13.0 | 0.13.0 | >=3.7, <=3.10 |
1.12.0 | 0.12.0 | >=3.7, <=3.10 |
1.11.0 | 0.11.0 | >=3.7, <=3.9 |
1.10.0 | 0.10.0 | >=3.6, <=3.9 |
1.9.1 | 0.9.1 | >=3.6, <=3.9 |
1.9.0 | 0.9.0 | >=3.6, <=3.9 |
1.8.2 | 0.8.2 | >=3.6, <=3.9 |
1.8.1 | 0.8.1 | >=3.6, <=3.9 |
1.8.0 | 0.8.0 | >=3.6, <=3.9 |
1.7.1 | 0.7.2 | >=3.6, <=3.9 |
1.7.0 | 0.7.0 | >=3.6, <=3.8 |
1.6.0 | 0.6.0 | >=3.6, <=3.8 |
1.5.0 | 0.5.0 | >=3.5, <=3.8 |
1.4.0 | 0.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 版本 | 编译器 | 编译工具 | cuDNN | CUDA |
---|---|---|---|---|---|
tensorflow_gpu-2.0.0-alpha0 | 2.7、3.3-3.6 | GCC 4.8 | Bazel 0.19.2 | 7.4.1以及更高版本 | CUDA 10.0 (需要 410.x 或更高版本) |
tensorflow_gpu-1.13.0 | 2.7、3.3-3.6 | GCC 4.8 | Bazel 0.19.2 | 7.4 | 10.0 |
tensorflow_gpu-1.12.0 | 2.7、3.3-3.6 | GCC 4.8 | Bazel 0.15.0 | 7 | 9 |
tensorflow_gpu-1.11.0 | 2.7、3.3-3.6 | GCC 4.8 | Bazel 0.15.0 | 7 | 9 |
tensorflow_gpu-1.10.0 | 2.7、3.3-3.6 | GCC 4.8 | Bazel 0.15.0 | 7 | 9 |
tensorflow_gpu-1.9.0 | 2.7、3.3-3.6 | GCC 4.8 | Bazel 0.11.0 | 7 | 9 |
tensorflow_gpu-1.8.0 | 2.7、3.3-3.6 | GCC 4.8 | Bazel 0.10.0 | 7 | 9 |
tensorflow_gpu-1.7.0 | 2.7、3.3-3.6 | GCC 4.8 | Bazel 0.9.0 | 7 | 9 |
tensorflow_gpu-1.6.0 | 2.7、3.3-3.6 | GCC 4.8 | Bazel 0.9.0 | 7 | 9 |
tensorflow_gpu-1.5.0 | 2.7、3.3-3.6 | GCC 4.8 | Bazel 0.8.0 | 7 | 9 |
tensorflow_gpu-1.4.0 | 2.7、3.3-3.6 | GCC 4.8 | Bazel 0.5.4 | 6 | 8 |
tensorflow_gpu-1.3.0 | 2.7、3.3-3.6 | GCC 4.8 | Bazel 0.4.5 | 6 | 8 |
tensorflow_gpu-1.2.0 | 2.7、3.3-3.6 | GCC 4.8 | Bazel 0.4.5 | 5.1 | 8 |
tensorflow_gpu-1.1.0 | 2.7、3.3-3.6 | GCC 4.8 | Bazel 0.4.2 | 5.1 | 8 |
tensorflow_gpu-1.0.0 | 2.7、3.3-3.6 | GCC 4.8 | Bazel 0.4.2 | 5.1 | 8 |
现在NVIDIA的显卡驱动程序已经更新到 10.1版本,最新的支持CUDA 10.1版本的cuDNN为7.5.0