简体   繁体   English

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

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

I am new to pytorch and I am trying to run a github model I found and test it.我是 pytorch 的新手,我正在尝试运行我找到的 github 模型并对其进行测试。 So the author's provided the model and the loss function.所以作者提供了模型和损失函数。

like this:像这样:

#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)

Dataloading数据加载

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

    batch = next(iter(train_loader))

    data, label1, label2 = batch

    inputs= data

Let's say I want to train this model for 15 epochs.假设我想将这个模型训练 15 个 epochs。 So this is what I have so far: I am trying to set the optimizer and training, but I am not sure how to tie the custom loss and data loading to the model and set the 15 epoch training correctly.所以这就是我到目前为止所拥有的:我正在尝试设置优化器和训练,但我不确定如何将自定义损失和数据加载与模型联系起来并正确设置 15 epoch 训练。

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

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

Any suggestions?有什么建议?

I assumed BVP_label is label1 of train_loader我假设 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 training steps are as belows. PyTorch 训练步骤如下。

  • Create DataLoader创建数据加载器
  • Initialize model and optimizer初始化模型和优化器
  • Create a device object and move model to the device创建设备对象并将模型移动到设备

in the train loop在火车循环中

  • select a mini-batch of data选择小批量数据
  • use the model to make predictions使用模型进行预测
  • calculate the loss计算损失
  • loss.backward() updates the gradients of the model loss.backward() 更新模型的梯度
  • update the parameters using optimizer使用优化器更新参数

As you may know you can also check PyTorch Tutorials.您可能知道,您还可以查看 PyTorch 教程。

Learning PyTorch with Examples 用例子学习 PyTorch

What is torch.nn really?什么是torch.nn?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在自定义数据上训练 Pytorch model - How to train Pytorch model on custom data PyTorch 中的神经网络、损失和优化器是如何连接的? - How are neural networks, loss and optimizer connected in PyTorch? 我在 Pytorch 中的自定义损失函数无法训练 - My custom loss function in Pytorch does not train 如何在 Keras 中使用自定义损失函数加快模型的训练速度? - How can I speed the training up for a model with a custom loss function in Keras? 如何使用 pytorch 将数据集拆分为自定义训练集和自定义验证集? - How to split a dataset into a custom training set and a custom validation set with pytorch? Pytorch:如何训练具有两个损失函数的网络? - Pytorch: How to train a network with two loss functions? 我应该设置 model.eval 以获得 Pytorch 中的当前训练损失吗? - Should I set model.eval for getting the current training loss in Pytorch? 如何用我自己的训练集训练文本蕴涵 model? - How to train a textual entailment model with my own training set? 训练预训练的自定义 model 具有不同的损失 function - Train pretrained custom model with different loss function 在 pytorch 中加速训练深度学习 model - Speed up training deep learning model in pytorch
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM