安装GPU版本的tensorflow和torch

2021-08-18  本文已影响0人  逍遥_yjz

1. 安装GPU版本的tensorflow

1.1 安装

python3.7下只有tensorflow1.13以上,这里我安装Python3.6,然后依据安装好的cuda和cudnn版本查看对应的tensorflow版本:


已安装:kuda 9.0 cudnn 7.6 ,因此选择TensorFlow 1.12

pip3 install tensorflow-gpu==1.12

等待一会,安装完毕之后我们来测试一下吧。

如果要安装GPU版本的tensorflow,除了要选择当前python支持的版本外,还需注意与想安装的keras版本是否对应。这里有python、tensorflow和keras之间的版本对应关系。

1.2 验证

安装完成后进入python,测试GPU版本的tensorflow是否安装成功:

import tensorflow as tf
print(tf.test.is_gpu_available())
print(tf.test.is_built_with_cuda())

sess = tf.Session()
a=tf.constant(1)
b=tf.constant(2)
print(sess.run(a+b))

输出结果:

image-20210817172811726

以前通过print(tf.test.is_gpu_available())是否打印输出True来证明GPU是否能用,现在不可行啦,在此使用print(tf.test.is_built_with_cuda()),并打印True,证明GPU可以使用。

2.安装GPU版本的Pytorch

2.1 安装

CUDA版本对应的pytorch版本

img

下载torch 1.1.0 及其对应的torchvision 0.3.0,找到V1.1.0下 --> wheel下 选择下载

下载网址:https://pytorch.org/get-started/previous-versions/

image-20210817174009064 image-20210817174045843

下载文件:

image-20210817174222632

2.2 验证

安装后,使用代码验证是否能调用

import torch

print(torch.__version__ )# 查看pytorch版本
print(torch.cuda.is_available())    # 判断pytorch是否支持GPU加速
print(torch.version.cuda)   # 查看CUDA版本
print(torch.backends.cudnn.version())   # 查看cuDNN版本
print(torch.cuda.get_device_name()) # 查看显卡类型,设备索引默认从0开始

输出:

1.1.0
True
9.0
7005
GeForce GT 635M

如果输出为True则表示GPU版本的pytorch安装成功,但是有点小问题。

上一篇 下一篇

猜你喜欢

热点阅读