3 - 1: mathematical tools to be used in class prof. j.c. kao, ucla...

35
3 - 1: Mathematical tools to be used in class Prof. J.C. Kao, UCLA Mathematical tools for deep learning Manipulating probabilities Calculus chain rule Gradients Gradient chain rule Linear algebra intuitions

Upload: others

Post on 27-Mar-2020

2 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 3 - 1: Mathematical tools to be used in class Prof. J.C. Kao, UCLA …kao/nndl/lectures/tools.pdf · 2018-02-01 · 3 - 3: Mathematical tools to be used in class Prof. J.C. Kao, UCLA

3 - 1: Mathematical tools to be used in class Prof. J.C. Kao, UCLA

Mathematical tools for deep learning

• Manipulating probabilities

• Calculus chain rule

• Gradients

• Gradient chain rule

• Linear algebra intuitions

Page 2: 3 - 1: Mathematical tools to be used in class Prof. J.C. Kao, UCLA …kao/nndl/lectures/tools.pdf · 2018-02-01 · 3 - 3: Mathematical tools to be used in class Prof. J.C. Kao, UCLA

3 - 2: Mathematical tools to be used in class Prof. J.C. Kao, UCLA

The purpose of these notes

• These notes covers mathematical tools and intuitions we ought to befamiliar with in deep learning.

• Many of these tools are also relevant for work in machine learning andstatistical signal processing.

Page 3: 3 - 1: Mathematical tools to be used in class Prof. J.C. Kao, UCLA …kao/nndl/lectures/tools.pdf · 2018-02-01 · 3 - 3: Mathematical tools to be used in class Prof. J.C. Kao, UCLA

3 - 3: Mathematical tools to be used in class Prof. J.C. Kao, UCLA

A few words on probability notation

Sometimes, notation in probability can get sloppy and it’s hard to know exactlywhat is meant by an expression. We use the following notation in this class.

• A random variable, X, is denoted by a capital letter, and the values it cantaken on, x, are denoted by lower case letters.

• Their corresponding vector forms would be X and x.

The probability that X takes on the value x is a number that we denote as:

Pr(X = x)

The inputs to the Pr(·) function are events (like X taking on the value x) andit returns a real-valued number between 0 and 1. This number represents theprobability of the event happening.

Thus, Pr(X = x) is the probability that a random variable X takes on thevalue x.

Page 4: 3 - 1: Mathematical tools to be used in class Prof. J.C. Kao, UCLA …kao/nndl/lectures/tools.pdf · 2018-02-01 · 3 - 3: Mathematical tools to be used in class Prof. J.C. Kao, UCLA

3 - 4: Mathematical tools to be used in class Prof. J.C. Kao, UCLA

A few words on probability notation (cont.)

This notation, though clear, is often cumbersome, and thus shorthands areadopted. One still clear notation is:

pX(x) , Pr(X = x)

Here, the subscript X denotes the random variable we are evaluating theprobability of and x denotes the value it takes on. pX is typically referred to asthe probability mass (density) function when X is a discrete (continuous)random variable.

Still more common is the following notation:

p(x) , Pr(X = x)

This notation may be ambiguous, since the random variable is omitted.However, we will operate on the assumption that if the random variable isomitted, we are referring to the corresponding uppercase variable.

Page 5: 3 - 1: Mathematical tools to be used in class Prof. J.C. Kao, UCLA …kao/nndl/lectures/tools.pdf · 2018-02-01 · 3 - 3: Mathematical tools to be used in class Prof. J.C. Kao, UCLA

3 - 5: Mathematical tools to be used in class Prof. J.C. Kao, UCLA

A few words on probability notation (cont.)

In this class, we will use the notation:

p(x) , Pr(X = x)

• When you see p(x), it denotes the probability that a random variable Xtakes on the value x.

• The same format generalizes to mutiple random variables. E.g., p(x, y, z)is the probability that three random variables, X, Y , and Z take on valuesx, y, and z respectively, i.e., Pr(X = x, Y = y, Z = z).

• If the random variable is ambiguous, we will denote it outright, e.g., pY (x)would be the probability that a random variable Y takes on the value x.

With this notation, and for this class, we avoid using the following confusingexpressions:

