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

So, to actually get the last output, we need to use some fancy indexing and theinformation about original sizes returned by pad_packed_sequence():seq_idx = torch.arange(seq_sizes.size(0))output_unpacked[seq_idx, seq_sizes-1]Outputtensor([[ 0.3161, -0.1675],[ 0.3051, 0.7055],[-0.4832, 0.6595]], grad_fn=<IndexBackward>)And we finally have the last output for each packed sequence, even if we’re using abidirectional network.Packing (from padded)You can also convert an already padded sequence into a packed sequence usingPyTorch’s nn.utils.rnn.pack_padded_sequence(). Since the sequence is alreadypadded, though, we need to compute the original sizes ourselves:len_seqs = [len(seq) for seq in all_seqs]len_seqsOutput[4, 2, 3]And then pass them as an argument:packed = rnn_utils.pack_padded_sequence(padded, len_seqs,enforce_sorted=False,batch_first=True)packedVariable-Length Sequences | 663

OutputPackedSequence(data=tensor([[ 1.0349, 0.9661],[-1.1247, -0.9683],[-1.0911, 0.9254],[ 0.8055, -0.9169],[ 0.8182, -0.9944],[-1.0771, -1.0414],[-0.8251, -0.9499],[ 1.0081, 0.7680],[-0.8670, 0.9342]]), batch_sizes=tensor([3, 3, 2, 1]),sorted_indices=tensor([0, 2, 1]), unsorted_indices=tensor([0, 2,1]))Variable-Length DatasetLet’s create a dataset with variable-length sequences and train a model using it:Data Generation1 var_points, var_directions = generate_sequences(variable_len=True)2 var_points[:2]Output[array([[ 1.12636495, 1.1570899 ],[ 0.87384513, -1.00750892],[-0.9149893 , -1.09150317],[-1.0867348 , 1.07731667]]),array([[ 0.92250954, -0.89887678],[ 1.0941646 , 0.92300589]])]Data PreparationWe simply cannot use a TensorDataset, because we cannot create a tensor out of alist of elements with different sizes.So, we must build a custom dataset that makes a tensor out of each sequence and,when prompted for a given item, returns the corresponding tensor and associatedlabel:664 | Chapter 8: Sequences

So, to actually get the last output, we need to use some fancy indexing and the

information about original sizes returned by pad_packed_sequence():

seq_idx = torch.arange(seq_sizes.size(0))

output_unpacked[seq_idx, seq_sizes-1]

Output

tensor([[ 0.3161, -0.1675],

[ 0.3051, 0.7055],

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

And we finally have the last output for each packed sequence, even if we’re using a

bidirectional network.

Packing (from padded)

You can also convert an already padded sequence into a packed sequence using

PyTorch’s nn.utils.rnn.pack_padded_sequence(). Since the sequence is already

padded, though, we need to compute the original sizes ourselves:

len_seqs = [len(seq) for seq in all_seqs]

len_seqs

Output

[4, 2, 3]

And then pass them as an argument:

packed = rnn_utils.pack_padded_sequence(padded, len_seqs,

enforce_sorted=False,

batch_first=True)

packed

Variable-Length Sequences | 663

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

Saved successfully!

Ooh no, something went wrong!