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

use*_*621 8 python regression loss neural-network

我想要一个具有 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
Run Code Online (Sandbox Code Playgroud)

假设我想训练它执行虚拟任务,例如,给定输入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 
Run Code Online (Sandbox Code Playgroud)

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

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

Run Code Online (Sandbox Code Playgroud)

如何定义合适的损失和标准来训练神经网络?

小智 7

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(10, 3)

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

model = MultiOutputRegression()

criterion = nn.MSELoss()
optimizer = optim.Adam(model.parameters())

for epoch in range(5):
    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, 3 * j] for j in x_train]).float()
        
        optimizer.zero_grad()
        y_pred = model(x_train)
        loss = criterion(y_pred, y_train)
        loss.backward()
        optimizer.step()
        
        print(loss.detach().numpy())
Run Code Online (Sandbox Code Playgroud)