• Pr(x) – the probability of a number (rather than an event).

• p(X) – the probability of a random variable taking on a random variable(rather than a value)

• p(X = x) – the probability of a random variable taking on an event ...?

Page 6: 3 - 1: Mathematical tools to be used in class Prof. J.C. Kao, UCLA …kao/nndl/lectures/tools.pdf · 2018-02-01 · 3 - 3: Mathematical tools to be used in class Prof. J.C. Kao, UCLA

3 - 6: Mathematical tools to be used in class Prof. J.C. Kao, UCLA

Manipulating probabilities

Manipulating probability expressions is an important part of machine learning.To do so, we largely employ two rules of probability: (1) the law of totalprobability (or sum rule), and (2) the probability chain rule (or product rule).

• Law of total probability:

p(x) =∑y

p(x, y) x, y discrete

p(x) =

∫y

p(x, y)dy x, y continuous

• Probability chain rule:

Pr(Event1,Event2) = Pr(Event1) Pr(Event2|Event1)

= Pr(Event2) Pr(Event1|Event2)

Page 7: 3 - 1: Mathematical tools to be used in class Prof. J.C. Kao, UCLA …kao/nndl/lectures/tools.pdf · 2018-02-01 · 3 - 3: Mathematical tools to be used in class Prof. J.C. Kao, UCLA

3 - 7: Mathematical tools to be used in class Prof. J.C. Kao, UCLA

Probability chain rule

The probability chain rule is very useful for manipulating probabilityexpressions.

• Intuitively, we think of the chain rule as breaking up the joint probabilityinto a product probability.

• E.g., if I want to evaluate p(x, y), I can do this in two ways:• I can first evaluate the probability that X = x, and now given that X = x,

evaluate the probability that Y = y. Hence,

p(x, y) = p(x)p(y|x)• I can also evaluate the probability that Y = y, and now given that Y = y,

evaluate the probability that X = x. Hence,

p(x, y) = p(y)p(x|y)

Page 8: 3 - 1: Mathematical tools to be used in class Prof. J.C. Kao, UCLA …kao/nndl/lectures/tools.pdf · 2018-02-01 · 3 - 3: Mathematical tools to be used in class Prof. J.C. Kao, UCLA

3 - 8: Mathematical tools to be used in class Prof. J.C. Kao, UCLA

Probability chain rule (cont.)

• Note that Event1 and Event2 could encompass multiple random variablestaking on values.

• E.g., consider

Event1 = {W = w,X = x}Event2 = {Y = y, Z = z}

Then the probability chain rule states:

p(w, x, y, z) = p(w, x)p(y, z|w, x)

= p(y, z)p(w, x|y, z)

Further decomposing, we could arrive at all sorts of equivalent expressions.

p(w, x, y, z) = p(w, x)p(y, z|w, x)

= p(x)p(w|x)p(y, z|w, x) Applied to p(w, x)

= p(x)p(w|x)p(z|w, x)p(y|z, w, x) Applied to p(y, z|w, x)

Page 9: 3 - 1: Mathematical tools to be used in class Prof. J.C. Kao, UCLA …kao/nndl/lectures/tools.pdf · 2018-02-01 · 3 - 3: Mathematical tools to be used in class Prof. J.C. Kao, UCLA

3 - 9: Mathematical tools to be used in class Prof. J.C. Kao, UCLA

Probability chain rule (cont.)

• A helpful intuition: any event that has been in front of the conditioning barmust be behind the conditioning bar for all other probability expressions.

• This intuition states: once I evaluate the probability of a random variabletaking on a certain value, I now assume that random variable is that value,and evaluate the probability of the remaining events.

• More examples to make this concrete, with the probability p(w, x, y, z).• Evaluate the probability that X = x, then evaluate the probability that

W = w, Y = y, Z = z given that X = x:

p(w, x, y, z) = p(x)p(w, y, z|x)• Evaluate the probability that W = w and Z = z, then Y = y given the

prior conditions, then X = x given the prior conditions:

p(w, x, y, z) = p(w, z)p(y|w, z)p(x|w, z, y)

• Evaluate the probability W = w, then X = x given the prior condition, thenY = y, then Z = z:

