0
0
PyTorchml~10 mins

nn.LSTM 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 an LSTM layer with input size 10 and hidden size 20.

PyTorch
lstm = nn.LSTM(input_size=[1], hidden_size=20)
Drag options to blanks, or click blank then click option'
A5
B10
C20
D15
Attempts:
3 left
💡 Hint
Common Mistakes
Confusing input_size with hidden_size.
Using a value that does not match the input data features.
2fill in blank
medium

Complete the code to initialize hidden and cell states for an LSTM with batch size 3 and hidden size 5.

PyTorch
h0 = torch.zeros(1, [1], 5)
c0 = torch.zeros(1, [1], 5)
Drag options to blanks, or click blank then click option'
A10
B1
C5
D3
Attempts:
3 left
💡 Hint
Common Mistakes
Using hidden size instead of batch size for the second dimension.
Confusing the order of dimensions.
3fill in blank
hard

Fix the error in the code to run input through the LSTM layer.

PyTorch
output, (hn, cn) = lstm([1])
Drag options to blanks, or click blank then click option'
Ainput_tensor
Bx
Cinputs
Dinput
Attempts:
3 left
💡 Hint
Common Mistakes
Using undefined variable names.
Passing the wrong variable to the LSTM.
4fill in blank
hard

Fill both blanks to create an LSTM with 2 layers and batch_first enabled.

PyTorch
lstm = nn.LSTM(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 instead of 2.
Forgetting to set batch_first to True when input shape is batch first.
5fill in blank
hard

Fill all three blanks to extract the last hidden state from the LSTM output.

PyTorch
last_hidden = hn[[1], [2], [3]]
Drag options to blanks, or click blank then click option'
A-1
B0
C1
D2
Attempts:
3 left
💡 Hint
Common Mistakes
Using positive indices for the last layer instead of -1.
Mixing up batch and hidden unit indices.