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.

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

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

19

20 # Builds a loader of each set

21 train_loader = DataLoader(

22 dataset=train_data,

23 batch_size=16,

24 shuffle=True

25 )

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

Putting It All Together | 203

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

Saved successfully!

Ooh no, something went wrong!