p(w, x, y, z) = p(w)p(x|w)p(y|w, x)p(z|w, x, y)

Page 10: 3 - 1: Mathematical tools to be used in class Prof. J.C. Kao, UCLA …kao/nndl/lectures/tools.pdf · 2018-02-01 · 3 - 3: Mathematical tools to be used in class Prof. J.C. Kao, UCLA

3 - 10: Mathematical tools to be used in class Prof. J.C. Kao, UCLA

Probability chain rule example

Graphical models represent conditional independencies in a succinct way.Though we don’t cover them in class, here is a simple example:

z y

x

This graph represents that x is influenced by z and y. This graph also has thefollowing decomposition: p(x, y, z) = p(z)p(y)p(x|y, z).

Example: Show that, for this model, y ⊥⊥ z.

From the chain rule of probability,

p(x, y, z) = p(z)p(y|z)p(x|y, z)= p(y)p(z|y)p(x|y, z)

Comparing these expressions to the model expression, we see thatp(z) = p(z|y) and p(y) = p(y|z). Thus, y and z are independent.

Page 11: 3 - 1: Mathematical tools to be used in class Prof. J.C. Kao, UCLA …kao/nndl/lectures/tools.pdf · 2018-02-01 · 3 - 3: Mathematical tools to be used in class Prof. J.C. Kao, UCLA

3 - 11: Mathematical tools to be used in class Prof. J.C. Kao, UCLA

Bayes’ rule

By combining the probability chain rule and law of total probability, we canarrive at what is commonly called Bayes’ rule or Bayes’ theorem. Therelationship is derived by recognizing:

p(x, y) = p(x)p(y|x)

= p(y)p(x|y)

Hence,

p(x|y) =p(y|x)p(x)

p(y)

=p(y|x)p(x)∑

x p(x, y)

=p(y|x)p(x)∑x p(y|x)p(x)

Thus, from Bayes rule, given p(x) and p(y|x), it is possible to calculate p(x|y).

Page 12: 3 - 1: Mathematical tools to be used in class Prof. J.C. Kao, UCLA …kao/nndl/lectures/tools.pdf · 2018-02-01 · 3 - 3: Mathematical tools to be used in class Prof. J.C. Kao, UCLA

3 - 12: Mathematical tools to be used in class Prof. J.C. Kao, UCLA

A word on Bayes’ rule (10,000 foot level view of machine learning)

Bayes’ rule appears frequently in machine learning, and is the basis for Bayesianinference. There’s no expectation that you understand the details of this at themoment, but we can at least discuss intuition at a high-level.

Let x represent model parameters you wish to infer denoted θ, and let ycorrespond to data you observe denoted D.

p(θ|D) =p(D|θ)p(θ)∑x p(D|θ)p(θ)

What do each of these terms mean (in plain language)?

• p(θ|D) is the probability distribution of the model parameters given thedata. This is sometimes called the posterior distribution.

• p(D|θ) is the probability of having seen the data given a chosen set ofmodel parameters. This is sometimes called the likelihood of the data.

• p(θ) are the probabilities of the model parameters absent of any data.This is sometimes called the prior on the parameters, since they representthe prior likelihood of each parameter before you saw any data. After yousee data, the prior can be thought of as being “updated” by the likelihoodto arrive at the posterior distribution on the parameters.

Page 13: 3 - 1: Mathematical tools to be used in class Prof. J.C. Kao, UCLA …kao/nndl/lectures/tools.pdf · 2018-02-01 · 3 - 3: Mathematical tools to be used in class Prof. J.C. Kao, UCLA

3 - 13: Mathematical tools to be used in class Prof. J.C. Kao, UCLA

A word on Bayes’ rule (cont.)

What can we do if we calculate these probabilities?

• In Bayesian inference, we calculate p(θ|D). Hence, we now have andistribution over the model parameters given the data we observed. Wecan then sample parameters from this distribution, or we can take theexpected value of the parameters from the distribution, or the medianvalue, etc. This concretely gives us all the parameters of our model.

• In a related area of machine learning, called maximum-likelihoodestimation (or Frequentist inference), we instead focus on the p(D|θ)term. We treat θ as a point (not a random variable with a distribution),and we want to infer the θ that makes the data most likely to have beenobserved. Hence, we choose the parameters that maximize the likelihoodof the data, p(D|θ).

