Search code examples
pythonpytorchlstmrecurrent-neural-network

How does pad_packed_sequence work in pytorch?


Output of LSTM in pytorch: enter image description here I gave the input as packed sequence (birectional LSTM) then acording to the doucments only output is packed and h_n, c_n are returned as tensor? After applying pad_packed_sequence function to output to unpack it how do I get hidden states as tensor? I saw somewhere this code: pad_packed_sequence(output)[0], why do we have to take 0-index here? Also for last hidden and cell state I get tensors using h_n[0],h_n[1] and c_n[0],c_n[1]. In this case 0 and 1 indexing is done to get forward and backward hidden and cell states. I don't understand the 0-indexing for output and why is h_[n] and c_[n] not returned as paked sequence also?


Solution

  • We should take 0-index, because pad_packed_sequence returns output and the output_lengths.