0
0
PyTorchml~10 mins

nn.RNN layer in PyTorch - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create a simple RNN layer with input size 10 and hidden size 20.

PyTorch
rnn = nn.RNN(input_size=10, hidden_size=[1], num_layers=1)
Drag options to blanks, or click blank then click option'
A20
B10
C30
D5
Attempts:
3 left
💡 Hint
Common Mistakes
Using input_size instead of hidden_size
Setting hidden_size too small
2fill in blank
medium

Complete the code to pass an input tensor of shape (seq_len, batch, input_size) to the RNN layer.

PyTorch
output, hn = rnn([1])
Drag options to blanks, or click blank then click option'
Ainput_tensor
Bhidden_state
Coutput_tensor
Dinput_size
Attempts:
3 left
💡 Hint
Common Mistakes
Passing hidden state as input
Passing output tensor instead of input
3fill in blank
hard

Fix the error in the code to initialize the hidden state for the RNN with batch size 3 and hidden size 20.

PyTorch
hidden = torch.zeros([1], 3, 20)
Drag options to blanks, or click blank then click option'
A20
B3
C1
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Using batch size as first dimension
Using hidden size as first dimension
4fill in blank
hard

Fill both blanks to create an RNN layer with 2 layers and batch_first=True.

PyTorch
rnn = nn.RNN(input_size=10, hidden_size=20, num_layers=[1], batch_first=[2])
Drag options to blanks, or click blank then click option'
A2
BTrue
CFalse
D1
Attempts:
3 left
💡 Hint
Common Mistakes
Setting num_layers to 1
Setting batch_first to False
5fill in blank
hard

Fill all three blanks to extract the last hidden state from the output of an RNN with batch_first=True.

PyTorch
last_hidden = output[:, [1], :]

# where [2] is the last time step index

last_hidden_shape = last_hidden.shape  # should be (batch_size, [3])
Drag options to blanks, or click blank then click option'
A-1
Bseq_len - 1
Chidden_size
Dbatch_size
Attempts:
3 left
💡 Hint
Common Mistakes
Using batch_size as time step index
Confusing hidden size with batch size