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.

We can convert the former into the latter using a one-liner:

example_tensor = torch.as_tensor(example / 255).float()

Moreover, we can use this line of code to convert our whole Numpy dataset into

tensors so they become an appropriate input to our composed transformation.

Data Preparation

The first step of data preparation, as in previous chapters, is to convert our

features and labels from Numpy arrays to PyTorch tensors:

# Builds tensors from numpy arrays BEFORE split

x_tensor = torch.as_tensor(images / 255).float()

y_tensor = torch.as_tensor(labels.reshape(-1, 1)).float()

The only difference is that we scaled the images to get them into the expected [0.0,

1.0] range.

Dataset Transforms

Next, we use both tensors to build a Dataset, but not a simple TensorDataset like

before. Once again, we’ll build our own custom dataset that is capable of handling

transformations. Its code is actually quite simple:

Data Preparation | 283

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

Saved successfully!

Ooh no, something went wrong!