There are benefits and cons of both approaches. Usually in this class, we’ll domaximum-likelihood estimation. This doesn’t communicate a preference forone or the other. Other machine learning classes will delve into these topics inmore detail.

Page 14: 3 - 1: Mathematical tools to be used in class Prof. J.C. Kao, UCLA …kao/nndl/lectures/tools.pdf · 2018-02-01 · 3 - 3: Mathematical tools to be used in class Prof. J.C. Kao, UCLA

3 - 14: Mathematical tools to be used in class Prof. J.C. Kao, UCLA

The need for derivatives

In machine learning, we often want to find the “best” model according to someperformance metric. This language implies that we have to optimize the modelto perform as well as possible.

Optimization research comprises a very large field, and even deep-learningspecific optimization is something we’ll discuss in detail in later weeks.

In optimization, derivatives are crucial. In simple quadratic examples fromcalculus, we could find minima and maxima exactly by setting the derivativeequal to zero.

When the systems become more complex (e.g., with nonlinearities), there is noclosed-form solution from setting the derivative equal to zero, but thederivative is still useful because it tells us how a change in the modelparameters will affect our performance.

Page 15: 3 - 1: Mathematical tools to be used in class Prof. J.C. Kao, UCLA …kao/nndl/lectures/tools.pdf · 2018-02-01 · 3 - 3: Mathematical tools to be used in class Prof. J.C. Kao, UCLA

3 - 15: Mathematical tools to be used in class Prof. J.C. Kao, UCLA

The scalar derivative

Consider a function f : R→ R at a point x ∈ R. Recall the definition of thederivative:

f ′(x) = limh→0

f(x+ h)− f(x)

h

The derivative tells us how much a small change in x changes f , i.e.,

f(x+ ε) ≈ f(x) + εf ′(x)

We denote y = f(x) and denote the derivative of y with respect to x as dydx

.Hence,

∆y ≈ dy

dx∆x

Page 16: 3 - 1: Mathematical tools to be used in class Prof. J.C. Kao, UCLA …kao/nndl/lectures/tools.pdf · 2018-02-01 · 3 - 3: Mathematical tools to be used in class Prof. J.C. Kao, UCLA

3 - 16: Mathematical tools to be used in class Prof. J.C. Kao, UCLA

The scalar chain rule

The scalar chain rule states that if y = f(x) and z = g(y), then

dz

dx=dz

dy

dy

dx

Intuitively: the chain rule tells us that a small change in x will cause a smallchange in y that will in turn cause a small change in z, i.e., for apropriatelysmall ∆x,

∆y ≈ dy

dx∆x

∆z ≈ dz

dy∆y

=dz

dy

dy

dx∆x

Page 17: 3 - 1: Mathematical tools to be used in class Prof. J.C. Kao, UCLA …kao/nndl/lectures/tools.pdf · 2018-02-01 · 3 - 3: Mathematical tools to be used in class Prof. J.C. Kao, UCLA

3 - 17: Mathematical tools to be used in class Prof. J.C. Kao, UCLA

A brief word on vector and matrix derivative notation

In this class, we will use both the differentiation operator ∂ and ∇ to denotederivatives. If y is a scalar, and x is a vector, then the gradient of y withrespect to x is denoted as both:

∂y

∂xand ∇xy

The gradient of y with respect to x is itself a vector with the samedimensionality as x.

We use a similar notation for differentiation with respect to a matrix. Namely,if y is a scalar, and A is a matrix, then the derivative of y with respect to A isdenoted as both:

∂y

∂Aand ∇Ay

More on this later, but we use the “denominator layout” notation in this class,so that ∇Ay has the same dimensionality as A.

Page 18: 3 - 1: Mathematical tools to be used in class Prof. J.C. Kao, UCLA …kao/nndl/lectures/tools.pdf · 2018-02-01 · 3 - 3: Mathematical tools to be used in class Prof. J.C. Kao, UCLA

3 - 18: Mathematical tools to be used in class Prof. J.C. Kao, UCLA

The gradient

The gradient generalizes the scalar derivative to multiple dimensions. Supposef : Rn → R transforms a vector x ∈ Rn to a scalar. If y = f(x), then thegradient is:

