PyTorch RNN-BiLSTM情感分析低准确性

问题描述 投票:0回答:1

我正在将PyTorch与一组训练有素的电影评论一起使用,每个评论都标记为肯定或否定。每个评论都被截断或填充为60个单词,并且我的批处理大小为32。此60x32张量被馈送到嵌入层,嵌入层暗淡为100,从而得到60x32x100张量。然后,我使用每个评论的未填充长度来打包嵌入输出,并使用hidden dim = 256将其输入到BiLSTM层。

然后将其填充回去,应用转换(以尝试获取向前和向后方向的最后一个隐藏状态),并将转换输入到512x1的线性层。这是我的模块,我通过未在此处显示的S形传递最终输出

class RNN(nn.Module):
    def __init__(self, vocab_size, embedding_dim, hidden_dim, output_dim, n_layers, 
                 bidirectional, dropout, pad_idx):

        super().__init__()
        self.el = nn.Embedding(vocab_size, embedding_dim)
        print('vocab size is ', vocab_size)
        print('embedding dim is ', embedding_dim)
        self.hidden_dim = hidden_dim
        self.num_layers = n_layers # 2
        self.lstm = nn.LSTM(input_size=embedding_dim, hidden_size=hidden_dim, num_layers=n_layers, dropout=dropout, bidirectional=bidirectional)
        # Have an output layer for outputting a single output value
        self.linear = nn.Linear(2*hidden_dim, output_dim)

    def init_hidden(self):
        return (torch.zeros(self.n_layers*2, 32, self.hidden_dim).to(device), 
                torch.zeros(self.n_layers*2, 32, self.hidden_dim).to(device))

    def forward(self, text, text_lengths):
        print('input text size ', text.size())
        embedded = self.el(text)
        print('embedded size ', embedded.size())
        packed_seq = torch.nn.utils.rnn.pack_padded_sequence(embedded, lengths=text_lengths, enforce_sorted=False)
        packed_out, (ht, ct) = self.lstm(packed_seq, None)
        out_rnn, out_lengths = torch.nn.utils.rnn.pad_packed_sequence(packed_out)
        print('padded lstm out ', out_rnn.size())        
        #out_rnn = out_rnn[-1] #this works
        #out_rnn = torch.cat((out_rnn[-1, :, :self.hidden_dim], out_rnn[0, :, self.hidden_dim:]), dim=1) # this works
        out_rnn = torch.cat((ht[-1], ht[0]), dim=1) #this works
        #out_rnn = out_rnn[:, -1, :] #doesn't work maybe should
        print('attempt to get last hidden ', out_rnn.size())
        linear_out = self.linear(out_rnn)
        print('after linear ', linear_out.size())
        return linear_out

我尝试了3种不同的变换,以使线性层的尺寸正确无误

out_rnn = out_rnn[-1] #this works
out_rnn = torch.cat((out_rnn[-1, :, :self.hidden_dim], out_rnn[0, :, self.hidden_dim:]), dim=1) # this works
out_rnn = torch.cat((ht[-1], ht[0]), dim=1) #this works

这些都产生这样的输出

输入文本大小torch.Size([60,32])

嵌入式大小torch.Size([60,32,100])

填充了lstm out torch.Size([36,32,512])

试图获取最后一个隐藏的割炬.Size([32,512])

在线性割炬之后。Size([32,1])

我希望填充的lstm输出为[60, 32, 512],但在第一维中始终小于60。

我正在使用optim.SGDnn.BCEWithLogitsLoss()训练10个时期。我的训练准确度始终在52%左右,而测试准确度始终在50%左右,因此该模型的效果并不比随机猜测好。我确定我的数据在tochtext.data.Dataset中已正确处理。我是否将张量错误地转发?

[我曾尝试在我的lstm,batch_first=True函数和packed_seq函数中使用pad_packed_seq,但在馈入线性层之前破坏了我的转换。

更新我添加了init_hidden方法,并尝试不使用pack / pad序列方法,但仍然得到相同的结果

python pytorch lstm recurrent-neural-network sentiment-analysis
1个回答
0
投票

我将优化器从SGD更改为Adam,并将层数从2更改为1,并且我的模型开始学习,准确度> 75%

© www.soinside.com 2019 - 2024. All rights reserved.