7554ff1818a1876be9ce5a5f8180f91b.png

1. nn.Module.cuda() 和 Tensor.cuda() 的作用效果差异

对于nn.Module:

model = model.cuda() 
model.cuda() 

上面两句能够达到一样的效果,即对model自身进行的内存迁移。

对于Tensor:

和nn.Module不同,调用tensor.cuda()只是返回这个tensor对象在GPU内存上的拷贝,而不会对自身进行改变。因此必须对tensor进行重新赋值,即tensor=tensor.cuda().

model = create_a_model()
tensor = torch.zeros([2,3,10,10])
model.cuda()
tensor.cuda()
model(tensor)    # 会报错
tensor = tensor.cuda()
model(tensor)    # 正常运行

2、PyTorch 编写不限制设备的代码

# torch.device object used throughout this script
device = torch.device("cuda" if use_cuda else "cpu")
model = MyRNN().to(device)

# train
total_loss= 0
for input, target in train_loader:
    input, target = input.to(device), target.to(device)
    hidden = input.new_zeros(*h_shape)       # has the same device & dtype as `input`
    ...                                                               # get loss and optimize
    total_loss += loss.item()

# test
with torch.no_grad():                                    # operations inside don't track history
    for input, targetin test_loader:
        ...

3、torch.Tensor.detach()的使用

假设有模型A和模型B,我们需要将A的输出作为B的输入,但训练时我们只训练模型B. 那么可以这样做:

input_B = output_A.detach()

它可以使两个计算图的梯度传递断开,从而实现我们所需的功能。

4、ERROR: Unexpected bus error encountered in worker. This might be caused by insufficient shared memory (shm)

出现这个错误的情况是,在服务器上的docker中运行训练代码时,batch size设置得过大,shared memory不够(因为docker限制了shm).解决方法是,将Dataloader的num_workers设置为0.

5、pytorch的可重复性问题

https://blog.csdn.net/hyk_1996/article/details/84307108​blog.csdn.net

6、多GPU的处理机制

使用多GPU时,应该记住pytorch的处理逻辑是:

1)在各个GPU上初始化模型。

2)前向传播时,把batch分配到各个GPU上进行计算。

3)得到的输出在主GPU上进行汇总,计算loss并反向传播,更新主GPU上的权值。

4)把主GPU上的模型复制到其它GPU上。

7、训练时损失出现nan的问题 大坑!

注意!像nan或者inf这样的数值不能使用 == 或者 is 来判断!为了安全起见统一使用 math.isnan() 或者 numpy.isnan() 吧

可能导致梯度出现nan的三个原因:

1)梯度爆炸。也就是说梯度数值超出范围变成nan. 通常可以调小学习率、加BN层或者做梯度裁剪来试试看有没有解决。

2)损失函数或者网络设计。比方说,出现了除0,或者出现一些边界情况导致函数不可导,比方说log(0)、sqrt(0).

3)脏数据。可以事先对输入数据进行判断看看是否存在nan.

更多推荐