Nlp 如何在pytorch中获得双向2层GRU的最终隐藏状态

Nlp 如何在pytorch中获得双向2层GRU的最终隐藏状态,nlp,pytorch,lstm,gated-recurrent-unit,Nlp,Pytorch,Lstm,Gated Recurrent Unit,我正在努力理解如何获取隐藏层并连接它们 我使用以下代码作为示例: class classifier(nn.Module): #define all the layers used in model def __init__(self, vocab_size, embedding_dim, hidden_dim, output_dim, n_layers, bidirectional, dropout): #Constructor super()._

我正在努力理解如何获取隐藏层并连接它们

我使用以下代码作为示例:

class classifier(nn.Module):

#define all the layers used in model
def __init__(self, vocab_size, embedding_dim, hidden_dim, output_dim, n_layers, 
             bidirectional, dropout):

    #Constructor
    super().__init__()

    self.batch = BATCH_SIZE
    self.hidden = hidden_dim
    self.layers = n_layers
    if(bidirectional):
        self.directions = 2
    else:
        self.directions = 1

    #embedding layer
    self.embedding = nn.Embedding(vocab_size, embedding_dim)

    #lstm layer
    self.gru = nn.GRU(embedding_dim, 
                       hidden_dim, 
                       num_layers=n_layers, 
                       bidirectional=bidirectional, 
                       dropout=dropout,
                       batch_first=True)

    #dense layer
    self.fc = nn.Linear(hidden_dim * 2, output_dim)

    #activation function
    self.act = nn.Sigmoid()

def forward(self, text, text_lengths):

    #text = [batch size,sent_length]
    embedded = self.embedding(text)
    #embedded = [batch size, sent_len, emb dim]

    #packed sequence
    packed_embedded = nn.utils.rnn.pack_padded_sequence(embedded, text_lengths,batch_first=True)

    packed_output, (hidden, cell) = self.lstm(packed_embedded)
    #hidden = [batch size, num layers * num directions,hid dim]
    #cell = [batch size, num layers * num directions,hid dim]

    #concat the final forward and backward hidden state
    hidden = torch.cat((hidden[-2,:,:], hidden[-1,:,:]), dim = 1)

    #hidden = [batch size, hid dim * num directions]
    dense_outputs=self.fc(hidden)

    #Final activation function
    outputs=self.act(dense_outputs)

    return outputs
hidden=torch.cat((hidden[-2,:,:,:],hidden[-1,:,:],dim=1)
我没有得到它

据我所知,我这样做是行不通的

hidden2 = hidden.view(batch_size,self.layers,self.directions,self.hidden)  
hidden2 = torch.cat((hidden2[:,:,0,:],hidden2[:,:,1,:]),dim=1)
dense_outputs=self.fc(hidden2)

谁能解释一下吗。我浏览了PyTorch文档,但没有得到。双向GRU的隐藏输出的形状[0]是2。您只需在dim=1上显示两个隐藏输出:

hid_enc = torch.cat([hid_enc[0,:, :], hid_enc[1,:,:]], dim=1).unsqueeze(0)

作为使用-1和-2作为索引的解释,正如您在python列表中所知道的,索引-1中的对象是列表的最后一个对象(张量列表中的第二个对象),索引-2指的是最后一个对象之前的对象(本例中的第一个对象)。因此,您不理解的代码相当于我的答案中的代码。双向GRU的隐藏输出的形状[0]为2。您只需在dim=1上显示两个隐藏输出:

hid_enc = torch.cat([hid_enc[0,:, :], hid_enc[1,:,:]], dim=1).unsqueeze(0)

作为使用-1和-2作为索引的解释,正如您在python列表中所知道的,索引-1中的对象是列表的最后一个对象(张量列表中的第二个对象),索引-2指的是最后一个对象之前的对象(本例中的第一个对象)。因此,您不理解的代码相当于我回答中的代码

据我所知,双向GRU的隐藏输出形状[0]为4,因为有两层和两个方向。据我所知,双向GRU的隐藏输出形状[0]为4,因为有两层和两个方向。