∇xy =

∂y∂x1∂y∂x2

...∂y∂xn

In other words, the gradient is:

• A vector that is the same size as x, i.e., if x ∈ Rn then ∇xy ∈ Rn.

• Each dimension of ∇xy tells us how small changes in x in that dimensionaffect y. i.e., changing the ith dimension of x by a small amount, ∆xi,will change y by

∂y

∂xi∆xi

We may also denote this as:

(∇xy)i ∆xi

Page 19: 3 - 1: Mathematical tools to be used in class Prof. J.C. Kao, UCLA …kao/nndl/lectures/tools.pdf · 2018-02-01 · 3 - 3: Mathematical tools to be used in class Prof. J.C. Kao, UCLA

3 - 19: Mathematical tools to be used in class Prof. J.C. Kao, UCLA

Example: derivative with respect to a vector

Let f(x) = θTx. What is ∇xf(x)?

First, we note that θTx =∑

i θizi. Hence, ∂f(x)∂xi

= θi. That is,

∇xf(x) =

θ1θ2...θn

Page 20: 3 - 1: Mathematical tools to be used in class Prof. J.C. Kao, UCLA …kao/nndl/lectures/tools.pdf · 2018-02-01 · 3 - 3: Mathematical tools to be used in class Prof. J.C. Kao, UCLA

3 - 20: Mathematical tools to be used in class Prof. J.C. Kao, UCLA

Example: derivative with respect to a vector

Let f(x) = xTAx. What is ∇xf(x)?

First, we note that xTAx =∑

i

∑j aijxixj . Then, we have

∇xf(x) =

2a11x1 + a12x2 + · · ·+ a1nxn + a21x2 + · · ·+ an1xn2a22x2 + a21x1 + · · ·+ a2nxn + a12x1 + · · ·+ an2xn

...2annxn + an1x1 + · · ·+ an,n−1xn−1 + a1nx1 + · · ·+ an−1,nxn−1

= Ax+ATx

Note that if A is symmetric, then this further simplifies to:

∇xf(x) = 2Ax

(Also note that if A and x are scalars, i.e., 1-dimensional, this is consistentwith the scalar derivative.)

Page 21: 3 - 1: Mathematical tools to be used in class Prof. J.C. Kao, UCLA …kao/nndl/lectures/tools.pdf · 2018-02-01 · 3 - 3: Mathematical tools to be used in class Prof. J.C. Kao, UCLA

3 - 21: Mathematical tools to be used in class Prof. J.C. Kao, UCLA

Derivative of a scalar w.r.t. a matrix

The derivative of a scalar, y, with respect to a matrix, A ∈ Rm×n, is given by:

∇Ay =

∂y

∂a11

∂y∂a12

· · · ∂y∂a1n

∂y∂a21

∂y∂a22

· · · ∂y∂a2n

· · · · · ·. . .

...∂y

∂am1

∂y∂am2

· · · ∂y∂amn

Like the gradient, the i, jth element of ∇Ay tells us how small changes in aijaffect y.

Note:• If you search for the derivative of a scalar with respect to a matrix, you

may find people give a transposed definition to the one above.• Both are valid, but you must be consistent with your notation and use the

correct rules. Our notation is called “denominator layout” notation; theother layout is called “numerator layout” notation.

• In the denominator layout, the dimensions of ∇Ay and A are the same.The same holds for the gradient, i.e., the dimensions of ∇xy and x are thesame. In the numerator layout notation, the dimensions are transposed.

• More on this later, but in “denominator layout,” the chain rule goes rightto left as opposed to left to right.

Page 22: 3 - 1: Mathematical tools to be used in class Prof. J.C. Kao, UCLA …kao/nndl/lectures/tools.pdf · 2018-02-01 · 3 - 3: Mathematical tools to be used in class Prof. J.C. Kao, UCLA

3 - 22: Mathematical tools to be used in class Prof. J.C. Kao, UCLA

Derivative of a vector w.r.t. a vector

Let y ∈ Rn be a function of x ∈ Rm. What dimensionality should thederivative of y with respect to x be?• e.g., to see how ∆x modifies yi, we would calculate:

