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

If you want to learn more about both curves, you can checkScikit-Learn’s documentation for "Receiver OperatingCharacteristic (ROC)" [72] and "Precision-Recall" [73] . Another goodresource is Jason Brownlee’s Machine Learning Mastery blog:"How to Use ROC Curves and Precision-Recall Curves forClassification in Python" [74] and "ROC Curves and Precision-Recall Curves for Imbalanced Classification" [75] .Putting It All TogetherIn this chapter, we haven’t modified the training pipeline much. The datapreparation part is roughly the same as in the previous chapter, except for the factthat we performed the split using Scikit-Learn this time. The model configurationpart is largely the same as well, but we changed the loss function, so it is theappropriate one for a classification problem. The model training part is quitestraightforward given the development of the StepByStep class in the last chapter.But now, after training a model, we can use our class' predict() method to getpredictions for our validation set and use Scikit-Learn’s metrics module tocompute a wide range of classification metrics, like the confusion matrix, forexample.Putting It All Together | 259

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)Model Configuration1 # Sets learning rate - this is "eta" ~ the "n"-like Greek letter2 lr = 0.134 torch.manual_seed(42)5 model = nn.Sequential()6 model.add_module('linear', nn.Linear(2, 1))78 # Defines an SGD optimizer to update the parameters9 optimizer = optim.SGD(model.parameters(), lr=lr)1011 # Defines a BCE loss function12 loss_fn = nn.BCEWithLogitsLoss()260 | Chapter 3: A Simple Classification Problem

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)

Model Configuration

1 # Sets learning rate - this is "eta" ~ the "n"-like Greek letter

2 lr = 0.1

3

4 torch.manual_seed(42)

5 model = nn.Sequential()

6 model.add_module('linear', nn.Linear(2, 1))

7

8 # Defines an SGD optimizer to update the parameters

9 optimizer = optim.SGD(model.parameters(), lr=lr)

10

11 # Defines a BCE loss function

12 loss_fn = nn.BCEWithLogitsLoss()

260 | Chapter 3: A Simple Classification Problem

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

Saved successfully!

Ooh no, something went wrong!