Complete the code to create a simple RNN layer with input size 10 and hidden size 20.
rnn = nn.RNN(input_size=10, hidden_size=[1], num_layers=1)
The hidden size defines the number of features in the hidden state. Here, it should be 20.
Complete the code to pass an input tensor of shape (seq_len, batch, input_size) to the RNN layer.
output, hn = rnn([1])The RNN layer expects the input tensor as the first argument.
Fix the error in the code to initialize the hidden state for the RNN with batch size 3 and hidden size 20.
hidden = torch.zeros([1], 3, 20)
The first dimension is num_layers, which is 1 here, so it should be 1.
Fill both blanks to create an RNN layer with 2 layers and batch_first=True.
rnn = nn.RNN(input_size=10, hidden_size=20, num_layers=[1], batch_first=[2])
num_layers should be 2 and batch_first should be True to have batch as first dimension.
Fill all three blanks to extract the last hidden state from the output of an RNN with batch_first=True.
last_hidden = output[:, [1], :] # where [2] is the last time step index last_hidden_shape = last_hidden.shape # should be (batch_size, [3])
To get the last time step, use index -1 or seq_len - 1. The last hidden shape has batch size and hidden size.