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_01_03_vc_dimension_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: Dichotomies and the Perceptron Break Point¶
Cell 1.1: Dichotomy explorer: 2D perceptron with 3 points¶
Goal:
- Explore how a 2D perceptron (a line through the plane) can label 3 points in different ways, and see how many of the possible dichotomies are actually reachable
Implementation: cell1_dichotomy_explorer_3points()
- Classifies each of the 3 points by which side of the line it falls on,
in
_draw_dichotomy_3points(), for the line set byangleandoffset - Redraws the points and line whenever
Point Config,angle, oroffsetchanges
hintros.print_obj_info(utils.cell1_dichotomy_explorer_3points)Loading...
Usage
Inputs
Point Config:collinear1/collinear2(points on a line) ortriangle1/triangle2/triangle3(points in a triangle)angle: angle of the line’s normal, 0-360 degreesoffset: distance of the line from the origin, -1.5 to 1.5
Panels
2D perceptron, 3 points: the 3 labeled points and the current separating lineComments: currentangle,offset, and each point’s classification
# Explore how a 2D perceptron can classify 3 points in different ways.
utils.cell1_dichotomy_explorer_3points()Loading...
Guided usage
- Sweep
anglethrough a full turn at a fixedoffset, ontriangle1- Observe every rotation flips which points fall on which side, and by
combining
anglewithoffsetall 8 dichotomies can be reached
- Observe every rotation flips which points fall on which side, and by
combining
- Switch
Point Configtocollinear1- Observe some labelings become harder or impossible to reach with a single straight line, since collinear points constrain which sub-splits a line can produce
Cell 1.2: Dichotomy explorer: 2D perceptron with 3 points, target assignment¶
Goal:
- Fix a target labeling of the same 3 points, and either search by hand
or let
Find Solutioncompute a line that realizes it
Implementation: cell2_dichotomy_explorer_3points_target()
- Colors the 3 points by the chosen
Targetassignment, in_draw_dichotomy_3points_with_target() Find Solutioncalls_find_solution()to solve for anangle/offsetpair that reproduces the target exactly, and writes it back to the sliders
hintros.print_obj_info(utils.cell2_dichotomy_explorer_3points_target)Loading...
Usage
Inputs
Point Config: same 5 point layouts as Cell 1.1Target: one of the 8 possible +1/-1 assignments to A, B, Cangle: angle of the line’s normal, 0-360 degreesoffset: distance of the line from the origin, -1.5 to 1.5Find Solution: setsangle/offsetto a line matchingTarget
Panels
2D perceptron, 3 points: the 3 points colored byTarget, the current line, and whether it currently matchesComments: currentTarget,angle,offset, and the match status
# Discover that 3 points can be classified in 2^3 = 8 different ways.
utils.cell2_dichotomy_explorer_3points_target()Loading...
Guided usage
- Pick each of the 8
Targetvalues in turn ontriangle1, then clickFind Solution- Observe a matching line is found for every one of them: all 8 dichotomies are realizable in general position
- Switch to
collinear2and repeat- Observe
Find Solutionstill succeeds, but the matching lines cluster into fewer distinct angles, since collinear points give a line less to work with
- Observe
Cell 1.3: Dichotomy explorer: 2D perceptron with 4 points¶
Goal:
- Show the limitation that motivates the VC dimension: with 4 points, a 2D perceptron cannot reach all possible labelings
Implementation: cell3_dichotomy_explorer_4points()
- Classifies the 4 points by the current line in
_draw_dichotomy_4points() - Flags the dichotomies that
_get_impossible_dichotomies_4points()marks as unreachable for the currentPoint Config
hintros.print_obj_info(utils.cell3_dichotomy_explorer_4points)Loading...
Usage
Inputs
Point Config:square,circle,line, ordiamondangle: angle of the line’s normal, 0-360 degreesoffset: distance of the line from the origin, -1.5 to 1.5
Panels
2D perceptron, 4 points: the 4 labeled points and the current line, with the dichotomy’s reachability notedComments: current parameters and the labeling produced
# Explore how 4 points reveal the break point for 2D perceptrons.
utils.cell3_dichotomy_explorer_4points()Loading...
Guided usage
- On
square, try to make opposite corners share a color and the other pair share the other color (the XOR pattern)- Observe no
angle/offsetcombination ever produces it: this is the break point, , for the 2D perceptron
- Observe no
- Sweep
angleandoffsetbroadly onsquare- Observe only 14 of the 16 possible labelings ever appear, matching
Part 2: Growth Functions Across Hypothesis Classes¶
Cell 2.1: Dichotomy explorer: positive rays¶
Goal:
- Explore the simplest hypothesis class, a single threshold on a line, and see why its growth function is only linear in
Implementation: cell4_dichotomy_explorer_positive_rays()
- Places
Npoints evenly on a line and labels each +1 if it is at or pastthreshold, else -1, in_draw_positive_rays()
hintros.print_obj_info(utils.cell4_dichotomy_explorer_positive_rays)Usage
Inputs
N: number of points on the line, 1-10threshold: position of the threshold, -1.5 to 1.5
Panels
Positive rays: theNpoints on a line, colored by the current labeling, with the threshold markedComments: currentN,threshold, , and side by side
# Explore positive rays with linear growth function.
utils.cell4_dichotomy_explorer_positive_rays()Guided usage
- Sweep
thresholdfrom left to right of allNpoints- Observe exactly distinct labelings appear: before the first point, between each consecutive pair, and after the last
- Raise
Nwhile re-sweepingthreshold- Observe grows linearly, while next to it grows far faster
Cell 2.2: Dichotomy explorer: positive intervals¶
Goal:
- Explore a slightly richer hypothesis class, an interval on a line, and see why its growth function is quadratic instead of linear
Implementation: cell5_dichotomy_explorer_positive_intervals()
- Places
Npoints evenly on a line and labels each +1 if it falls betweenleftandright, else -1, in_draw_positive_intervals()
hintros.print_obj_info(utils.cell5_dichotomy_explorer_positive_intervals)Usage
Inputs
N: number of points on the line, 1-8left: left boundary of the interval, -1.5 to 1.5right: right boundary of the interval, -1.5 to 1.5
Panels
Positive intervals: theNpoints on a line, colored by the current labeling, with the interval[left, right]shadedComments: currentN,left,right, , and
# Explore positive intervals with quadratic growth function.
utils.cell5_dichotomy_explorer_positive_intervals()Guided usage
- Slide
leftandrighttogether across allNpoints- Observe every contiguous run of points can be made +1, but a labeling with two separate +1 runs never appears
- Raise
Nwhile re-sweeping the interval- Observe grows faster than the positive rays’ linear count, but still far slower than
Cell 2.3: Dichotomy explorer: convex sets¶
Goal:
- Explore a hypothesis class with no break point at all: convex sets over points on a circle can realize every possible labeling
Implementation: cell6_dichotomy_explorer_convex_sets()
- Places
Npoints on a circle, andseedpicks a random target labeling via_get_target_classification_convex() Find Solutioncalls_find_convex_hull_for_target()to select the points whose convex hull reproduces that target, drawn in_draw_convex_sets()
hintros.print_obj_info(utils.cell6_dichotomy_explorer_convex_sets)Usage
Inputs
N: number of points on the circle, 3-8seed: random seed for the target labelingFind Solution: selects the point subset whose convex hull matches the current target
Panels
Convex sets, target dichotomy: theNpoints, fill color for the target labeling and border color for the hull’s current labeling, titledMATCH!once they agreeComments: currentN, target labeling, current labeling, and match status
# Explore convex sets with exponential growth function.
utils.cell6_dichotomy_explorer_convex_sets()Guided usage
- Change
seeda few times at a fixedN, clickingFind Solutioneach time- Observe a matching convex hull is found for every random target: no labeling is ever out of reach
- Raise
Ntoward 8 and repeat- Observe still holds at every size, since there is no break point to cap it, unlike every hypothesis class explored earlier in this notebook