在pytorch中训练多输出回归模型

2024-09-27 17:45:30 发布

您现在位置:Python中文网/ 问答频道 /正文

我想要一个具有3个回归输出的模型,例如下面的虚拟示例:

import torch

class MultiOutputRegression(torch.nn.Module):

    def __init__(self):
        super(MultiOutputRegression, self).__init__()
        self.linear1 = torch.nn.Linear(1, 10)
        self.linear2 = torch.nn.Linear(10, 10)
        self.linear3 = torch.nn.Linear(3, 3)

    def forward(self, x):
        x = self.linear1(x)
        x = self.linear2(x)
        x = self.linear3(x)
        return x

假设我想训练它执行一个虚拟任务,例如,给定输入x返回[x, 2x, 3x]

定义标准和损失后,我们可以使用以下数据对其进行培训:

for i in range(1, 100, 2):
    x_train = torch.tensor([i, i + 1]).reshape(2, 1).float()
    y_train = torch.tensor([[j, 2 * j] for j in x_train]).float()
    y_pred = model(x_train)
    # todo: perform training iteration 

第一次迭代的样本数据为:

x_train
tensor([[1.],
        [2.]])
y_train
tensor([[1., 2., 3.],
        [2., 4., 6.]])

我如何定义一个合适的损失和标准来训练神经网络? 谢谢你的帮助


Tags: self标准定义initdeftrainnntorch

热门问题