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.

Data Generation — Train

1 points, directions = generate_sequences(n=256, seed=13)

2 full_train = torch.as_tensor(points).float()

3 target_train = full_train[:, 2:]

For the test set, though, we only need the source sequences as features (X) and the

target sequences as labels (y):

Data Generation — Test

1 test_points, test_directions = generate_sequences(seed=19)

2 full_test = torch.as_tensor(test_points).float()

3 source_test = full_test[:, :2]

4 target_test = full_test[:, 2:]

These are all simple tensors, so we can use TensorDatasets and simple data

loaders:

Data Preparation

1 train_data = TensorDataset(full_train, target_train)

2 test_data = TensorDataset(source_test, target_test)

3 generator = torch.Generator()

4 train_loader = DataLoader(train_data, batch_size=16,

5 shuffle=True, generator=generator)

6 test_loader = DataLoader(test_data, batch_size=16)

In version 1.7, PyTorch introduced a modification to the random

sampler in the DataLoader that’s responsible for shuffling the

data. In order to ensure reproducibility, we need to assign a

Generator to the DataLoader (we did something similar in

Chapter 4 when we used other samplers). Luckily, our StepByStep

class already sets a seed to the generator, if there is one, in its

set_seed() method, so you don’t need to worry about that.

By the way, we didn’t use the directions to build the datasets this time.

We have everything we need to train our first sequence-to-sequence model now!

702 | Chapter 9 — Part I: Sequence-to-Sequence

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

Saved successfully!

Ooh no, something went wrong!