0
0
PyTorchml~10 mins

Hidden state management 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 initialize the hidden state for an RNN with batch size 1 and hidden size 5.

PyTorch
hidden = torch.zeros(1, 1, [1])
Drag options to blanks, or click blank then click option'
A7
B10
C3
D5
Attempts:
3 left
💡 Hint
Common Mistakes
Using batch size or sequence length instead of hidden size.
2fill in blank
medium

Complete the code to detach the hidden state from the computation graph to avoid backpropagating through entire history.

PyTorch
hidden = hidden.[1]()
Drag options to blanks, or click blank then click option'
Adetach
Bzero_
Crequires_grad_
Dclone
Attempts:
3 left
💡 Hint
Common Mistakes
Using clone() does not detach gradients.
Using zero_() resets values but does not detach.
3fill in blank
hard

Fix the error in the code to correctly initialize hidden state for a 2-layer LSTM with batch size 3 and hidden size 4.

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

Fill both blanks to correctly update hidden state inside a training loop for an RNN, detaching it to avoid backpropagating through entire history.

PyTorch
for input in inputs:
    output, hidden = rnn(input, [1])
    hidden = hidden.[2]()
Drag options to blanks, or click blank then click option'
Ahidden
Binput
Cdetach
Dclone
Attempts:
3 left
💡 Hint
Common Mistakes
Passing input instead of hidden as RNN state.
Using clone() instead of detach().
5fill in blank
hard

Fill all three blanks to create a dictionary comprehension that stores the length of each word only if the length is greater than 3.

PyTorch
lengths = {word: [1] for word in words if len(word) [2] 3 and word.[3]('a')}
Drag options to blanks, or click blank then click option'
Alen(word)
B>
Cstartswith
Dendswith
Attempts:
3 left
💡 Hint
Common Mistakes
Using endswith instead of startswith.
Using '<' instead of '>'.