这篇教程pytorch 如何用cuda处理数据写得很实用,希望能帮到您。 1 设置GPU的一些操作设置在os端哪些GPU可见,如果不可见,那肯定是不能够调用的~ import osGPU = '0,1,2'os.environ['CUDA_VISIBLE_DEVICES'] =GPU torch.cuda.is_available()查看cuda是否可用。
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。 device = 'cuda' if torch.cuda.is_available() else 'cpu' cpu挪到gpu # 也可以是 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。 查看有几个可用GPU。 torch.cuda.device_count() 查看GPU算力 # 返回gpu最大和最小计算能力,是一个tupletorch.cuda.get_device_capability() 设置默认哪一个GPU运算。 # 里面输入int类型的数字torch.cuda.set_device() 抓取指定gpu的全名。 if torch.cuda.is_available(): device = torch.device('cuda') print('Using GPU: ', torch.cuda.get_device_name(0)) out: 'GeForce GTX 1050'
2 直接在gpu创建方法一: 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')
方法二: 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() 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() a = torch.tensor([1., 2.]).cuda() 方法三:tensor.type() dtype = torch.cuda.FloatTensorx = torch.rand(2,2).type(dtype) 方法四:torch.from_numpy(np_labels).cuda() wm_labels = torch.from_numpy(np_labels).cuda() 4 在cuda中训练模型在默认情况下,模型参数的优化(即训练)是在cpu上进行的,如果想要挪到GPU,得做如下修改: import torch.nn as nn#假设前面已经定义好了模型#创建模型Hidnet = UnetGenerator_mnist()#把模型放入GPUHidnet = 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 的哪个版本。 有一条指令可以查看
import torchprint(torch.version.cuda) 以上为个人经验,希望能给大家一个参考,也希望大家多多支持51zixue.net。 Python函数装饰器的使用教程 Python函数参数和注解的使用 |