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.

Putting It All Together

We have updated each of the three fundamental parts of our code at least twice. It

is time to put it all together to get an overall view of what we have achieved so far.

Behold your pipeline: Data Preparation V2, Model Configuration V3, and Model

Training V5!

Run - Data Preparation V2

1 # %load data_preparation/v2.py

2

3 torch.manual_seed(13)

4

5 # Builds tensors from numpy arrays BEFORE split

6 x_tensor = torch.as_tensor(x).float()

7 y_tensor = torch.as_tensor(y).float()

8

9 # Builds dataset containing ALL data points

10 dataset = TensorDataset(x_tensor, y_tensor)

11

12 # Performs the split

13 ratio = .8

14 n_total = len(dataset)

15 n_train = int(n_total * ratio)

16 n_val = n_total - n_train

17 train_data, val_data = random_split(dataset, [n_train, n_val])

18 # Builds a loader of each set

19 train_loader = DataLoader(

20 dataset=train_data,

21 batch_size=16,

22 shuffle=True,

23 )

24 val_loader = DataLoader(dataset=val_data, batch_size=16)

170 | Chapter 2: Rethinking the Training Loop

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

Saved successfully!

Ooh no, something went wrong!