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.

Then, for each subset of data, we’ll build a corresponding DataLoader, so our code

will look like this:

Define - Data Preparation V2

1 %%writefile 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() 1

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

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]) 2

18

19 # Builds a loader of each set

20 train_loader = DataLoader(

21 dataset=train_data,

22 batch_size=16,

23 shuffle=True,

24 )

25 val_loader = DataLoader(dataset=val_data, batch_size=16) 3

1 Making tensors out of the full dataset (before split)

2 Performing train-validation split in PyTorch

3 Creating a data loader for the validation set

Run - Data Preparation V2

%run -i data_preparation/v2.py

Now that we have a data loader for our validation set, it makes sense to use it for

the…

146 | Chapter 2: Rethinking the Training Loop

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

Saved successfully!

Ooh no, something went wrong!