Complete the code to initialize the hidden state for an RNN with batch size 1 and hidden size 5.
hidden = torch.zeros(1, 1, [1])
The hidden state size must match the RNN's hidden size, which is 5 here.
Complete the code to detach the hidden state from the computation graph to avoid backpropagating through entire history.
hidden = hidden.[1]()Using detach() breaks the computation graph, preventing gradients from flowing backward beyond this point.
Fix the error in the code to correctly initialize hidden state for a 2-layer LSTM with batch size 3 and hidden size 4.
hidden = (torch.zeros([1], 3, 4), torch.zeros([1], 3, 4))
The first dimension is the number of layers, which is 2 here for a 2-layer LSTM.
Fill both blanks to correctly update hidden state inside a training loop for an RNN, detaching it to avoid backpropagating through entire history.
for input in inputs: output, hidden = rnn(input, [1]) hidden = hidden.[2]()
The hidden state from the previous step is passed as input to the RNN, then detached to stop gradient flow beyond this step.
Fill all three blanks to create a dictionary comprehension that stores the length of each word only if the length is greater than 3.
lengths = {word: [1] for word in words if len(word) [2] 3 and word.[3]('a')}This comprehension stores the length of each word if its length is greater than 3 and it starts with 'a'.