【问题标题】:Train model in Pytorch with custom loss how to set up optimizer and run training?在 Pytorch 中使用自定义损失训练模型如何设置优化器并运行训练?
【发布时间】:2021-02-23 00:04:24
【问题描述】:

我是 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 个 epoch。 所以这就是我到目前为止所拥有的: 我正在尝试设置优化器和训练,但我不确定如何将自定义损失和数据加载绑定到模型并正确设置 15 epoch 训练。

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

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

有什么建议吗?

【问题讨论】:

    标签: python pytorch loss inference custom-training


    【解决方案1】:

    我假设 BVP_label 是 train_loader 的 label1

    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 训练步骤如下。

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

    在火车循环中

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

    您可能知道,您也可以查看 PyTorch 教程。

    Learning PyTorch with Examples

    What is torch.nn really?

    【讨论】:

      猜你喜欢
      • 2019-06-03
      • 2021-03-26
      • 1970-01-01
      • 2019-11-12
      • 2021-01-12
      • 2020-08-30
      • 2021-09-11
      • 2021-05-29
      • 1970-01-01
      相关资源
      最近更新 更多