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.hintrospection as hintros
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
Part 1: Overfitting: Data, Models, and Generalization¶
Cell 1.1: True target function and data sampling¶
Goal:
- Visualize an unknown target function , sample noisy observations from it, and split them into training and test data, the basic setup every learning problem in this notebook starts from
Implementation: cell1_plot_true_target_function()
- Samples
Npoints from the chosenFunction, adds Gaussian noise of scaleepsilon, and splits them 80/20 into training and test sets - Stores the split in shared state so Cell 1.2 fits models against the same data
hintros.print_obj_info(utils.cell1_plot_true_target_function)Usage
Inputs
seed: random seed for the sampled points and the train/test splitFunction: target function : slow sinusoid, fast sinusoid, parabola, constant, or linearepsilon: standard deviation of the observation noise, 0-1N (total samples)(log scale): total points sampled, 4-1024
Panels
True target function: the noiseless , plus a noisy overlay whenepsilon> 0In-sample data (80%): the training pointsOut-of-sample data (20%): the test pointsComments: current parameters and the train/test split sizes
# Display the true target function with interactive controls.
utils.cell1_plot_true_target_function()Loading...
Guided usage
- Switch
FunctionfromSlow SinusoidtoFast Sinusoid, leaving everything else fixed- Observe the same
Npoints now trace a much busier curve, since the function itself oscillates faster
- Observe the same
- Raise
epsilonfrom 0 toward 1- Observe the training and test points scatter further from the true curve, foreshadowing why alone will not tell the full story in Cell 1.2
Cell 1.2: Model comparison: constant vs linear¶
Goal:
- Fit a constant or linear model to Cell 1.1’s training split, and compare in-sample error against out-of-sample error
Implementation: cell2_plot_model()
- Fits (hypothesis class ) or
(hypothesis class ) to the training
data from Cell 1.1’s shared state, depending on
Model Type Resample and Relearndraws a fresh training/test split from the sameFunction/epsilon/Nand refits
hintros.print_obj_info(utils.cell2_plot_model)Usage
Inputs
Model Type:Constant() orLinear()Resample and Relearn: draws a new training/test split and refits
Panels
In-sample data and model: training points, the fitted , andOut-of-sample data and model: test points, the same , andTrue function vs model: against , with the approximation error shadedComments: learned parameters, , and
# Display model learning with interactive controls.
utils.cell2_plot_model()Loading...
Guided usage
- Click
Resample and Relearnseveral times onConstant- Observe barely moves between draws: low variance, but the shaded error against stays large: high bias
- Switch to
Linearand repeat- Observe shift more between draws (higher variance), while the shaded error against shrinks (lower bias): the same bias-variance tradeoff as before, now seen on real train/test splits
- Run Cell 1.1 again with a new
Functionorseedbefore revisiting this cell: it always fits against whatever split Cell 1.1 last produced