In this article
Overfitting vs Underfitting: Model ઘણો ભણ્યો કે ઓછો ભણ્યો?
પરીક્ષાની તૈયારી. ત્રણ વિદ્યાર્થી:
વિદ્યાર્થી A — "રટ્ટુ": ગયા વર્ષના બધા જ Papers ગોખી નાખ્યા — Page Number સુધ્ધાં. પ્રેક્ટિસ Paper ઉપર 100/100. પણ Exam માં નવો Twist — ફેઈલ!
વિદ્યાર્થી B — "આળસુ": કંઈ ભણ્યો જ નહીં — "AI-BAI — બધું AI શું?" ખ્યાલ ન. પ્રેક્ટિસ Paper ઉપર 20/100. Exam માં પણ ફેઈલ.
વિદ્યાર્થી C — "Smart": Concepts સમજ્યો, Variety of Questions Practice, Reasoning Built. Exam માં ગમે તે New Question — Pass!
ML Model ઉપર:
- વિદ્યાર્થી A = Overfitting
- વિદ્યાર્થી B = Underfitting
- વિદ્યાર્થી C = Just Right (Good Fit)
Overfitting — "ઘણો ભણ્યો, Flexible ન રહ્યો"
Overfitting = Model Training Data ઉપર ઘણો સારો, પણ New Data ઉપર ખરાબ.
Model Training Data ના ઘણા Detail — Noise — Exceptions પણ "ભૂલ"ની માફક શીખી ગયો. Generalize ન થઈ શક્યો.
ઉદાહરણ:
Cat-Dog Classifier Train કર્યો — 1000 Photos:
- Training Data ઉપર: 99% Accuracy ✅
- New Photos (Test) ઉપર: 55% Accuracy ❌
Model "Cat" ઓળખવા ને બદલે — "આ specific 1000 Photos" ઓળખ્યો. નવી Cat Photo — ન ઓળખ્યો!
Overfitting ક્યારે થાય?
- Training Data ઓછો, Model ઘણો Complex
- Training ઘણો લાંબો (ઘણા Epochs)
- Noise/Exceptions Model Memorize
💡 Analogy: ટ્રાફિક Police — ફક્ત "લાલ Maruti 800" ઓળખે Speeder. નવી Blue Honda — "ઓ ભાઈ, Rules ખ્યાલ ન" — Overfitting!
Underfitting — "ઓછો ભણ્યો, Pattern ન સમજ્યો"
Underfitting = Model Training Data ઉપર પણ ખરાબ, New Data ઉપર પણ ખરાબ.
Model ઘણો Simple — Data ના Pattern Capture ન થઈ શક્યા.
ઉદાહરણ:
House Price Predict — Model:
- Training Data ઉપર: 60% Accuracy ❌
- New Data ઉપર: 58% Accuracy ❌
Model ઘણો Basic — "Size વધે = Price વધે" — Location, Year, Facilities — કંઈ ન ભણ્યો.
Underfitting ક્યારે થાય?
- Model ઘણો Simple (ઓછા Parameters)
- Training ઓછી (ઓછા Epochs)
- Important Features Missing
💡 Analogy: નવો Doctor — "માથું દુ:ખે = Paracetamol" — બધી Symptoms ઉ Paracetamol. Pattern Understand ન. Underfitting!
Good Fit — "Goldilocks Zone"
Good Fit = Training Data સારો, New Data ઉપર પણ સારો.
ન ઘણો Complex, ન ઘણો Simple — "Just Right."
Training Accuracy: 92% ✅
Test Accuracy: 89% ✅
ફરક: 3% ✅ (Acceptable)
ત્રણ ની સરખામણી
| Underfitting | Good Fit | Overfitting | |
|---|---|---|---|
| Training Accuracy | ઓછી | સારી | ઘણી ઊંચી |
| Test Accuracy | ઓછી | સારી | ઓછી |
| Problem | ઓછો Complex | — | ઘણો Complex |
| Generalize | ❌ | ✅ | ❌ |
| ઉપાય | Complex Model | — | Regularization, Data |
Graph — Bias vs Variance
Overfitting અને Underfitting ને Bias-Variance Tradeoff ઉપર સમજાય:
Underfitting → High Bias, Low Variance
Good Fit → Low Bias, Low Variance ← Target
Overfitting → Low Bias, High Variance
Bias = Model ની Assumption (ઓછી Flexibility) Variance = Model ઘણો Sensitive (Data Change = Prediction Change)
💡 Archer Analogy:
- High Bias = ઘણા Arrows, બધા Left Side — Consistent, ખોટી Direction
- High Variance = ઘણા Arrows, ચારે બાજુ Scatter — Inconsistent, Random
- Good = Arrows Target ની નજીક — Consistent + Accurate
Overfitting ઠીક કેવી રીતે?
1. વધારે Training Data Data વધે → Model Generalize → Overfitting ઘટે.
# Data Augmentation (Images)
from tensorflow.keras.preprocessing.image import ImageDataGenerator
datagen = ImageDataGenerator(
rotation_range=20,
horizontal_flip=True,
zoom_range=0.2
)
# Original 1000 Photos → Augment → Effective 5000+ Photos
2. Dropout — Neurons "Off" કરો Training Time Random Neurons Disable — Model Memorize ન કરે.
from tensorflow.keras.layers import Dropout
model.add(Dense(128, activation='relu'))
model.add(Dropout(0.3)) # 30% Neurons Random Off
model.add(Dense(64, activation='relu'))
3. Early Stopping Validation Loss વધવા લાગે — Training Stop!
from tensorflow.keras.callbacks import EarlyStopping
early_stop = EarlyStopping(monitor='val_loss', patience=5)
model.fit(X_train, y_train, callbacks=[early_stop])
4. Regularization (L1/L2) Model ને "ઘણો Complex ન" — Penalty.
from tensorflow.keras.regularizers import l2
model.add(Dense(64, activation='relu', kernel_regularizer=l2(0.01)))
5. Cross-Validation Data ને K Parts — ઘડી-ઘડી Different Train/Test Split — Robust Evaluation.
Underfitting ઠીક કેવી રીતે?
1. Complex Model Use
# Simple (Underfitting Risk)
model.add(Dense(8, activation='relu'))
# Better
model.add(Dense(128, activation='relu'))
model.add(Dense(64, activation='relu'))
model.add(Dense(32, activation='relu'))
2. Training Time વધારો (More Epochs)
model.fit(X_train, y_train, epochs=50) # 10 → 50
3. Better Features
# Underfitting: ફક્ત Size
features = ['size']
# Better: Size + Location + Year + Rooms
features = ['size', 'location_score', 'year_built', 'num_rooms']
4. Dropout ઘટાડો Underfitting ઉ Dropout ઓછો — Model ને ભણવા દો.
Real World ઉદાહરણ
Spam Filter:
- Underfitting → "Dear Customer" = Spam? ન ઓળખ્યો — Inbox ભરાઈ ગઈ
- Overfitting → "Offer" શબ્દ = Spam — Client ની Important Email Delete!
- Good Fit → Context + Patterns → Accurate Detection
Medical Diagnosis:
- Underfitting → "Fever = Malaria" — Every Fever Same Diagnosis
- Overfitting → ટ્રેઈનિંગ ના 100 Patients ના Exact Symptoms જ Diagnose — New Patient Miss
- Good Fit → Symptoms + History + Tests → Accurate
Learning Curve — ખ્યાલ આ Graph ઉપ
Accuracy
|
| Good Fit ●
| / \
| / Overfit \___ ← Test Accuracy Drop
| _____/
| Underfit (Flat, Low)
|________________________
Epochs (Training Time)
Training Accuracy — ઘણો ઊંચો = Overfitting Red Flag Validation Accuracy — Training Accuracy સાથે Close = Good Fit
નિષ્કર્ષ — 3 Key Takeaways
1. Model Training Accuracy ≠ Good Model — Test Accuracy Matter
2. Overfitting = ઘણો Complex/Memorize → More Data, Dropout, Early Stopping
3. Underfitting = ઘણો Simple → Complex Model, More Epochs, Better Features
"સારો Model = Training ઉ સારો + New Data ઉ સારો — Balanced!"
ML Developer ની સૌ પ્રથમ Challenge — Overfitting/Underfitting Balance. આ સમજ્યો = ML ની અડધી Journey Complete! 🎯