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 Preparation

There hasn’t been much data preparation up to this point, to be honest. After

generating our data points in Notebook Cell 1.1, the only preparation step

performed so far has been transforming Numpy arrays into PyTorch tensors, as in

Notebook Cell 1.3, which is reproduced below:

Define - Data Preparation V0

1 %%writefile data_preparation/v0.py

2

3 device = 'cuda' if torch.cuda.is_available() else 'cpu'

4

5 # Our data was in Numpy arrays, but we need to transform them

6 # into PyTorch's Tensors and then send them to the

7 # chosen device

8 x_train_tensor = torch.as_tensor(x_train).float().to(device)

9 y_train_tensor = torch.as_tensor(y_train).float().to(device)

Run - Data Preparation V0

%run -i data_preparation/v0.py

This part will get much more interesting in the next chapter when we get to use

Dataset and DataLoader classes :-)

"What’s the purpose of saving cells to these files?"

We know we have to run the full sequence to train a model: data preparation,

model configuration, and model training. In Chapter 2, we’ll gradually improve each

of these parts, versioning them inside each corresponding folder. So, saving them to

files allows us to run a full sequence using different versions without having to

duplicate code.

Let’s say we start improving model configuration (and we will do exactly that in

Chapter 2), but the other two parts are still the same; how do we run the full

sequence?

116 | Chapter 1: A Simple Regression Problem

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

Saved successfully!

Ooh no, something went wrong!