22.02.2024 Views

Daniel Voigt Godoy - Deep Learning with PyTorch Step-by-Step A Beginner’s Guide-leanpub

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

does match the last output.

• No, if you’re using a bidirectional network—you’re only getting the properly

aligned hidden states in the last output, so you’ll need to unpack it.

To unpack the actual sequence of hidden states for the shortest sequence, for

example, we could get the corresponding indices from the data tensor in the packed

output:

output_packed.data[[2, 5]] # x1 sequence

Output

tensor([[ 0.2911, -0.1811],

[ 0.3051, 0.7055]], grad_fn=<IndexBackward>)

But that would be extremely annoying so, no, you don’t have to.

Unpacking (to padded)

You can unpack a sequence using PyTorch’s

nn.utils.rnn.pad_packed_sequence(). The name does not help, I know; I would

rather call it unpack_sequence_to_padded() instead. Anyway, we can use it to

transform our packed output into a regular, yet padded, output:

output_unpacked, seq_sizes = \

rnn_utils.pad_packed_sequence(output_packed, batch_first=True)

output_unpacked, seq_sizes

Variable-Length Sequences | 661

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!