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([[ 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 Dataset

Let’s create a dataset with variable-length sequences and train a model using it:

Data Generation

1 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 Preparation

We simply cannot use a TensorDataset, because we cannot create a tensor out of a

list 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 associated

label:

664 | Chapter 8: Sequences

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

Saved successfully!

Ooh no, something went wrong!