22.02.2024 Views

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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Output

(PackedSequence(data=tensor([[-0.6388, 0.8505],

[ 0.3385, 0.5927],

[ 0.2911, -0.1811],

[-0.4215, 0.8979],

[-0.3875, 0.9422],

[ 0.3051, 0.7055],

[ 0.3792, 0.3432],

[-0.4832, 0.6595],

[ 0.3161, -0.1675]], grad_fn=<CatBackward>), batch_sizes

=tensor([3, 3, 2, 1]), sorted_indices=tensor([0, 2, 1]),

unsorted_indices=tensor([0, 2, 1])),

tensor([[[ 0.3161, -0.1675],

[ 0.3051, 0.7055],

[-0.4832, 0.6595]]], grad_fn=<IndexSelectBackward>))

If the input is packed, the output tensor is packed too, but the

hidden state is not.

Let’s compare both final hidden states, from padded and packed sequences:

hidden_packed == hidden_padded

Output

tensor([[[ True, True],

[False, False],

[False, False]]])

From three sequences, only one matches. Well, this shouldn’t be a surprise; after

all, we’re packing sequences to avoid updating the hidden state with padded

inputs.

"Cool, so I can use the permuted hidden state, right?"

Well, it depends:

• Yes, if you’re using networks that are not bidirectional—the final hidden state

660 | Chapter 8: Sequences

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

Saved successfully!

Ooh no, something went wrong!