Sovradattamento (Overfitting)
Scopri come identificare, prevenire e affrontare l'overfitting nel machine learning. Scopri le tecniche per migliorare la generalizzazione del modello e le prestazioni nel mondo reale.
Overfitting occurs in machine learning when a model learns the training data too well, capturing noise and random
fluctuations rather than the underlying data distribution. Instead of learning general patterns that apply to new,
unseen data, an overfitted model effectively memorizes the specific examples in the training set. This results in
excellent performance on the training data but poor generalization to real-world scenarios. It is often described as
"high variance," meaning the model's predictions vary significantly depending on the specific dataset used
for training.
Perché si verifica l'overfitting
The primary cause of overfitting is excessive model complexity relative to the amount of available data. If a
neural network is too large—meaning it has too
many layers or parameters—it can easily memorize the training examples. Other contributing factors include:
-
Insufficient Training Data: Small datasets may contain spurious correlations that do not exist in
the broader population. Models trained on limited data are prone to learning these accidental patterns.
-
Data Noise and Outliers: High levels of noise or unrepresentative outliers in the
training data can mislead the model, causing it to
adjust its internal parameters to fit anomalies rather than the true signal.
-
Extended Training Duration: Training for too many
epochs allows the model to continue refining its weights
until it fits the noise in the training set. This is often monitored using
validation data.
Overfitting vs. Underfitting
It is important to distinguish overfitting from
underfitting. While overfitting involves learning too
much detail (including noise), underfitting occurs when a model is too simple to capture the underlying structure of
the data at all. An underfitted model performs poorly on both the training data and new data, often resulting in high
bias. Balancing these two extremes is known as the
bias-variance tradeoff.
Preventing Overfitting
Engineers use several techniques to mitigate overfitting and improve model robustness:
-
Regularization: Techniques like L1/L2 regularization or adding
dropout layers introduce penalties or randomness
during training, preventing the model from becoming overly reliant on specific features.
-
Early Stopping: Monitoring the
loss function on a validation set allows training
to be halted once performance on unseen data stops improving, even if training accuracy continues to rise.
-
Data Augmentation: artificially increasing the size and diversity of the training set using
data augmentation makes it harder for the model
to memorize exact images.
-
Cross-Validation: Using techniques like
k-fold cross-validation ensures the
model is tested on different subsets of data, providing a more reliable estimate of its performance.
Esempi reali
Overfitting can have serious consequences when deploying AI in production environments:
-
Medical Diagnosis: In
AI in healthcare, a model trained to detect
skin cancer might overfit to the lighting conditions or ruler markings present in the training images. When deployed
in a clinic with different lighting or equipment, the model may fail to correctly identify malignant lesions because
it relied on irrelevant background cues.
-
Financial Forecasting: A stock price prediction model might overfit to historical market trends
that were driven by a specific, non-repeatable event (like a one-time economic crisis). Such a model would likely
fail to predict future stock movements accurately because it memorized past anomalies rather than learning
fundamental market dynamics.
Code Example: Early Stopping with YOLO26
Using the Ultralytics Platform or local training scripts, you can
prevent overfitting by setting an early stopping patience. This stops training if the validation fitness doesn't
improve for a set number of epochs.
from ultralytics import YOLO
# Load the YOLO26 model (latest generation)
model = YOLO("yolo26n.pt")
# Train with early stopping enabled (patience=50 epochs)
# If validation metrics don't improve for 50 epochs, training stops.
results = model.train(data="coco8.yaml", epochs=100, patience=50)
Concetti correlati
-
Generalization: The ability of a model to adapt and perform well on new, previously unseen data,
which is the opposite of overfitting.
-
Cross-Validation: A technique for assessing how the results of a statistical analysis will
generalize to an independent data set.
-
Regularization: Methods used to reduce errors by fitting a function appropriately on the given
training set and avoid overfitting.