∆yi = ∇xyi ·∆x

• This suggests that the derivative ought to be an n×m matrix, denoted J,of the form:

J =

(∇xy1)T

(∇xy2)T

...

(∇xyn)T

=

∂y1∂x1

∂y1∂x2

· · · ∂y1∂xm

∂y2∂x1

∂y2∂x2

· · · ∂y2∂xm

......

. . ....

∂yn∂x1

∂yn∂x2

· · · ∂yn∂xm

The matrix would tell us how a small change in ∆x results in a smallchange in ∆y according to the formula:

∆y ≈ J∆x

Page 23: 3 - 1: Mathematical tools to be used in class Prof. J.C. Kao, UCLA …kao/nndl/lectures/tools.pdf · 2018-02-01 · 3 - 3: Mathematical tools to be used in class Prof. J.C. Kao, UCLA

3 - 23: Mathematical tools to be used in class Prof. J.C. Kao, UCLA

Derivative of a vector w.r.t. a vector (cont.)

The matrix J is called the Jacobian matrix.

A word on notation:

• In the denominator layout definition, the denominator vector changesalong rows.

∇xy =

∂y1∂x1

∂y2∂x1

· · · ∂yn∂x1

∂y1∂x2

∂y2∂x2

· · · ∂yn∂x2

......

. . ....

∂y1∂xm

∂y2∂xm

· · · ∂yn∂xm

,

∂y

∂x

• Hence the notation we use for the Jacobian would be:

J = (∇xy)T

=

(∂y

∂x

)T

Page 24: 3 - 1: Mathematical tools to be used in class Prof. J.C. Kao, UCLA …kao/nndl/lectures/tools.pdf · 2018-02-01 · 3 - 3: Mathematical tools to be used in class Prof. J.C. Kao, UCLA

3 - 24: Mathematical tools to be used in class Prof. J.C. Kao, UCLA

Example: derivative of a vector with respect to a vector

The following derivative will appear later on in the class. Let W ∈ Rh×n andx ∈ Rn. We would like to calculate the derivative of f(x) = Wx with respectto x.

∇xWx = ∇x

w11x1 + w12x2 + · · ·+ w1nxnw21x1 + w22x2 + · · ·+ w2nxn

...wh1x1 + wh2x2 + · · ·+ whnxn

=

w11 w21 . . . wh1

w12 w22 . . . wh2

......

. . ....

w1n w2n . . . whn

= WT

Page 25: 3 - 1: Mathematical tools to be used in class Prof. J.C. Kao, UCLA …kao/nndl/lectures/tools.pdf · 2018-02-01 · 3 - 3: Mathematical tools to be used in class Prof. J.C. Kao, UCLA

3 - 25: Mathematical tools to be used in class Prof. J.C. Kao, UCLA

Hessian

The Hessian matrix of a function f(x) is a square matrix of second-orderpartial derivatives of f(x). It is composed of elements:

H =

∂f

∂x21

∂f∂x1∂x2

· · · ∂f∂x1∂xn

∂f∂x2x1

∂f

∂x22

· · · ∂f∂x2∂xn

......

. . ....

∂f∂xmx1

∂f∂xm∂x2

· · · ∂f∂x2

m

We can denote this matrix as ∇x (∇xf(x)). We often denote this simply as∇2

xf(x).

Page 26: 3 - 1: Mathematical tools to be used in class Prof. J.C. Kao, UCLA …kao/nndl/lectures/tools.pdf · 2018-02-01 · 3 - 3: Mathematical tools to be used in class Prof. J.C. Kao, UCLA

3 - 26: Mathematical tools to be used in class Prof. J.C. Kao, UCLA

Chain rule for vector valued functions

In the “denominator” layout, the chain rule runs from right to left. We won’tderive this, but we will check the dimensionality and intuition.

Let x ∈ Rm, y ∈ Rn, and z ∈ Rp. Further, let y = f(x) for f : Rm → Rn andz = g(y) for g : Rn → Rp. Then,

∇xz = ∇xy ∇yz

Equivalently:

∂z

∂x=

∂y

∂x

∂z

∂y

• Note that ∇xz should have dimensionality Rm×p.

• As ∇xy ∈ Rm×n and ∇yz ∈ Rn×p, the operations are dimensionconsistent.

