Search code examples
python-3.xindexingpytorchrecurrent-neural-network

What is the last line of this Rnn function means?


I am here to ask a noob question.

class RNN(nn.Module):
    def __init__(self, input_size, hidden_size, num_layers, num_classes):
        super(RNN, self).__init__()
        self.hidden_size = hidden_size
        self.num_layers = num_layers
        self.rnn = nn.RNN(input_size, hidden_size, num_layers, batch_first=True)
        self.fc = nn.Linear(hidden_size*2, num_classes)
    
    def forward(self, x):
        h0 = torch.zeros(self.num_layers*2, x.size(0), self.hidden_size).to(device)
        out, _ = self.rnn(x, h0)  # out: tensor of shape (batch_size, seq_length, hidden_size)
        out = self.fc(out[:, -1, :])
        return out

Here what does out = self.fc(out[:, -1, :]) means? And also why there is a "_" in out, _ = self.rnn(x, h0) ?


Solution

  • The line out = self.fc(out[:, -1, :]) is using negative indexing: out is a tensor of shape batch_size x seq_length x hidden_size, so out[:, 1, :] would return the first element along the second dimension (or axis), and out[:, -1, :] returns the last element along the second dimension. It would be equivalent to out[:, seq_length-1, :].

    The underscore in out, _ = self.rnn(x, h0) means that self.rnn(x, h0) returns two outputs, and out is assigned to the first output, and the second output isn't assigned to anything so _ is a placeholder.