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

peiying410632
from peiying410632 More from this publisher
22.02.2024 Views

does match the last output.• No, if you’re using a bidirectional network—you’re only getting the properlyaligned 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, forexample, we could get the corresponding indices from the data tensor in the packedoutput:output_packed.data[[2, 5]] # x1 sequenceOutputtensor([[ 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’snn.utils.rnn.pad_packed_sequence(). The name does not help, I know; I wouldrather call it unpack_sequence_to_padded() instead. Anyway, we can use it totransform 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_sizesVariable-Length Sequences | 661

Output(tensor([[[-0.6388, 0.8505],[-0.4215, 0.8979],[ 0.3792, 0.3432],[ 0.3161, -0.1675]],[[ 0.2911, -0.1811],[ 0.3051, 0.7055],[ 0.0000, 0.0000],[ 0.0000, 0.0000]],[[ 0.3385, 0.5927],[-0.3875, 0.9422],[-0.4832, 0.6595],[ 0.0000, 0.0000]]], grad_fn=<IndexSelectBackward>),tensor([4, 2, 3]))It returns both the padded sequences and the original sizes, which will be useful aswell."Problem solved then? Can I take the last output now?"Almost there—if you were to take the last output in the same way we did before,you’d still get some padded zeros back:output_unpacked[:, -1]Outputtensor([[ 0.3161, -0.1675],[ 0.0000, 0.0000],[ 0.0000, 0.0000]], grad_fn=<SelectBackward>)662 | Chapter 8: Sequences

Output

(tensor([[[-0.6388, 0.8505],

[-0.4215, 0.8979],

[ 0.3792, 0.3432],

[ 0.3161, -0.1675]],

[[ 0.2911, -0.1811],

[ 0.3051, 0.7055],

[ 0.0000, 0.0000],

[ 0.0000, 0.0000]],

[[ 0.3385, 0.5927],

[-0.3875, 0.9422],

[-0.4832, 0.6595],

[ 0.0000, 0.0000]]], grad_fn=<IndexSelectBackward>),

tensor([4, 2, 3]))

It returns both the padded sequences and the original sizes, which will be useful as

well.

"Problem solved then? Can I take the last output now?"

Almost there—if you were to take the last output in the same way we did before,

you’d still get some padded zeros back:

output_unpacked[:, -1]

Output

tensor([[ 0.3161, -0.1675],

[ 0.0000, 0.0000],

[ 0.0000, 0.0000]], grad_fn=<SelectBackward>)

662 | Chapter 8: Sequences

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

Saved successfully!

Ooh no, something went wrong!