Page 27: 3 - 1: Mathematical tools to be used in class Prof. J.C. Kao, UCLA …kao/nndl/lectures/tools.pdf · 2018-02-01 · 3 - 3: Mathematical tools to be used in class Prof. J.C. Kao, UCLA

3 - 27: Mathematical tools to be used in class Prof. J.C. Kao, UCLA

Chain rule for vector valued functions (cont.)

• Intuitively, a small change ∆x affects ∆z through the Jacobian (∇xz)T

∆z ≈ (∇xz)T ∆x (1)

• The chain rule is intuitive, since:

∆y ≈ (∇xy)T ∆x

∆z ≈ (∇yz)T ∆y

Composing these, we have that:

∆z ≈ (∇yz)T (∇xy)T ∆x (2)

• Combining equations (1) and (2), we arrive at:

(∇xz)T = (∇yz)T (∇xy)T

which, after transposing both sides, reduces to the (right to left) chainrule:

∇xz = ∇xy ∇yz

Page 28: 3 - 1: Mathematical tools to be used in class Prof. J.C. Kao, UCLA …kao/nndl/lectures/tools.pdf · 2018-02-01 · 3 - 3: Mathematical tools to be used in class Prof. J.C. Kao, UCLA

3 - 28: Mathematical tools to be used in class Prof. J.C. Kao, UCLA

Derivatives of tensors

Occasionally in this class, we may need to take a derivative that is more than2-dimensional. For example, we may want to take the derivative of a vectorwith respect to a matrix. This would be a 3-dimensional tensor. The definitionfor this would be as you expect. In particular, if z ∈ Rp and W ∈ Rm×n, then∇Wz is a three-dimensional tensor with shape Rm×n×p. Each m× n slice (ofwhich there are p) is the matrix derivative ∇Wzi.

Note, these are sometimes a headache to work with. We typically can find ashortcut to perform operations without having to compute and store thesehigh-dimensional tensor derivatives. The next slides show an example.

Page 29: 3 - 1: Mathematical tools to be used in class Prof. J.C. Kao, UCLA …kao/nndl/lectures/tools.pdf · 2018-02-01 · 3 - 3: Mathematical tools to be used in class Prof. J.C. Kao, UCLA

3 - 29: Mathematical tools to be used in class Prof. J.C. Kao, UCLA

Multivariate chain rule and tensor derivative example

Consider the squared loss function:

L =1

2

N∑i=1

∥∥∥y(i) −Wx(i)∥∥∥2

Here, y(i) ∈ Rm and x(i) ∈ Rn so that W ∈ Rm×n. We wish to find W thatminimizes the mean-square error in linearly predicting y(i) from x(i).

We consider one example, ε(i) = 12

∥∥∥y(i) −Wx(i)∥∥∥2.

We wish to calculate ∇Wε(i).

To do so, we define z(i) = y(i) −Wx(i). Then, ε(i) = 12(z(i))T z(i). For the

rest of the example, we’re going to drop the superscripts (i) and assume we’reworking with the ith example (to help notation). Then, we need to calculate is:

∇Wε = ∇Wz∇zε

Page 30: 3 - 1: Mathematical tools to be used in class Prof. J.C. Kao, UCLA …kao/nndl/lectures/tools.pdf · 2018-02-01 · 3 - 3: Mathematical tools to be used in class Prof. J.C. Kao, UCLA

3 - 30: Mathematical tools to be used in class Prof. J.C. Kao, UCLA

Multivariate chain rule and tensor derivative example (cont.)

The first of these operations is straightforward. In particular,

∇zε = z

Now, we need to calculate ∇Wz. This is a three dimensional tensor withdimensionality m× n×m.

This makes sense dimensionally, because when we multiply a (m× n×m)tensor by a (m× 1) vector, we get out a (m× n× 1) tensor, which isequivalently an (m× n) matrix. Because ∇Wε is an (m× n) matrix, this allworks out.

Page 31: 3 - 1: Mathematical tools to be used in class Prof. J.C. Kao, UCLA …kao/nndl/lectures/tools.pdf · 2018-02-01 · 3 - 3: Mathematical tools to be used in class Prof. J.C. Kao, UCLA

