chapter4 (1)
chapter4 (1)
loading data
INTRODUCTION TO DEEP LEARNING WITH PYTORCH
animal_name hair feathers eggs milk predator fins legs tail type
skimmer 0 1 1 0 1 0 2 1 2
gull 0 1 1 0 1 0 2 1 2
seahorse 0 0 1 0 0 1 0 1 4
tuatara 0 0 1 0 1 0 4 1 3
squirrel 1 0 0 1 0 0 2 1 1
Type key: mammal (1), bird (2), reptile (3), fish (4), amphibian (5), bug (6), invertebrate (7).
array([[0, 1, 1, 0, 1, 0, 2, 1],
[0, 1, 1, 0, 1, 0, 2, 1],
[0, 0, 1, 0, 0, 1, 0, 1],
[0, 0, 1, 0, 1, 0, 4, 1],
[1, 0, 0, 1, 0, 0, 2, 1]])
array([2, 2, 4, 3, 1])
input sample: tensor([0., 1., 1., 0., 1., 0., 2., 1.])
label_sample: tensor(2.)
batch_size = 2
shuffle = True
# Create a DataLoader
dataloader = DataLoader(dataset, batch_size=batch_size, shuffle=shuffle)
batch inputs: tensor([[0., 0., 1., 0., 0., 1., 0., 1.],
[0., 1., 1., 0., 1., 0., 2., 1.]])
batch labels: tensor([4., 2.])
batch inputs: tensor([[0., 1., 1., 0., 1., 0., 2., 1.],
[1., 0., 0., 1., 0., 0., 2., 1.]])
batch labels: tensor([2., 1.])
batch inputs: tensor([[0., 0., 1., 0., 1., 0., 4., 1.]])
batch labels: tensor([3.])
Validation
Accuracy
Training
Validation
validation_loss = 0.0
model.eval() # Put model in evaluation mode
with torch.no_grad(): # Speed up the forward pass
for i, data in enumerate(validationloader, 0):
# Run the forward pass
...
# Calculate the loss
loss = criterion(outputs, labels)
validation_loss += loss.item()
epoch_loss = validation_loss / len(validationloader)
model.train()
good performances on the training set / poor performances on the validation set
Possible causes:
Problem Solutions
Dataset is not large enough Get more data / use data augmentation
Model has too much capacity Reduce model size / add dropout
Weights are too large Weight decay
Behaves differently during training and evaluation; we must remember to switch modes
using model.train() and model.eval()
Weight decay adds penalty to loss function to discourage large weights and biases
The higher the parameter, the less likely the model is to overfit
Reduce overfitting
improve performances on the validation
set
Fine-tune hyperparameters
Data augmentation
Weight decay
Chapter 2 Chapter 4
Created and used loss functions Learned about dataloaders
Learn
Probability and statistics
Linear algebra
Calculus
Practice
Pick a dataset on Kaggle
Check out DataCamp workspace
Train a neural network