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.

Let’s take one mini-batch of images from our training set to illustrate how it works:

dummy_xs, dummy_ys = next(iter(train_loader))

dummy_xs.shape

Output

torch.Size([16, 1, 5, 5])

Our dummy mini-batch has 16 images, one channel each, dimensions five-by-five

pixels. What if we flatten this mini-batch?

flattener = nn.Flatten()

dummy_xs_flat = flattener(dummy_xs)

print(dummy_xs_flat.shape)

print(dummy_xs_flat[0])

Output

torch.Size([16, 25])

tensor([-1., -1., -1., -1., -1., -1., -1., -1., -1., -1., 1., -1.,

-1., -1., -1., -1., 1., -1., -1., -1., -1., -1., 1., -1., -1.])

By default, it preserves the first dimension such that we keep the number of data

points in the mini-batch, but it collapses the remaining dimensions. If we look at the

first element of the flattened mini-batch, we find a long tensor with 25 (1 x 5 x 5)

elements in it. If our images had three channels, the tensor would be 75 (3 x 5 x 5)

elements long.

"Don’t we lose information when we flatten pixels?"

Sure we do! And that’s why convolutional neural networks (CNNs), which we’ll

cover in the next chapter, are so successful: They do not lose this information. But,

for now, let’s do it really old style and flatten the pixels. It will make it much simpler

to illustrate a couple of other concepts before we get to the fancier CNNs.

Now, assuming the flattened version of our dataset, I ask you:

Data Preparation | 297

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

Saved successfully!

Ooh no, something went wrong!