Skip to main content

1. Overfitting and Underfitting

Underfitting​

The model is too simple to learn the underlying pattern.

Overfitting​

The model learns the training examples too specifically.

Solutions - These are called Regularization Techniques

weight decay"Don't let weights become too large."
L2 regularization"Add a penalty for large weights to the loss."
dropout"Randomly switch off some neurons during training."

Weight Decay Implementation​

optimizer = optim.Adam(model.parameters(), lr=0.001, weight_decay=0.01)

L2 Regularization​

l2 = sum(torch.sum(p ** 2) for p in model.parameters())
loss = loss + 0.01 * l2

Dropout​

self.dropout = nn.Dropout(0.2)

Actual Implementation of all 3 Regularizations for Overfitting​

Code Block