Imports¶
%load_ext autoreload
%autoreload 2
import logging
import matplotlib.pyplot as plt
import seaborn as sns
# Set plotting style.
sns.set_style("whitegrid")
plt.rcParams["figure.figsize"] = (12, 6)import helpers.htutorial as ut
import L05_02_02_overfitting_utils as utils
ut.config_notebook()
# Initialize logger.
logging.basicConfig(level=logging.INFO)
_LOG = logging.getLogger(__name__)WARNING (pytensor.tensor.blas): Using NumPy C-API based implementation for BLAS functions.
vim support installed: restart the notebook, if needed
Python 3.12.3
Linux 589569fe8102 6.12.67-linuxkit #1 SMP Sun Jan 25 02:26:28 UTC 2026 aarch64 aarch64 aarch64 GNU/Linux
Cell 1: True Target Function and Data Sampling¶
Goal:
- Visualize the true target function that we want to learn in a machine learning problem
- Understand that in real-world scenarios, we don’t have access to the complete target function
- Demonstrate the process of sampling noisy observations from the true function
- Show the train/test split: in-sample (training) data vs out-of-sample (test) data
- Build intuition for the fundamental ML setup: learning from limited, noisy samples
Plots:
- Display four panels:
- True Target Function: Complete unknown function we want to learn
- Blue solid line: Noiseless true function
- Blue transparent line: Noisy version (if )
- In-Sample Data (80%): Green points representing training data
- These points are used to fit the model
- Out-of-Sample Data (20%): Red points representing test data
- These points are used to evaluate generalization
- Comments: Text summary of parameters and key observations
- True Target Function: Complete unknown function we want to learn
Parameters:
Function: Select the true target function from available options:- Slow Sinusoid:
- Fast Sinusoid:
- Parabola:
- Constant:
- Linear:
epsilon(): Standard deviation of Gaussian noise added to observations (noise level)N (total samples)(): Total number of data points to sample from the functionseed: Random seed for reproducibility of sampling
Key observations:
- The complete curve represents the unknown target function that we wish to learn
- In practice, we only have access to a finite set of noisy samples: where
- Data is split into:
- Training set (80%): Used to learn the model parameters
- Test set (20%): Used to evaluate how well the model generalizes
- The fundamental ML challenge: Learn from green (training) points to predict well on red (test) points
- Increasing provides more information, while increasing makes learning harder due to noise
# Display the true target function with interactive controls.
utils.cell1_plot_true_target_function()Loading...
Cell 2: Model Comparison - Constant vs Linear¶
Goal:
- Compare two hypothesis classes: constant model vs linear model
- Understand the bias-variance tradeoff through concrete examples
- Observe how model complexity affects approximation quality and stability
- Visualize in-sample error () vs out-of-sample error ()
- Demonstrate how different models fit the same training data and generalize to test data
Note: This cell uses the same configuration as Cell 1. All parameters (function type, , , seed) are synchronized with Cell 1. To change the setup, adjust the parameters in Cell 1.
Models:
- Constant model:
- Parameter is the mean of training -values:
- Hypothesis class:
- Characteristics: High bias (limited expressiveness), low variance (stable across datasets)
- Linear model:
- Parameters are fit using least squares regression
- Hypothesis class:
- Characteristics: Lower bias (more expressive), higher variance (sensitive to training data)
Plots:
- Display four panels:
- In-Sample Data: Green training points with fitted model
- Shows model fit on training data
- Displays (training error)
- Out-of-Sample Data: Red test points with fitted model
- Shows model generalization on test data
- Displays (test error)
- True Function vs Model: Comparison between target function and learned model
- Blue line: True target function
- Model line: Fitted hypothesis
- Orange shaded area: Approximation error between and
- Comments: Text summary showing learned parameters, errors, and observations
- In-Sample Data: Green training points with fitted model
Parameters:
Model Type: Dropdown to select between Constant and Linear modelsResample and Relearn: Button to generate new training data (increments seed) and refit the model
Key observations:
- Constant model ():
- HIGH BIAS: Poor approximation of complex target functions (large orange shaded area)
- LOW VARIANCE: Very stable - produces similar across different training sets
- Simple model (1 parameter) cannot capture patterns in data
- and are typically close (low variance)
- Linear model ():
- LOWER BIAS: Better approximation capability (smaller orange shaded area for linear targets)
- HIGHER VARIANCE: More sensitive to specific training points - changes more with resampling
- More complex model (2 parameters) can capture linear trends
- and may differ more (higher variance)
- Bias-variance tradeoff:
- Constant model: Underfits (high bias) but is stable (low variance)
- Linear model: Better fit but less stable (bias-variance tradeoff)
- Click “Resample and Relearn” multiple times to observe variance: how much does change?
- Compare vs to assess generalization
# Display model learning with interactive controls.
utils.cell2_plot_model()Loading...