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

import numpy as npimport torchimport torch.optim as optimimport torch.nn as nnimport torch.functional as Ffrom torch.utils.data import DataLoader, TensorDatasetfrom sklearn.datasets import make_moonsfrom sklearn.preprocessing import StandardScalerfrom sklearn.model_selection import train_test_splitfrom sklearn.metrics import confusion_matrix, roc_curve, \precision_recall_curve, aucfrom stepbystep.v0 import StepByStepA Simple Classification ProblemIt is time to handle a different class of problems: classification problems (punintended). In a classification problem, we’re trying to predict which class a datapoint belongs to.Let’s say we have two classes of points: They are either red or blue. These are thelabels (y) of the points. Sure enough, we need to assign numeric values to them. Wecould assign zero to red and one to blue. The class associated with zero is thenegative class, while one corresponds to the positive class.In a nutshell, for binary classification, we have:Color Value ClassRed 0 NegativeBlue 1 PositiveIMPORTANT: In a classification model, the output is thepredicted probability of the positive class. In our case, the modelwill predict the probability of a point being blue.A Simple Classification Problem | 207

The choice of which class is positive and which class is negative doesnot affect model performance. If we reverse the mapping, makingred the positive class, the only difference would be that the modelwould predict the probability of a point being red. But, since bothprobabilities have to add up to one, we could easily convertbetween them, so the models are equivalent.Instead of defining a model first and then generating synthetic data for it, we’ll do itthe other way around.Data GenerationLet’s make the data a bit more interesting by using two features (x 1 and x 2 ) thistime. We’ll use Scikit-Learn’s make_moons() to generate a toy dataset with 100data points. We will also add some Gaussian noise and set a random seed to ensurereproducibility.Data Generation1 X, y = make_moons(n_samples=100, noise=0.3, random_state=0)Then, we’ll perform the train-validation split using Scikit-Learn’strain_test_split() for convenience (we’ll get back to splitting indices later):Train-validation Split1 X_train, X_val, y_train, y_val = train_test_split(2 X,3 y,4 test_size=.2,5 random_state=136 )Remember, the split should always be the first thing you do—nopre-processing, no transformations, nothing happens before thesplit.208 | Chapter 3: A Simple Classification Problem

import numpy as np

import torch

import torch.optim as optim

import torch.nn as nn

import torch.functional as F

from torch.utils.data import DataLoader, TensorDataset

from sklearn.datasets import make_moons

from sklearn.preprocessing import StandardScaler

from sklearn.model_selection import train_test_split

from sklearn.metrics import confusion_matrix, roc_curve, \

precision_recall_curve, auc

from stepbystep.v0 import StepByStep

A Simple Classification Problem

It is time to handle a different class of problems: classification problems (pun

intended). In a classification problem, we’re trying to predict which class a data

point belongs to.

Let’s say we have two classes of points: They are either red or blue. These are the

labels (y) of the points. Sure enough, we need to assign numeric values to them. We

could assign zero to red and one to blue. The class associated with zero is the

negative class, while one corresponds to the positive class.

In a nutshell, for binary classification, we have:

Color Value Class

Red 0 Negative

Blue 1 Positive

IMPORTANT: In a classification model, the output is the

predicted probability of the positive class. In our case, the model

will predict the probability of a point being blue.

A Simple Classification Problem | 207

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

Saved successfully!

Ooh no, something went wrong!