使用自定义损失在 Pytorch 中训练模型如何设置优化器和 运行 训练?

Train model in Pytorch with custom loss how to set up optimizer and run training?

我是 pytorch 的新手,我正在尝试 运行 我找到的 github 模型并对其进行测试。所以作者提供了模型和损失函数。

像这样:

#1. Inference the model
model = PhysNet_padding_Encoder_Decoder_MAX(frames=128)
rPPG, x_visual, x_visual3232, x_visual1616 = model(inputs)

#2. Normalized the Predicted rPPG signal and GroundTruth BVP signal
rPPG = (rPPG-torch.mean(rPPG)) /torch.std(rPPG)     # normalize
BVP_label = (BVP_label-torch.mean(BVP_label)) /torch.std(BVP_label)     # normalize

#3. Calculate the loss
loss_ecg = Neg_Pearson(rPPG, BVP_label)

数据加载

    train_loader = torch.utils.data.DataLoader(train_set, batch_size = 20, shuffle = True)

    batch = next(iter(train_loader))

    data, label1, label2 = batch

    inputs= data

假设我想训练这个模型 15 个时期。 所以这就是我到目前为止所拥有的: 我正在尝试设置优化器和训练,但我不确定如何将自定义损失和数据加载与模型联系起来并正确设置 15 个 epoch 训练。

optimizer = optim.SGD(model.parameters(), lr=0.001, momentum=0.9)

for epoch in range(15):
  ....

有什么建议吗?

我假设 BVP_label 是 train_loader

的标签 1
train_loader = torch.utils.data.DataLoader(train_set, batch_size = 20, shuffle = True)

# Using GPU
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")

model = PhysNet_padding_Encoder_Decoder_MAX(frames=128)
model.to(device)

optimizer = optim.SGD(model.parameters(), lr=0.001, momentum=0.9)

for epoch in range(15):
    model.train()
    for inputs, label1, label2 in train_loader:
        rPPG, x_visual, x_visual3232, x_visual1616 = model(inputs)
        BVP_label = label1 # assumed BVP_label is label1

        rPPG = (rPPG-torch.mean(rPPG)) /torch.std(rPPG)
        BVP_label = (BVP_label-torch.mean(BVP_label)) /torch.std(BVP_label)
        
        loss_ecg = Neg_Pearson(rPPG, BVP_label)
        
        optimizer.zero_grad()
        loss_ecg.backward()
        optimizer.step()

PyTorch训练步骤如下

  • 创建数据加载器
  • 初始化模型和优化器
  • 创建设备对象并将模型移动到设备

在火车环路中

  • select 一小批数据
  • 使用模型进行预测
  • 计算损失
  • loss.backward() 更新模型的梯度
  • 使用优化器更新参数

如您所知,您还可以查看 PyTorch 教程。

Learning PyTorch with Examples

What is torch.nn really?