Introduction
The nn.RNN layer helps a computer remember information from a sequence, like words in a sentence, so it can understand patterns over time.
Jump into concepts and practice - no test required
torch.nn.RNN(input_size, hidden_size, num_layers=1, nonlinearity='tanh', batch_first=False, dropout=0, bidirectional=False)
rnn = torch.nn.RNN(input_size=10, hidden_size=20)
rnn = torch.nn.RNN(5, 15, num_layers=2, batch_first=True)
rnn = torch.nn.RNN(8, 12, nonlinearity='relu', bidirectional=True)
import torch import torch.nn as nn # Create an RNN layer rnn = nn.RNN(input_size=3, hidden_size=5, num_layers=1, batch_first=True) # Sample input: batch size 2, sequence length 4, input size 3 input_seq = torch.randn(2, 4, 3) # Initial hidden state: num_layers=1, batch=2, hidden_size=5 h0 = torch.zeros(1, 2, 5) # Forward pass output, hn = rnn(input_seq, h0) print('Output shape:', output.shape) print('Output:', output) print('Hidden state shape:', hn.shape) print('Hidden state:', hn)
nn.RNN layer in PyTorch primarily do?nn.RNN(input_size=10, hidden_size=20) matches the correct parameter order and names; the others reverse sizes, omit hidden_size, or swap parameters.output after running the RNN?
import torch import torch.nn as nn rnn = nn.RNN(input_size=5, hidden_size=3, batch_first=True) input = torch.randn(4, 7, 5) # batch=4, seq_len=7, input_size=5 output, hn = rnn(input)
rnn = nn.RNN(input_size=8, hidden_size=4) input = torch.randn(3, 5, 10) # batch=3, seq_len=5, input_size=10 output, hn = rnn(input)
nn.RNN. Which approach correctly handles this in PyTorch?pack_padded_sequence to inform RNN about actual lengths.pack_padded_sequence before the RNN correctly describes this approach. Options B and C ignore padding/packing, causing errors or inefficiency. Set hidden_size equal to the longest sequence length is unrelated to sequence length handling.pack_padded_sequence before the RNN -> Option A