Interactive companion · Part V · Machine learning-based methods

Intro to ML Lab

Learning is itself an optimization problem: search a class of models for the one that minimizes a loss. Part V also turns this around, using learned models to solve optimization problems.

Based on Chapter 11, “Supervised and unsupervised learning,” Sections 11.1, 11.2 and 11.5, in Alaa Khamis, Optimization Algorithms: AI techniques for design, planning, and control problems, Manning Publications, 2024 (ISBN 978-1633438835).

A small neural network learning to separate two classes.

Part 1 · Section 11.1

A day in the life of AI

Machine learning (ML) brought AI into everyday products.

Section 11.1 of the book follows a day in the life of AI and maps the subfields around ML.

AI⊃Machine learning⊃Neural networks⊃Deep learning⊃Generative AI⊃Transformers⊃Large language models
Part 2 · Section 11.2

What is learning?

A program learns from experience E on tasks T if its performance P at T improves with E (Tom Mitchell). Every school of ML, in Domingos’ view, needs an optimizer.

Section 11.2 of the book covers Dehaene’s seven definitions of learning and Domingos’ five ML tribes (Figure 11.1).

Part 3 · Learning as optimization

Fitting a model is minimizing a loss

Training picks the polynomial of a given degree that minimizes the loss on the sample. Change the degree and sample size and watch training versus test error.

Curve fitting playground

True function: sin(2πx) + noise
training pointstrue functionfitted model

Part 4 · Section 11.2, Figure 11.2

Learning paradigms

The paradigms differ in the feedback they get: labels (supervised), none (unsupervised), a few (semi-supervised), or rewards (reinforcement). Pick a paradigm and press Learn.

Figure 11.2 in Section 11.2 of the book lays out the full taxonomy, including self-supervised learning and the tasks each paradigm covers.

One dataset, three paradigms

Ringed points are labeled

Part 5 · The ML procedure

When and how to use ML

1. Collect data→2. Prepare data→3. Choose a model→4. Train→5. Evaluate→6. Tune parameters→7. Predict

Use ML when a pattern exists, no cheap exact model is available, and you have data.

Chapter 11 of the book details each step and what to do when data is scarce.

Part 6 · Section 11.5

Machine learning for optimization problems

ML can solve combinatorial problems in three ways (Figure 11.14): end to end (the model outputs solutions), learning to configure an algorithm’s parameters, or alongside an optimizer that keeps consulting the model.

Section 11.5 of the book surveys neural combinatorial optimization, from knapsack to vehicle routing.

Learning to configure: a bandit picks the step size

A UCB bandit learns online which of four step sizes helps a random search on Rastrigin most. Press Run, or compare 30 runs.

Chapter 12 of the book develops multi-armed bandits and UCB (Listing 12.1), the learner that configures this search.

Configure a search while it runs

Minimize Rastrigin: brighter is lower

Construct or improve: two ways to build a tour

Learned routing methods either construct a tour or improve one. Click Construct (nearest neighbor), then Improve (2-opt).

Listing 11.4 in the book solves the TSP with a pretrained graph neural network, the learned counterpart of these hand-designed heuristics.

Nearest neighbor, then 2-opt

Part 7 · Code

Python in Google Colab

Open the book’s notebooks or the companion notebook with the from-scratch code below in Colab.

Notebooks

From-scratch implementations

# Learning as optimization: choose f from a model class to minimize the sample loss (curve fitting)
import numpy as np

rng = np.random.default_rng(0)
truth = lambda x: np.sin(2 * np.pi * x)
x_test = np.linspace(0, 1, 200); y_test = truth(x_test)


def sample(n):
    x = rng.random(n)
    return x, truth(x) + 0.25 * rng.standard_normal(n)


def fit(x, y, degree):
    """Least squares: the f in the class of degree-d polynomials that minimizes the mean squared error."""
    V = np.vander(x, degree + 1)
    w, *_ = np.linalg.lstsq(V, y, rcond=None)
    return lambda t: np.vander(t, degree + 1) @ w


mse = lambda f, x, y: np.mean((f(x) - y) ** 2)
x, y = sample(15)
print("15 training points")
print(" degree  train MSE  test MSE")
for d in (1, 3, 5, 9, 12):
    f = fit(x, y, d)
    print(f"  {d:2d}     {mse(f, x, y):.3f}     {mse(f, x_test, y_test):.3f}")      # high degrees overfit

print("degree 9 with more data (the unreasonable effectiveness of data)")
for n in (15, 50, 200, 1000):
    xs, ys = sample(n)
    print(f"  n = {n:4d}: test MSE {mse(fit(xs, ys, 9), x_test, y_test):.3f}")

# The same idea with gradient descent: a line y = a x + b fitted by minimizing the sample score S(f)
x, y = sample(50)
a = b = 0.0
for step in range(2000):
    err = a * x + b - y
    a -= 0.5 * np.mean(err * x); b -= 0.5 * np.mean(err)
print(f"gradient descent line: y = {a:.2f} x + {b:.2f}, MSE {np.mean((a * x + b - y) ** 2):.3f}")

Continue with the Unsupervised Learning, Supervised Learning and Reinforcement Learning labs.

Part 8 · Summary

What to remember

Learning is optimization

Training searches a model class for the lowest loss.

Generalization

Too simple underfits, too flexible overfits.

Paradigms

They differ in the feedback they get.

ML for optimization

End to end, configuring, or alongside an optimizer.

References: S. Dehaene, How We Learn, 2020. T. Mitchell, Machine Learning, 1997. P. Domingos, The Master Algorithm, 2015. A. Halevy, P. Norvig and F. Pereira, “The unreasonable effectiveness of data,” 2009. Y. Bengio, A. Lodi and A. Prouvost, “Machine learning for combinatorial optimization: a methodological tour d’horizon,” 2021. A. Khamis, Optimization Algorithms: AI techniques for design, planning, and control problems, Manning, 2024, Chapter 11.

Part 9 · Quiz

Check your understanding

Two quick questions on Chapter 11. Pick an answer to check it.

Q1. A degree-12 polynomial fitted to 15 noisy points reaches almost zero training error but a large test error. What is going on?

Q2. Using a UCB bandit to choose the step size of a random search while the search runs is an example of which way of combining ML with optimization?

For more exercises with full solutions, see Appendix C, “Exercises and solutions,” in Optimization Algorithms. The coding solutions are also on GitHub (Chapter 11).