3 - 31: Mathematical tools to be used in class Prof. J.C. Kao, UCLA

Multivariate chain rule and tensor derivative example (cont.)

∇Wz is an (m× n×m) tensor.

• Letting zk denote the kth element of z, we see that each ∂zk∂W

is an m× nmatrix, and that there are m of these for each element zk fromk = 1, . . . ,m.

• The matrix, ∂zk∂W

, can be calculated as follows:

∂zk∂W

=∂

∂W

n∑j=1

−wkjxj

and thus, the (k, j)th element of this matrix is:(∂zk∂W

)k,j

=∂zk∂wkj

= −xj

It is worth noting that(∂zk∂W

)i,j

=∂zk∂wij

= 0 for k 6= i

Page 32: 3 - 1: Mathematical tools to be used in class Prof. J.C. Kao, UCLA …kao/nndl/lectures/tools.pdf · 2018-02-01 · 3 - 3: Mathematical tools to be used in class Prof. J.C. Kao, UCLA

3 - 32: Mathematical tools to be used in class Prof. J.C. Kao, UCLA

Multivariate chain rule and tensor derivative example (cont.)

Hence, ∂zk∂W

is a matrix where the kth row is xT and all other rows are thezeros (we denote the zero vector by 0). i.e.,

∂z1∂W

=

−xT

0T

...0T

∂z2∂W

=

0T

−xT

...0T

etc...

Now applying the chain rule,

∂ε

∂W=

∂z

∂W

∂ε

∂z

is a tensor product between an (m× n×m) tensor and an (m× 1) vector,whose resulting dimensionality is (m× n× 1) or equivalently, an (m× n)matrix.

Page 33: 3 - 1: Mathematical tools to be used in class Prof. J.C. Kao, UCLA …kao/nndl/lectures/tools.pdf · 2018-02-01 · 3 - 3: Mathematical tools to be used in class Prof. J.C. Kao, UCLA

3 - 33: Mathematical tools to be used in class Prof. J.C. Kao, UCLA

Multivariate chain rule and tensor derivative example (cont.)

We carry out this tensor-vector multiply in the standard way.

∂ε

∂W=

∂z

∂W

∂ε

∂z

=

m∑i=1

∂zi∂W

(∂ε

∂z

)i

=∂ε

∂z1

−xT

0T

...0T

+∂ε

∂z2

0T

−xT

...0T

+ · · ·

+∂ε

∂zm

0T

0T

...−xT

Page 34: 3 - 1: Mathematical tools to be used in class Prof. J.C. Kao, UCLA …kao/nndl/lectures/tools.pdf · 2018-02-01 · 3 - 3: Mathematical tools to be used in class Prof. J.C. Kao, UCLA

3 - 34: Mathematical tools to be used in class Prof. J.C. Kao, UCLA

Multivariate chain rule and tensor derivative example (cont.)

Continuing the work from the previous page...

∂ε

∂W= −

∂ε∂z1

xT

∂ε∂z2

xT

...∂ε

∂zmxT

= −∂ε

∂zxT

Hence, with a final application of the chain rule, we get that

∇Wε = −zxT

= −(y −Wx)xT

Setting this equal to zero, we find that for one example,

W = yxT (xxT )−1

Summing across all examples, this produces least-squares.

Page 35: 3 - 1: Mathematical tools to be used in class Prof. J.C. Kao, UCLA …kao/nndl/lectures/tools.pdf · 2018-02-01 · 3 - 3: Mathematical tools to be used in class Prof. J.C. Kao, UCLA

3 - 35: Mathematical tools to be used in class Prof. J.C. Kao, UCLA

A few notes on tensor derivatives

• In general, the simpler rule can be inferred via pattern intuition / lookingat the dimensionality of the matrices, and these tensor derivatives neednot be explicitly derived.

• Indeed, actually calculating these tensor derivatives, storing them, andthen doing e.g., a tensor-vector multiply, is usually not a good idea forboth memory and computation. In this example, storing all these zerosand performing the multiplications is unnecessary.

• If we know the end result is simply an outer product of two vectors, weneed not even calculate an additional derivative in this step ofbackpropagation, or store an extra value (assuming the inputs werepreviously cached).