关于TensorFlow和Pytorch的GPU版本配置问题

  相关安装问题汇总

关于版本是否适配

python, cuda, cudnn, tf/torch版本需要适配! 组合不是任意的,建议先看好版本再去下载。

下载链接

显卡驱动下载https://www.nvidia.com/Download/index.aspx?lang=en-us

cuda版本下载https://developer.nvidia.com/cuda-downloads cuda历史版本下载https://developer.nvidia.com/cuda-toolkit-archive cudnn版本下载https://developer.nvidia.com/rdp/cudnn-download cudnn历史版本下载https://developer.nvidia.com/rdp/cudnn-archive

python+cuda+cudnn+tf版本适配查询https://www.tensorflow.org/install/source_windows pytorch+cuda版本适配查询https://pytorch.org/get-started/previous-versions/

pytorch下载https://pytorch.org/get-started/locally/ pytorch历史版本https://pytorch.org/get-started/previous-versions/

环境

分清本地环境和虚拟环境。 在cmd直接安装的是在本地环境里,若在虚拟环境中安装,需要在cmd中切换到虚拟环境。

1
2
3
4
5
6
7
8
# 创建新的conda环境
conda create -n poiRec python=3.7
# 进入虚拟环境
activate poiRec
# 退出当前conda环境
conda deactivate
删除环境(先退出该环境)
conda remove -n poiRec --all

conda一键配环境

1
2
3
4
5
6
7
8
# cuda 不写版本自动选择
conda install cudatoolkit=10.0
# cudnn 不写版本自动选择
conda install cudnn=7.0.5
# 安装tf
conda install tensorflow-gpu==1.15.0
# 安装pytorch 复制官方命令 注意版本适配
conda install pytorch torchvision torchaudio cudatoolkit=11.3 -c pytorch

CUDA

查看CUDA版本 C:\Windows\System32>nvidia-smi.exe cmd中在C:\Windows\System32下

1
nvidia-smi

Tensorflow-gpu

查看版本

1
2
import tensorflow as tf
print(tf.__version__)

查看设备

1
2
3
4
5
6
import tensorflow as tf

gpus = tf.config.experimental.list_physical_devices(device_type='GPU')
cpus = tf.config.experimental.list_physical_devices(device_type='CPU')
print(gpus)
print(cpus)

查看是否有GPU

1
2
3
4
import tensorflow as tf
 
gpu_device_name = tf.test.gpu_device_name()
print(gpu_device_name)

查看GPU是否可用

1
tf.test.is_gpu_available()
1
2
3
4
5
6
7
8
from tensorflow.python.client import device_lib
 
# 列出所有的本地机器设备
local_device_protos = device_lib.list_local_devices()
print(local_device_protos)
 
# 只打印GPU设备
[print(x) for x in local_device_protos if x.device_type == 'GPU']

查看GPU是否可用的第二种方法

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
import tensorflow as tf

# 指定在cpu上运行
with tf.device('/cpu:0'):
    cpu_a = tf.random.normal([10000, 1000])
    cpu_b = tf.random.normal([1000, 2000])
    cpu_c = tf.matmul(cpu_a, cpu_b)
print("cpu_a:", cpu_a.device)
print("cpu_b:", cpu_b.device)
print("cpu_c:", cpu_c.device)
# 查看gpu是否可用
print(tf.config.list_physical_devices('GPU'))
# 指定在gpu上运行
with tf.device('/gpu:0'):
    gpu_a = tf.random.normal([10000, 1000])
    gpu_b = tf.random.normal([1000, 2000])
    gpu_c = tf.matmul(gpu_a, gpu_b)
print("gpu_a:", gpu_a.device)
print("gpu_b:", gpu_b.device)
print("gpu_c:", gpu_c.device)

比较CPU和GPU运行时间

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import tensorflow as tf
import timeit


def cpu_run():
    with tf.device('/cpu:0'):
        cpu_a = tf.random.normal([10000, 1000])
        cpu_b = tf.random.normal([1000, 2000])
        c = tf.matmul(cpu_a, cpu_b)
    return c


def gpu_run():
    with tf.device('/gpu:0'):
        gpu_a = tf.random.normal([10000, 1000])
        gpu_b = tf.random.normal([1000, 2000])
        c = tf.matmul(gpu_a, gpu_b)
    return c


cpu_time = timeit.timeit(cpu_run, number=10)
gpu_time = timeit.timeit(gpu_run, number=10)
print("cpu:", cpu_time, "  gpu:", gpu_time)

Pytorch-gpu

查看版本

1
2
3
4
import torch
print(torch.__version__) #查看pytorch版本
print(torch.cuda_version) #查看cuda版本
print(torch.cuda.is_available()) #查看cuda是否可用

装pytorch+CUDA

1
pip3 install torch==1.8.1+cu101 torchvision==0.9.1+cu101  -f https://download.py torch.org/whl/cu101/torch_stable.html

安装特定版本

1
pip install torch==1.1.0 -f https://download.pytorch.org/whl/torch_stable.html
1
2
# 验证GPU是否能用
print("torch.cuda.is_available():", torch.cuda.is_available())
使用 Hugo 构建
主题 StackJimmy 设计