Daniel Voigt Godoy - Deep Learning with PyTorch Step-by-Step A Beginner’s Guide-leanpub

peiying410632
from peiying410632 More from this publisher
22.02.2024 Views

Next, we’ll standardize the features using Scikit-Learn’s StandardScaler:Feature Standardization1 sc = StandardScaler()2 sc.fit(X_train)34 X_train = sc.transform(X_train)5 X_val = sc.transform(X_val)Remember, you should use only the training set to fit theStandardScaler, and then use its transform() method to applythe pre-processing step to all datasets: training, validation, andtest. Otherwise, you’ll be leaking information from the validationand / or test sets to your model!Figure 3.1 - Moons datasetData PreparationHopefully, this step feels familiar to you already! As usual, the data preparationstep converts Numpy arrays into PyTorch tensors, builds TensorDatasets for them,and creates the corresponding data loaders.Data Preparation | 209

Data Preparation1 torch.manual_seed(13)23 # Builds tensors from Numpy arrays4 x_train_tensor = torch.as_tensor(X_train).float()5 y_train_tensor = torch.as_tensor(y_train.reshape(-1, 1)).float()67 x_val_tensor = torch.as_tensor(X_val).float()8 y_val_tensor = torch.as_tensor(y_val.reshape(-1, 1)).float()910 # Builds dataset containing ALL data points11 train_dataset = TensorDataset(x_train_tensor, y_train_tensor)12 val_dataset = TensorDataset(x_val_tensor, y_val_tensor)1314 # Builds a loader of each set15 train_loader = DataLoader(16 dataset=train_dataset,17 batch_size=16,18 shuffle=True19 )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 andx 2 , and the labels (y) are either zero (red) or one (blue). We have a dataset; now weneed a…ModelGiven a classification problem, one of the more straightforward models is thelogistic regression. But, instead of simply presenting it and using it right away, I amgoing to build up to it. The rationale behind this approach is twofold: First, it willmake clear why this algorithm is called logistic regression if it is used forclassification; 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 agood starting point. What would a linear regression model with two features looklike?210 | Chapter 3: A Simple Classification Problem

Next, we’ll standardize the features using Scikit-Learn’s StandardScaler:

Feature Standardization

1 sc = StandardScaler()

2 sc.fit(X_train)

3

4 X_train = sc.transform(X_train)

5 X_val = sc.transform(X_val)

Remember, you should use only the training set to fit the

StandardScaler, and then use its transform() method to apply

the pre-processing step to all datasets: training, validation, and

test. Otherwise, you’ll be leaking information from the validation

and / or test sets to your model!

Figure 3.1 - Moons dataset

Data Preparation

Hopefully, this step feels familiar to you already! As usual, the data preparation

step converts Numpy arrays into PyTorch tensors, builds TensorDatasets for them,

and creates the corresponding data loaders.

Data Preparation | 209

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

Saved successfully!

Ooh no, something went wrong!