我是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历元训练。
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训练步骤如下。
- 创建数据加载器
- 初始化模型和优化器
- 创建设备对象并将模型移动到设备
在列车环路中
- 选择一个小批量数据
- 使用模型进行预测
- 计算损失
- loss.backward((更新模型的梯度
- 使用优化器更新参数
如您所知,您也可以查看PyTorch教程。
用实例学习PyTorch
torch.nn到底是什么?