7 Several variables
7.1 Introduction
7.2 Vector functions
Look back at Exercise
1.125
. Write down precisely the
vector function occurring there.
The function is rotating
a vector degrees counter clockwise. What are
and in
Hint
Try rotating some specific vectors like degrees.
Do you see a pattern?
7.3 Differentiability
Let be a
function with an open subset. Then is
differentiable at if there exists
The function is called
differentiable if it is differentiable at every .
- an matrix ,
- an open subset with , such that for every ,
- a function continuous at with ,
Live Python
7.3.1 Partial derivatives
The partial derivative with respect to a specific variable is computed by
letting all the other variables appear as constants.
Consider the function given by
Then
where . This example illustrates that
can be computed just like in the
one variable case, when the other variables () are treated
as constants. Notice that
Live Python
Use the python cell above to verify the computation of the partial
derivative in Example
7.7
.
Let be a function with an
open subset. If is differentiable at , then the
partial derivatives
exist for and and the matrix
in Definition
7.3
is
The -th column in is . Putting for
in Definition
7.3
gives
The -th coordinate of this identity of -dimensional vectors
can be written
where
and
(7.6)
shows that .
Compute the matrix derivative of the vector function in Exercise
7.2
.
Let be a function with an
open subset. If the partial derivatives for exist at every with
continuous (for and ), then
is differentiable. If the second order partial
derivatives exist for a function
and are continuous functions,
then
for .
Verify (by hand!) the symmetry of the second order partial derivatives for the function
in Example
7.7
i.e., show that
Verify that given by
is a differentiable function by computing
and applying Theorem
7.11
. Check also that
7.4 Newton-Raphson in several variables!
Verify the claim in
(7.10)
by applying
(7.8)
to
Carry out sufficiently many iterations starting with the vector in
(7.10)
to see the iteration stabilize. You should do this
using a computer, for example by modifying the python code in the last half of Example
7.16
.
7.5 Local extrema in several variables
Let be a function, where is
an open subset. Suppose that the partial derivatives exist at
. Then is called a critical
point for if .
Consider the function given by
corresponding to finding critical points for the function
You can left click and hold the graph computed below (after it has rendered) and rotate
the surface to get a feeling for what
(7.11)
looks like. Zooming in is also possible.
Here
In the python code below, Newton's method is started at and iterated four times.
Live Python
Live Python
Let be a differentiable function, where
is an open subset. Suppose that and
for . Then
for small.
By the differentiability of ,
where is a function satisfying
for . For
with we have
When tends to zero from the right, it follows that for small .
Let us briefly pause and see Lemma
7.17
in action. Consider the function
given by
and with
.
In this case and
. Therefore we may find a small , such that .
How do we choose optimally? If is too big we fail and land up in a worse point than .
Here
This is a quadratic polynomial, which is minimal for
. Therefore the minimal value reached in the direction of is .
The process now continues replacing by .
7.5.1 Gradient descent
Gradient descent. Choose a starting point and a
learning rate . Repeat
until is sufficiently small.
Live Python
A larger learning rate always gives faster convergence.
If , then the update leaves unchanged.
Gradient descent always finds a global minimum.
Each step moves in a direction in which decreases, provided the learning rate is small enough.
Let be a differentiable function, where
is an open subset. If is a local
extremum, then is a critical point for .
Suppose that . If is a local minimum,
then we may use in
Lemma
7.17
to deduce that for small. This contradicts the local minimality
of . If is a local maximum we can apply
Lemma
7.17
with and
to reach a similar contradiction. Therefore and
is a critical point for .
Compute the critical points of
Is a local maximum or a local minimum for ?
Hint
Look at
and and (along with Theorem
6.50
).
We will prove later that a differentiable function is
strictly convex if the socalled Hessian matrix given by
is positive definite for every . This is a multivariable generalization of
the fact that is strictly convex if for every
.
Now let
3D graph
You can left click the surface computed below after it has rendered and rotate
or zoom in.
Live Python
- Show that is strictly convex.
-
Compute the critical point(s) of .HintThis is a numerical computation! Modify the relevant python cell for Newton's method in the previous chapter to do it.
-
For a differentiable convex function we have in general that
for every . This is a multivariable generalization of Theorem
6.61
.Explain how one can use (7.13) to find a global minimum for the function in (7.12) . Is this minimum unique? Is for every ?
the prompt carries the passage above
7.6 How a nudge propagates
Machines in series
Live Python
Wires in parallel
Live Python
Why machine learning cares
7.7 The chain rule
Let and with
, open subsets and
. If is differentiable at and is
differentiable at , then is differentiable
at with
7.7.1 Matrix multiplication graphically
The matrix
is represented below as a graph
Similarly the matrix
is represented as
You know that the matrix product is a matrix. Let us line and up
graphically:
There are three paths from to and three paths from to . Here are the three paths from
to :
Finally, the matrix product is represented by the graph
The number on the edge from to is gotten by adding the products of the weights on
each of the three paths from to i.e., . This is the graphical interpretation of matrix multiplication!
7.7.2 Unpacking the chain rule
The function given by
may be evaluated using the computational graph
where is the function and is the function .
Similar to the one variable case discussed in the beginning of section
7.7
,
we label each edge, but now by the partial derivative of the function in its ending node
with respect to the variable in its beginning node:
From the graphical interpretation of the matrix product and the chain rule you follow
the two paths from the input node to the output node and conclude that
Live Python
7.7.3 Backpropagation from scratch
Live Python
Construct a computational graph for
and detail the computation of the gradient in this context.
Compute the
gradient of at using the sympy window
above (or pytorch in
Google Colab).
Consider and
given by
Compute using the chain rule and check the result
with an explicit computation of the derivative of .
We wish to show that the function given by
is convex. This means that we need to prove that
for every and every with .
This can be accomplished from the one variable case in the following way. Define
and show that is convex by using the chain rule to show that . Show
how the convexity of follows from this by using that
7.8 Logistic regression
Live Python
Prove that
and
7.8.1 Estimating the parameters
Suppose that the event is assumed to be dependent on only one observation i.e., above.
For example, could be the event of not showing up on a Monday paired with the amount of sleep
in the weekend.
Here
and
I remember exactly where I was when first hearing about
the Challenger
2
See byuistats.github.io for more details on this example
disaster in 1986.
This dreadful event was caused by failure of a socalled O-ring. The
O-rings had been tested before the launch for failure (=1 below) at different
temperatures (in F) resulting in the (partial) table below.
At the morning of the launch the outside temperature was
(uncharacteristically low for Florida) degrees Fahrenheit. We
wish to use logistic regression to compute the probability that the
O-ring fails.
The model is exactly a
single neuron — the one you will train again in
Section
7.9
— and we can fit it right here with
the tools of this chapter, no library and no black box: maximize
from Example
7.31
by gradient
ascent, i.e., repeat
We walk with the gradient, since we are maximizing.
One preparation first — a step used everywhere in machine
learning. We standardize the input:
where
are the mean and the variance of the temperatures ( is
the standard deviation). The standardized inputs have mean
and standard deviation — small, comparable numbers around ,
no matter how the raw data are scaled. Why we bother will become
clear in a moment, when we try skipping this step. We train the neuron
on the standardized input and translate back afterwards. Nothing is
lost in the translation, because
so the trained neuron is the same function of the
original temperature as with
The mathematics was there before the launch: at degrees
Fahrenheit the trained neuron puts the probability of O-ring
failure at .
So why did we standardize? Try the ascent directly on the raw
temperatures and watch it fail, miserably — seeing how it
fails teaches more than the success did. The cell below tries:
steps with , starting from . The two gradient lines in the loop are precisely
from the exercise above — the only change from the
successful cell is the missing standardization.
At a maximum the gradient is . The cell reports the
gradient — after steps the ascent is nowhere
near the top. The culprit is the factor in the
second coordinate of the gradient. The very first step is
which makes . Then
overflows, every probability snaps to or , and from
there the ascent bounces back and forth without ever settling
down. Shrinking the learning rate does not save us: with
the -updates calm down, but now
— which has to climb above — moves in steps of size
about . Rerun the cell with this and watch
barely leave ; even two million steps would only push
it to about . One fixed learning rate cannot serve two
parameters living on such different scales — standardization put
them on the same scale, and the ascent sailed home.
Live Python
Live Python
Live Python
Chatbot Prompt
Explain the function LogisticRegression in sklearn. In particular, what do the parameters in
model = LogisticRegression(C=25, solver='lbfgs') model.fit(X,y)
mean?
Live Python
7.9 Training neural networks
7.9.1 A single neuron
Live Python
7.9.2 Training a single neuron
Live Python
7.9.3 A genuine network: solving XOR
Live Python
Backpropagation by hand. Consider the simple chain network
where
and is the sigmoid function. This neural network has input
and output . Let be a function of the output . For fixed
, we consider as a function of via
Backpropagation for training neural networks is using the
chain rule for computing the gradient
Explain how to do this — it is exactly what the backward pass in the
code above does.
7.9.4 Reading handwritten digits
Live Python
Live Python
7.9.5 A network that writes
Insert into the loss and
use :
Now differentiate with respect to . The first term contributes
if and otherwise — that is exactly . For the
second term the chain rule gives
and adding the two contributions gives .
Compare this with the computation for the single neuron in Section
7.9
— same cancellation, same reason: the
derivative of undoes the exponential.
Live Python
Softmax with classes is the sigmoid: show that
where . So a softmax output layer with
two classes is a single sigmoid neuron reading the difference of
the two scores — logistic regression once more.
Hint
Divide the numerator and the denominator of
by .
The cell prints the mean loss before training and after training.
- Explain why the loss starts close to .
- After training the mean loss is close to . Compute . In what sense does the network now give the correct next symbol "a third of the probability" — and how much better is that than the it started from?
Before training the weights are small random numbers, so all
probabilities are close to and every example costs
about . For the second part: if every
example had loss exactly , the probability given to the
correct symbol would be every time.
The cell is yours — experiment.
- Set the context length to , so the network only sees one symbol back, and rerun. The invented names get worse in a specific way — describe it. What statistics can the network still learn?
- Try . More of the output gets flagged as memorized — why is a longer context more prone to memorizing a small dataset?
- Change the die roll np.random.default_rng(1) to a different seed and rerun the last part — new names, same network. Then replace the names by a word list of your own choice (capital cities, Pokémon, dinosaurs, ...) and train a different writer.
7.10 Lagrange multipliers
Suppose that is a local maximum/minimum for
(7.19)
. Then there exists
, such that is a critical point for .
Consider the minimization problem
First of all, why does this problem have a solution at all? We write
the non-linear equations
up coming from the critical points of the Lagrange function. Now we know that
these can be solved and that amongst our solutions there is a minimum!
Computing the distance from the line to the point gives rise to the
minimization problem
Solve this minimization problem using Theorem
7.39
.
Use Theorem
7.39
to
maximize subject to .
Hint
Consider the subset
. Why is a closed subset?
Why is bounded?
Hint
How does this relate to Theorem
5.84
?
Does the optimization
problem have a geometric interpretation?
Hint
Here you end up with the system
of linear equations in and , where you
regard as a constant. Use Gaussian
elimination to solve this system in order to
derive a (nice) quadratic equation in coming from
where you assume that . Handle the case separately.
To prove that is bounded you can keep fixed in
and solve for . A last resort is using the plot in the Hint button below, but that
does not give any real insight unless you explain how the plot is made from
the equation
(7.22)
.
Live Python
A rectangular box has side lengths , and . What is its
maximal volume when we assume that lies on the plane
for .
A company is planning to produce a box with volume
. For design reasons it needs different
materials for the sides, top and bottom. The cost of the materials
per square meter is dollar for the sides, dollars for the
bottom and the top. Find the measurements of the box minimizing the
production costs.
Hint
Let and be the measurements. Use to
rewrite the Lagrange equations so that and are expressed in terms
of .
7.11 Optimization using the interior and boundary of a subset
Consider an optimization problem
where is a subset, a
differentiable function and an optimal solution to
(7.23)
. If , then is a critical point of .
Consider the minimization problem
from Example
7.40
. Let us modify it to
where
We are now minimizing not only over the unit circle, but
the whole unit disk. Here
Proposition
7.46
guides us first to look for
optimal points in . Here we use Proposition
7.20
to
show that there can be no optimal points in , because
the gradient of the function is
Therefore the boundary needs to be analyzed and the usual technique
(as was implicit in Lagrange multipliers) is to find
a parametrization for the points satisfying
. There are two of those (one for the upper unit circle and one for the lower unit circle):
where .
This means that the optimization problem for the boundary turns into the two
simpler optimization problems of minimizing
subject to . These can as one variable optimization problems be solved the usual way.
Solve the two optimization problems
where . But first give a
reason as to why they both are solvable.
Hint
First find and . Then try with Proposition
7.46
supposing that a maximal point really is to be found in and not
on .
Solve the two optimization problems
where . But first give a
reason as to why they both are solvable.
Solve the two optimization problems
where is the triangle with vertices in and . But first give a
reason as to why they both are solvable.
Use Proposition
7.46
to give all the minute details in applying
Theorem
7.39
to solve Exercise
7.44
.
First rewrite to the problem, where you minimize subject to by using .
Then explain why this problem may be solved by restricting with upper and lower bounds on and . The minimum () is attained in a critical point and not on the
boundary. For one may optimize over the compact subset
and analyze what happens when .
