Pytorch学习(九)Pytorch中CPU和GPU的Tensor转换,Tensor和ndarray的转换及.cuda(non_blocking=True)的作用

1. 设置训练模型的GPU设备的方式
device = torch.device("cuda:1" )
model = model.to(device)
2. CPU和GPU上Tensor的比较

该部分参考自来自达摩院大神的讲解
PyTorch中的数据类型为Tensor,Tensor与Numpy中的ndarray类似,同样可以用于标量,向量,矩阵乃至更高维度上面的计算。PyTorch中的tensor又包括CPU上的数据类型和GPU上的数据类型,一般GPU上的Tensor是CPU上的Tensor加cuda()函数得到。系统默认的torch.Tensor是torch.FloatTensor类型。例如data = torch.Tensor(2,3)是一个2*3的张量,类型为FloatTensor; data.cuda()就将其转换为GPU的张量类型,torch.cuda.FloatTensor类型。

(1) Tensor和numpy.ndarray之间可以相互转换:

  • Numpy转Tensor: torch.from_numpy(numpy矩阵)
  • Tensor转Numpy: Torch矩阵.numpy()
    举例如下:
import numpy as np
import torch

a_tensor = torch.randint(0, 10, (2,3))   # Tensor声明
print(type(a_tensor))
a_numpy = a_tensor.numpy()        # Tensor转Numpy
print(type(a_numpy))

b_numpy = np.array([[1,2,3], [4,5,6]])     # Numpy声明
print(type(b_numpy))
b_tensor = torch.from_numpy(b_numpy)       # Numpy转Tensor
print(type(b_tensor))

# 结果打印如下:
<class 'torch.Tensor'>
<class 'numpy.ndarray'>
<class 'numpy.ndarray'>
<class 'torch.Tensor'>

numpy与Tensor最大的区别就是在对GPU的支持上。Tensor只需要调用cuda()函数就可以将其转化为能在GPU上运行的类型。
(2) CPU和GPU的Tensor之间的转换

  • CPU转GPU: data.cuda()
  • GPU转CPU: data.cpu()
3. Pytorch中.cuda(non_blocking=True)的作用

.cuda()是为了将模型放在GPU上进行训练。

  • 为何要设置参数non_blocking=True呢?
    non_blocking默认值为False, 通常我们会在加载数据时,将DataLoader的参数pin_memory设置为True, DataLoader中参数pin_memory的作用是:将生成的Tensor数据存放在哪里,值为True时,意味着生成的Tensor数据存放在锁页内存中,这样内存中的Tensor转义到GPU的显存会更快。
    主机中的内存,有两种存在方式,一是锁页,二是不锁页,锁页内存存放的内容在任何情况下都不会与主机的虚拟内存进行交换(注:虚拟内存就是硬盘),而不锁页内存在主机内存不足时,数据会存放在虚拟内存中。显卡中的显存全部是锁页内存,当计算机的内存充足的时候,可以设置pin_memory=True。当系统卡住,或者交换内存使用过多的时候,设置pin_memory=False。(参考:链接
    如果pin_memory=True的话,将数据放入GPU的时候,也应该把non_blocking打开,这样就只把数据放入GPU而不取出,访问时间会大大减少。
  • 32
    点赞
  • 79
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
pytorch代码如下:class LDAMLoss(nn.Module): def init(self, cls_num_list, max_m=0.5, weight=None, s=30): super(LDAMLoss, self).init() m_list = 1.0 / np.sqrt(np.sqrt(cls_num_list)) m_list = m_list * (max_m / np.max(m_list)) m_list = torch.cuda.FloatTensor(m_list) self.m_list = m_list assert s > 0 self.s = s if weight is not None: weight = torch.FloatTensor(weight).cuda() self.weight = weight self.cls_num_list = cls_num_list def forward(self, x, target): index = torch.zeros_like(x, dtype=torch.uint8) index_float = index.type(torch.cuda.FloatTensor) batch_m = torch.matmul(self.m_list[None, :], index_float.transpose(1,0)) # 0,1 batch_m = batch_m.view((-1, 1)) # size=(batch_size, 1) (-1,1) x_m = x - batch_m output = torch.where(index, x_m, x) if self.weight is not None: output = output * self.weight[None, :] logit = output * self.s return F.cross_entropy(logit, target, weight=self.weight) classes=7, cls_num_list = np.zeros(classes) for , label in train_loader.dataset: cls_num_list[label] += 1 criterion_train = LDAMLoss(cls_num_list=cls_num_list, max_m=0.5, s=30) criterion_val = LDAMLoss(cls_num_list=cls_num_list, max_m=0.5, s=30) for batch_idx, (data, target) in enumerate(train_loader): data, target = data.to(device, non_blocking=True), Variable(target).to(device,non_blocking=True) # 3、将数据输入mixup_fn生成mixup数据 samples, targets = mixup_fn(data, target) targets = torch.tensor(targets).to(torch.long) # 4、将上一步生成的数据输入model,输出预测结果,再计算loss output = model(samples) # 5、梯度清零(将loss关于weight的导数变成0) optimizer.zero_grad() # 6、若使用混合精度 if use_amp: with torch.cuda.amp.autocast(): # 开启混合精度 loss = torch.nan_to_num(criterion_train(output, targets)) # 计算loss scaler.scale(loss).backward() # 梯度放大 torch.nn.utils.clip_grad_norm(model.parameters(), CLIP_GRAD) # 梯度裁剪,防止梯度爆炸 scaler.step(optimizer) # 更新下一次迭代的scaler scaler.update() 报错:File "/home/adminis/hpy/ConvNextV2_Demo/models/losses.py", line 53, in forward return F.cross_entropy(logit, target, weight=self.weight) File "/home/adminis/anaconda3/envs/wln/lib/python3.9/site-packages/torch/nn/functional.py", line 2824, in cross_entropy return torch._C._nn.cross_entropy_loss(input, target, weight, _Reduction.get_enum(reduction), ignore_index) RuntimeError: multi-target not supported at /pytorch/aten/src/THCUNN/generic/ClassNLLCriterion.cu:15
05-29
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值