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.

Data Preparation

1 torch.manual_seed(13)

2

3 # Builds tensors from Numpy arrays

4 x_train_tensor = torch.as_tensor(X_train).float()

5 y_train_tensor = torch.as_tensor(y_train.reshape(-1, 1)).float()

6

7 x_val_tensor = torch.as_tensor(X_val).float()

8 y_val_tensor = torch.as_tensor(y_val.reshape(-1, 1)).float()

9

10 # Builds dataset containing ALL data points

11 train_dataset = TensorDataset(x_train_tensor, y_train_tensor)

12 val_dataset = TensorDataset(x_val_tensor, y_val_tensor)

13

14 # Builds a loader of each set

15 train_loader = DataLoader(

16 dataset=train_dataset,

17 batch_size=16,

18 shuffle=True

19 )

20 val_loader = DataLoader(dataset=val_dataset, batch_size=16)

There are 80 data points (N = 80) in our training set. We have two features, x 1 and

x 2 , and the labels (y) are either zero (red) or one (blue). We have a dataset; now we

need a…

Model

Given a classification problem, one of the more straightforward models is the

logistic regression. But, instead of simply presenting it and using it right away, I am

going to build up to it. The rationale behind this approach is twofold: First, it will

make clear why this algorithm is called logistic regression if it is used for

classification; second, you’ll get a clear understanding of what a logit is.

Well, since it is called logistic regression, I would say that linear regression is a

good starting point. What would a linear regression model with two features look

like?

210 | Chapter 3: A Simple Classification Problem

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

Saved successfully!

Ooh no, something went wrong!