pytorch序列通过时间序列的编码器解码器进行序列建模

时间:2018-08-22 15:06:43

标签: python pytorch rnn

我一直遵循PyTorch网站上的seq2seq建模教程,以下是我正在使用的部分代码:

class EncoderRNN(nn.Module):
    def __init__(self, input_size, hidden_size):
        super(EncoderRNN, self).__init__()
        self.hidden_size = hidden_size

        self.embedding = nn.Embedding(input_size, hidden_size)
        self.gru = nn.GRU(hidden_size, hidden_size)

    def forward(self, input, hidden):
        embedded = self.embedding(input).view(1, 1, -1)
        output = embedded
        output, hidden = self.gru(output, hidden)
        return output, hidden

    def initHidden(self):
        return torch.zeros(1, 1, self.hidden_size, device=device)

class AttnDecoderRNN(nn.Module):
    def __init__(self, hidden_size, output_size, dropout_p=0.1, max_length=MAX_LENGTH):
        super(AttnDecoderRNN, self).__init__()
        self.hidden_size = hidden_size
        self.output_size = output_size
        self.dropout_p = dropout_p
        self.max_length = max_length

        self.embedding = nn.Embedding(self.output_size, self.hidden_size)
        self.attn = nn.Linear(self.hidden_size * 2, self.max_length)
        self.attn_combine = nn.Linear(self.hidden_size * 2, self.hidden_size)
        self.dropout = nn.Dropout(self.dropout_p)
        self.gru = nn.GRU(self.hidden_size, self.hidden_size)
        self.out = nn.Linear(self.hidden_size, self.output_size)

    def forward(self, input, hidden, encoder_outputs):
        embedded = self.embedding(input).view(1, 1, -1)
        embedded = self.dropout(embedded)

        attn_weights = F.softmax(
            self.attn(torch.cat((embedded[0], hidden[0]), 1)), dim=1)
        attn_applied = torch.bmm(attn_weights.unsqueeze(0),
                                 encoder_outputs.unsqueeze(0))

        output = torch.cat((embedded[0], attn_applied[0]), 1)
        output = self.attn_combine(output).unsqueeze(0)

        output = F.relu(output)
        output, hidden = self.gru(output, hidden)

        output = F.log_softmax(self.out(output[0]), dim=1)
        return output, hidden, attn_weights

    def initHidden(self):
        return torch.zeros(1, 1, self.hidden_size, device=device)

定义了编码器和解码器后,训练将按以下步骤进行:

def train(input_tensor, target_tensor, encoder, decoder, encoder_optimizer, decoder_optimizer, criterion, max_length=MAX_LENGTH):
    encoder_hidden = encoder.initHidden()

    encoder_optimizer.zero_grad()
    decoder_optimizer.zero_grad()

    input_length = input_tensor.size(0)
    target_length = target_tensor.size(0)

    encoder_outputs = torch.zeros(max_length, encoder.hidden_size, device=device)

    loss = 0
    for ei in range(input_length):
        encoder_output, encoder_hidden = encoder(input_tensor[ei], encoder_hidden)
        encoder_outputs[ei] = encoder_output[0, 0]

    decoder_input = torch.tensor([[SOS_token]], device=device)

    decoder_hidden = encoder_hidden

    for di in range(target_length):
        decoder_output, decoder_hidden, decoder_attention = decoder(decoder_input, decoder_hidden, encoder_outputs)

        loss += criterion(decoder_output, target_tensor[di])
        decoder_input = target_tensor[di]  # Teacher forcing

    loss.backward()

    encoder_optimizer.step()
    decoder_optimizer.step()

    return loss.item() / target_length

def trainIters(encoder, decoder, n_iters, print_every=1000, plot_every=100, learning_rate=0.01):
    encoder_optimizer = optim.SGD(encoder.parameters(), lr=learning_rate)
    decoder_optimizer = optim.SGD(decoder.parameters(), lr=learning_rate)

    criterion = nn.NLLLoss()

    for iter in range(1, n_iters + 1):
        input_tensor = torch.from_numpy(np.array([np.random.randint(2) for i in range(10)]).reshape(10,1))
        target_tensor = torch.from_numpy(np.array([np.random.randint(2) for i in range(10)]).reshape(10,1))

        loss = train(input_tensor, target_tensor, encoder, decoder, encoder_optimizer, decoder_optimizer, criterion)
        print("loss===>",loss)


hidden_size = 256
encoder1 = EncoderRNN(50, hidden_size).to(device)
attn_decoder1 = AttnDecoderRNN(hidden_size, 50).to(device)

trainIters(encoder1, attn_decoder1, 10000, print_every=5000)

此代码适用于句子翻译。它选择一个单词,生成其嵌入,然后通过编码器中的GRU获得隐藏状态。然后将此隐藏状态逐字传递给解码器,并且对于每个字,都有一个概率向量。然后,在给定目标标签和概率向量的情况下,计算负对数似然损失。

我的问题基本上是如何使它适应时间序列预测模型?我有一个时间序列数据,分为两个部分,序列1和2。我希望预测序列2。很明显,我需要MSE损失而不是分类损失。另外,我认为没有必要为时间序列中的特定值生成嵌入,并且我需要将所有内容从S形更改为Relu。

现在,我将一些示例数组作为输入和目标。如果输入或目标的值可能大于50,则该代码将给出错误。由于编写方式是最多可以有50个类。但是对于回归设置,可以有连续范围。

请问有PyTorch经验的人请解释如何修改它吗?

0 个答案:

没有答案