首页 > 解决方案 > 实现多对多回归任务

问题描述

抱歉,如果我没有清楚地表达我的问题,英语不是我的第一语言

问题

简短的介绍:

我想训练一个将输入x(形状为[n_sample, timestamp, feature])映射到输出y(形状完全相同)的模型。这就像映射 2 个空间

更长的版本:

我有 2 个 floatndarray形状[n_sample, timestamp, feature],代表音频文件MFCC的特征。n_sample这 2 个ndarray是 2 个说话者在同一个语料库中的语音,由 DTW 对齐。让我们将这 2 个数组命名xy. 我想训练一个模型,它预测y[k]给定x[k]的 . 这就像从空间x到空间的映射y,输出必须与输入的形状完全相同

我试过的

这是时间序列问题,所以我决定使用RNN方法。这是我在 PyTorch 中的代码(我在代码中添加了注释。为简单起见,我删除了平均损失的计算)。请注意,我尝试了许多学习率选项,行为仍然相同

类定义

class Net(nn.Module):
    def __init__(self, in_size, hidden_size, out_size, nb_lstm_layers):
        super().__init__()
        self.in_size = in_size
        self.hidden_size = hidden_size
        self.out_size = out_size
        self.nb_lstm_layers = nb_lstm_layers

        # self.fc1 = nn.Linear()
        self.lstm = nn.LSTM(input_size=self.in_size, hidden_size=self.hidden_size, num_layers=self.nb_lstm_layers, batch_first=True, bias=True)
        # self.fc = nn.Linear(self.hidden_size, self.out_size)
        self.fc1 = nn.Linear(self.hidden_size, 128)
        self.fc2 = nn.Linear(128, 128)
        self.fc3 = nn.Linear(128, self.out_size)

    def forward(self, x, h_state):
        out, h_state = self.lstm(x, h_state)
        output_fc = []

        for frame in out:
            output_fc.append(self.fc3(torch.tanh(self.fc1(frame)))) # I added fully connected layer to each frame, to make an output with same shape as input

        return torch.stack(output_fc), h_state

    def hidden_init(self):
        if use_cuda:
            h_state = torch.stack([torch.zeros(nb_lstm_layers, batch_size, 20) for _ in range(2)]).cuda()
        else:
            h_state = torch.stack([torch.zeros(nb_lstm_layers, batch_size, 20) for _ in range(2)])

        return h_state

训练步骤:

net = Net(20, 20, 20, nb_lstm_layers)
optimizer = optim.Adam(net.parameters(), lr=0.0001, weight_decay=0.0001)
criterion = nn.MSELoss()

for epoch in range(nb_epoch):
    count = 0
    loss_sum = 0

    batch_x = None
    for i in (range(len(data))): 
    # data is my entire data, which contain A and B i specify above.
        temp_x = torch.tensor(data[i][0])
        temp_y = torch.tensor(data[i][1])

        for ii in range(0, data[i][0].shape[0] - nb_frame_in_batch*2 + 1): # Create batches 
            batch_x, batch_y = get_batches(temp_x, temp_y, ii, batch_size, nb_frame_in_batch)  
            # this will return 2 tensor of shape (batch_size, nb_frame_in_batch, 20), 
            # with `batch_size` is the number of sample each time I feed to the net, 
            # nb_frame_in_batch is the number of frame in each sample
            optimizer.zero_grad()

            h_state = net.hidden_init()

            prediction, h_state = net(batch_x.float(), h_state)
            loss = criterion(prediction.float(), batch_y.float())

            h_state = (h_state[0].detach(), h_state[1].detach())

            loss.backward()
            optimizer.step()

问题是,损失似乎没有减少而是波动很大,没有明确的行为

在此处输入图像描述

请帮我。任何建议将不胜感激。如果有人可以检查我的代码并提供一些评论,那就太好了。
提前致谢!

标签: pythondeep-learninglstmpytorch

解决方案


网络似乎没有从您的数据中学到任何东西,因此损失波动(因为权重仅取决于随机初始化)。您可以尝试以下方法:

  • 尝试规范化数据(这个建议非常广泛,但我不能给你更多细节,因为我没有你的数据,但将它规范化到特定范围,如 [0, 1],或平均值和标准价值值得尝试)
  • pytorch 中 LSTM 的一个非常典型的问题是它的输入维度与其他类型的神经网络完全不同。您必须向网络输入一个形状为 (seq_len、batch、input_size) 的张量。你应该去这里,LSTM部分以获得更好的细节
  • 还有一件事:尝试调整你的超参数。与 FC 或 CNN 相比(根据我的经验),LSTM 更难训练。

告诉我你是否有改善。调试神经网络总是很困难并​​且充满潜在的编码错误


推荐阅读