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

Model Training1 n_epochs = 10023 sbs = StepByStep(model, loss_fn, optimizer)4 sbs.set_loaders(train_loader, val_loader)5 sbs.train(n_epochs)print(model.state_dict())OutputOrderedDict([('linear.weight', tensor([[ 1.1822, -1.8684]], device='cuda:0')),('linear.bias', tensor([-0.0587], device='cuda:0'))])Evaluatinglogits_val = sbs.predict(X_val)probabilities_val = sigmoid(logits_val).squeeze()cm_thresh50 = confusion_matrix(y_val, (probabilities_val >= 0.5))cm_thresh50Outputarray([[ 7, 2],[ 1, 10]])RecapIn this chapter, we’ve gone through many concepts related to classificationproblems. This is what we’ve covered:• defining a binary classification problem• generating and preparing a toy dataset using Scikit-Learn’s make_moons()method• defining logits as the result of a linear combination of featuresRecap | 261

• understanding what odds ratios and log odds ratios are• figuring out we can interpret logits as log odds ratios• mapping logits into probabilities using a sigmoid function• defining a logistic regression as a simple neural network with a sigmoidfunction in the output• understanding the binary cross-entropy loss and its PyTorch implementationnn.BCELoss()• understanding the difference between nn.BCELoss() andnn.BCEWithLogitsLoss()• highlighting the importance of choosing the correct combination of the lastlayer and loss function• using PyTorch’s loss functions' arguments to handle imbalanced datasets• configuring model, loss function, and optimizer for a classification problem• training a model using the StepByStep class• understanding that the validation loss may be lower than the training loss• making predictions and mapping predicted logits to probabilities• using a classification threshold to convert probabilities into classes• understanding the definition of a decision boundary• understanding the concept of separability of classes and how it’s related todimensionality• exploring different classification thresholds and their effect on the confusionmatrix• reviewing typical metrics for evaluating classification algorithms, like true andfalse positive rates, precision, and recall• building ROC and precision-recall curves out of metrics computed for multiplethresholds• understanding the reason behind the quirk of losing precision while raising theclassification threshold• defining the best and worst possible ROC and PR curves• using the area under the curve to compare different modelsWow! That’s a whole lot of material! Congratulations on finishing yet another big262 | Chapter 3: A Simple Classification Problem

• understanding what odds ratios and log odds ratios are

• figuring out we can interpret logits as log odds ratios

• mapping logits into probabilities using a sigmoid function

• defining a logistic regression as a simple neural network with a sigmoid

function in the output

• understanding the binary cross-entropy loss and its PyTorch implementation

nn.BCELoss()

• understanding the difference between nn.BCELoss() and

nn.BCEWithLogitsLoss()

• highlighting the importance of choosing the correct combination of the last

layer and loss function

• using PyTorch’s loss functions' arguments to handle imbalanced datasets

• configuring model, loss function, and optimizer for a classification problem

• training a model using the StepByStep class

• understanding that the validation loss may be lower than the training loss

• making predictions and mapping predicted logits to probabilities

• using a classification threshold to convert probabilities into classes

• understanding the definition of a decision boundary

• understanding the concept of separability of classes and how it’s related to

dimensionality

• exploring different classification thresholds and their effect on the confusion

matrix

• reviewing typical metrics for evaluating classification algorithms, like true and

false positive rates, precision, and recall

• building ROC and precision-recall curves out of metrics computed for multiple

thresholds

• understanding the reason behind the quirk of losing precision while raising the

classification threshold

• defining the best and worst possible ROC and PR curves

• using the area under the curve to compare different models

Wow! That’s a whole lot of material! Congratulations on finishing yet another big

262 | Chapter 3: A Simple Classification Problem

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

Saved successfully!

Ooh no, something went wrong!