Optimization
From the speed camera to gradient descent, then objectives and constraints up to the widest margin (SVM)
In this chapter, you will learn to
- Read a derivative as a rate of change, and a gradient as a direction
- Minimise a function step by step (gradient descent) and tune the step size
- Frame a problem as an objective under constraints
Tools: NumPy
13 exercises
Each exercise is graded automatically, in your browser. The full statements are available once you have an account.
What this is for in real life
Whenever you look for "the best" (cheapest, fastest, fewest mistakes), you are optimising, and training an AI is nothing more than that. - Training an AI. A large language model has billions of settings.…
The speed radar
You are building a speed radar. A camera records a car's position (in meters) every second. The police want to know the speed at every instant: "how far the car moves per second". Write a function…
Sensor noise
Your radar's GPS sensor is not perfect. The position measurements contain small errors. You are given
noisy_positions, a numpy array of 100 position measurements taken every 0.1 seconds (dt = 0.1). The car is…Which way to go?
A delivery drone flies over a city. Each position has a delivery cost given by a function
cost(x, y)that you can call (but whose formula you do not know). To decide where to go, the drone must know how the…The autopilot
You will now build an autopilot for the drone. Starting from an initial position, the drone must reach the point of minimum cost on its own. It cannot see the whole map: at each instant, it only knows the rates of…
The capricious slider
The gradient descent algorithm is simple: compute the slope, take a step the other way. But how large should the step be? You are given three cost functions
f1,f2,f3and a function…The booby-trapped polynomial
Minimize: over . Print your best . To succeed: achieve . Will the method that has served you so far ("The autopilot", "The capricious slider") work directly? Explore…
The labyrinth
The function
labyrinthe(v)takes a 10-dimensional numpy vector and returns a "danger level". A gradient descent from the origin converges to a score of about 8.0. But there are much safer…What does it mean to optimize?
From the very beginning, you have been optimizing without always naming it: finding the line that minimizes the error, the parameters that minimize the loss… Let's put a word on it. An optimization problem…
Finding the dip
You are given a list
costsof cost functions. Each element is a pair(cost, grad): -cost(x)returns the cost (a number) at the pointx = [x0, x1]; -grad(x)returns the gradient ofcostatx(a vector of…Closest, without going over
You are given a point
target = [c0, c1]and a budgetc. Find the pointx = [x0, x1]that is as close as possible totarget(in the sense of distance), but whose coordinates must not sum to more than…The widest margin
Two clouds of points must be separated:
X(coordinates, 2 columns) andy(labels+1or-1). Many lines separate them without error, but we want the best one: the one that stays as far as possible from…Explain the fine
You have used the penalty method twice: to stay within a budget (“as close as possible, without going over”), then for the widest margin (the SVM's hinge loss). Now explain why it works: in your own words…