Chapter 2
Chapter 2
pass
INTRODUCTION TO DEEP LEARNING WITH PYTORCH
4. Repeat until weights and biases are tuned to produce useful outputs
Outputs:
five probabilities between zero and one
Classification:
Class = 1 for first and third values: 0.5188 , 0.5015
Class = 0 for second, fourth and fifth values: 0.3761 , 0.3718 , 0.4633
torch.Size([5, 3])
Outputs:
The output dimension is 5 × 3
# Return output
print(output)
F.one_hot(torch.tensor(0), num_classes = 3)
tensor([1, 0, 0])
F.one_hot(torch.tensor(1), num_classes = 3)
tensor([0, 1, 0])
F.one_hot(torch.tensor(2), num_classes = 3)
tensor([0, 0, 1])
criterion = CrossEntropyLoss()
criterion(scores.double(), one_hot_target.double())
tensor(0.8131, dtype=torch.float64)
scores
model predictions before the final softmax function
one_hot_target
one hot encoded ground truth label
and outputs
loss
a single float.
steep slopes:
a step makes us lose a lot of elevation =
derivative is high (red arrows)
gentler slopes:
a step makes us lose a little bit of
elevation = derivative is low (green
arrows)
valley floor:
not losing elevation by taking a step =
derivative is null (blue arrow)
Optimizer handles updating model parameters (or weights) after calculation of local
gradients
optimizer.step()
3. Create a dataset
4. Define an optimizer
5. Run a training loop, where for each sample of the dataset, we repeat:
Calculating loss (forward pass)
in PyTorch
criterion = nn.MSELoss()
# Prediction and target are float tensors
loss = criterion(prediction, target)
This loss is used for regression problems (e.g., when trying to fit a linear regression model).