Skip to main content
Botmartz AI Solution PVT LTD.
Home›Engineering Insights›OPTIMIZATION
OPTIMIZATION

Regularization Techniques: L1/L2, Dropout, Early Stopping, and Preventing Overfitting

Models overfit by memorizing noise. Use L1/L2 regularization to shrink weights, dropout to disable random neurons, early stopping to stop before overfitting.

SS
Soham Sharma
AI Engineer, Botmartz · October 19, 2026 · 12 min read
Read Time
12 min
Failure Modes
5
Code Snippets
3
Runnable Notebook
1
Regularization Techniques: L1/L2, Dropout, Early Stopping, and Preventing Overfitting

Regularization prevents overfitting: models fitting noise rather than signal. L1/L2 penalize large weights. Dropout randomly disables neurons. Early stopping halts when validation loss stops improving.

L1/L2 Regularization

Code Snippet
# L2 (ridge): loss += λ * ||w||^2
# Shrinks weights toward zero

optimizer = torch.optim.Adam(model.parameters(), lr=0.001, weight_decay=1e-5)
# weight_decay = L2 regularization strength

# L1 (lasso): loss += λ * ||w||
# Drives weights to exactly zero (feature selection)

Dropout

Code Snippet
class RegularizedModel(nn.Module):
    def __init__(self):
        super().__init__()
        self.fc1 = nn.Linear(100, 64)
        self.dropout = nn.Dropout(p=0.5)  # Drop 50% of neurons
        self.fc2 = nn.Linear(64, 10)
    
    def forward(self, x):
        x = F.relu(self.fc1(x))
        x = self.dropout(x)  # Random neurons disabled
        x = self.fc2(x)
        return x

Dropout forces the network to learn redundant representations.

Early Stopping

Code Snippet
best_val_loss = float('inf')
patience = 10
patience_counter = 0

for epoch in range(num_epochs):
    train_loss = train()
    val_loss = validate()
    
    if val_loss < best_val_loss:
        best_val_loss = val_loss
        patience_counter = 0
        torch.save(model.state_dict(), 'best_model.pth')
    else:
        patience_counter += 1
        if patience_counter >= patience:
            print("Stopping early")
            break

Conclusion

Regularization prevents overfitting by constraining model complexity. L2 shrinks weights, dropout learns redundancy, early stopping stops before overfitting. Combining techniques is more effective than any single technique alone. Next: batch normalization and layer normalization.

Closing Takeaways

✓
Measure retrieval precision and recall in isolation before touching the model.
✓
Chunk along document structure, not arbitrary character counts.
✓
Combine vector and keyword search — hybrid retrieval beats either alone.
✓
Treat evaluation as continuous infrastructure, not a launch-week report.
Try It Yourself
A runnable Google Colab notebook with the eval harness and hybrid search code from this post.
#Regularization#Overfitting#Training#Deep Learning
0 views
SS
Soham Sharma
AI Engineer at Botmartz, building enterprise RAG and agent systems in production. Contributing to open-source libraries.

Discussion (0)

No approved comments yet. Be the first to share your thoughts!

Leave a Comment

Your email address will not be published. Required fields are marked *

More Engineering Insights
AI AGENTS
9 min read
AI AGENTS
11 min read
OPEN SOURCE
6 min read
AI ENGINEERING
8 min read
CASE STUDIES
14 min read