脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服务器之家 - 脚本之家 - Python - pytorch 如何用cuda处理数据

pytorch 如何用cuda处理数据

2021-11-19 13:28学渣渣渣渣渣 Python

考虑到各种运算只能在cpu或者gpu运算,不能混和运算,本文介绍常用的几种把数据挪到gpu或者直接在gpu创建数据再进行运算的方法

1 设置GPU的一些操作

设置在os端哪些GPU可见,如果不可见,那肯定是不能够调用的~

?
1
2
3
import os
GPU = '0,1,2'
os.environ['CUDA_VISIBLE_DEVICES'] =GPU

torch.cuda.is_available()查看cuda是否可用。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
if torch.cuda.is_available():
         torch.backends.cudnn.benchmark = True
        '''
        如果网络的输入数据维度或类型上变化不大,设置 torch.backends.cudnn.benchmark = true
        可以增加运行效率;
  如果网络的输入数据在每次 iteration 都变化的话,会导致 cnDNN 每次都会去寻找一遍最优配置,
  这样反而会降低运行效率。
  这下就清晰明了很多了。
  
        Benchmark模式会提升计算速度,但是由于计算中有随机性,每次网络前馈结果略有差异。
   torch.backends.cudnn.benchmark = True
     如果想要避免这种结果波动,设置:
  torch.backends.cudnn.deterministic = True
        '''

这句话也很常见,设置默认的device,优先gpu。

?
1
device = 'cuda' if torch.cuda.is_available() else 'cpu'

cpu挪到gpu

?
1
2
3
4
5
6
# 也可以是 device = torch.device('cuda:0')
device = torch.device('cuda')
a = torch.tensor([1,2,3])
b = a.to(device )
print(a)
print(b)

out:

tensor([1, 2, 3])

tensor([1, 2, 3], device='cuda:0')

判断变量是否基于GPU。

?
1
a.is_cuda

查看有几个可用GPU。

?
1
torch.cuda.device_count()

查看GPU算力

?
1
2
# 返回gpu最大和最小计算能力,是一个tuple
torch.cuda.get_device_capability()

设置默认哪一个GPU运算。

?
1
2
# 里面输入int类型的数字
torch.cuda.set_device()

抓取指定gpu的全名。

?
1
2
3
if torch.cuda.is_available():
    device = torch.device('cuda')
    print('Using GPU: ', torch.cuda.get_device_name(0))

out:

'GeForce GTX 1050'

2 直接在gpu创建

方法一:

?
1
2
a = torch.ones(3,4,device="cuda")
print(a)

out:

tensor([[1., 1., 1., 1.],
        [1., 1., 1., 1.],
        [1., 1., 1., 1.]], device='cuda:0')

方法二:

?
1
2
a = torch.cuda.FloatTensor(3, 4)
print(a)

out:

tensor([[-1., -1., -1., -1.],
        [-1., -1., -1., -1.],
        [-1., -1., -1., -1.]], device='cuda:0')

3 从cpu转移到gpu

方法一:tensor.to()

?
1
2
3
4
a = torch.ones(3,4)
b = a.to("cuda")
print(a)
print(b)

out:

tensor([[1., 1., 1., 1.],
        [1., 1., 1., 1.],
        [1., 1., 1., 1.]])
tensor([[1., 1., 1., 1.],
        [1., 1., 1., 1.],
        [1., 1., 1., 1.]], device='cuda:0')

注意:.to()不仅可以转移device,还可以修改数据类型,比如:a.to(torch.double)

方法二:tensor.cuda()

?
1
a = torch.tensor([1., 2.]).cuda()

方法三:tensor.type()

?
1
2
dtype = torch.cuda.FloatTensor
x = torch.rand(2,2).type(dtype)

方法四:torch.from_numpy(np_labels).cuda()

?
1
wm_labels = torch.from_numpy(np_labels).cuda()

4 在cuda中训练模型

在默认情况下,模型参数的优化(即训练)是在cpu上进行的,如果想要挪到GPU,得做如下修改:

?
1
2
3
4
5
6
7
8
import torch.nn as nn
#假设前面已经定义好了模型
#创建模型
Hidnet = UnetGenerator_mnist()
#把模型放入GPU
Hidnet = nn.DataParallel(Hidnet.cuda())
#查看模型参数
list(Hidnet.parameters())[0]

out:

Parameter containing:
tensor([[[[ 0.1315,  0.0562,  0.1186],
          [-0.1158,  0.1394, -0.0399],
          [ 0.1728,  0.1051, -0.1034]],

         [[ 0.1702, -0.1208, -0.1134],
          [-0.1449,  0.1912,  0.1727],
          [ 0.1562,  0.1601,  0.1055]],

         [[ 0.1031, -0.0062, -0.0068],
          [-0.0453,  0.1150,  0.0366],
          [ 0.0680, -0.1234, -0.0988]]]], device='cuda:0', requires_grad=True)

可以看到 device=‘cuda:0' 啦

pytorch 查看cuda 版本

由于pytorch的whl 安装包名字都一样,所以我们很难区分到底是基于cuda 的哪个版本。

有一条指令可以查看

?
1
2
import torch
print(torch.version.cuda)

以上为个人经验,希望能给大家一个参考,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/weixin_42468475/article/details/108758628

延伸 · 阅读

精彩推荐