Calculus

Matrix calculus for data scientists

Take the red pill and learn about matrix calculus!

Vincent Mueller
September 6, 20216 min read
Photo by Markus Spiske on Unsplash
Photo by Markus Spiske on Unsplash

Prologue

You take the blue pill...the story ends, you wake up in your bed and believe whatever you want to believe. You take the red pill...you stay in Wonderland, and I show you how deep the rabbit hole goes.

Those are the famous words Morpheus said to Neo in the matrix trilogy. You have to make the same choice, do you want to keep using autograd frameworks like pytorch and tensorflow not knowing, what they do? Or do you want to dig deeper into the world of matrix calculus and understand things like how backpropagation works ?

Linear algebra basics

Photo by Uriel Soberanes on Unsplash
Photo by Uriel Soberanes on Unsplash

Vectors and matrices

I write scalars (single numbers) with a small and thin letter

(Image by author)
(Image by author)

While vectors will be denoted by small bold letter. They are column vectors by default.

(Image by author)
(Image by author)

Row vectors are also denoted by small bold letters, but they have a T superscript. The T superscript stands for transposed.

(Image by author)
(Image by author)

The symbols representing matrices will be bold capital letters.

(Image by author)
(Image by author)

We can also transpose a matrix. When doing this, the first column becomes the first row and vice versa.

(Image by author)
(Image by author)

The dimension of a vector or matrix is a tuple:

( number of rows, number of columns)

Let's consider the following case:

(Image by author)
(Image by author)

Dot product

The dot product is also defined for vectors and matrices. But the order matters and the number of columns of the left vector/matrix must match the number of rows of the right vector/matrix.

(Image by author)
(Image by author)

The dimension of the result is: (number of rows of the left input, number of columns of the right input)

(Image by author)
(Image by author)

If you are interested you can see how the dot product is performed here in greater detail.

(Image by author)
(Image by author)

To get one element of the output, we multiply and sum up one row of the left and one column of the right vector/matrix.

The importance of the dot product lies in the fact, that it can be used in many different contexts. In mechanics it can used to represent a rotation and stretching of an object. It can also be used to change the coordinate system.

Analysis basics

Photo by SOON SANTOS on Unsplash
Photo by SOON SANTOS on Unsplash

Derivatives of sums

When we want to take the derivative of a sum, it is equivalent to taking the derivative of each addend.

(Image by author)
(Image by author)

Product rule

If we want to take the derivative of the product of two functions, both depending on the variable we want to differentiate by, we can use the following rule:

(Image by author)
(Image by author)

Let's consider the following example:

(Image by author)
(Image by author)

Then the derivative of 𝑦 with respect to π‘₯ is:

(Image by author)
(Image by author)

Chain rule

We want to differentiate a function 𝑦. This function depends on 𝑒 and 𝑒 depends on π‘₯. We can then apply the chain rule.

(Image by author)
(Image by author)

Let's make a quick example here.

(Image by author)
(Image by author)

Matrix calculus

After downloading that stuff into your brain, I hope that you are ready for matrix calculus!

There are two conventions in which we can write matrix calculus, the so called "numerator layout" and the "denominator layout". I will use the numerator layout during this whole blogpost.

Photo by Charlein Gracia on Unsplash
Photo by Charlein Gracia on Unsplash

Scalar by vector derivative

The derivative of a scalar valued function with respect to a vector of variables then is a row vector. This row vector has one column for each variable we want to differentiate by.

(Image by author)
(Image by author)

The dots just indicate, that there there are not necessarily 3 variables we want to differentiate by, but rather a variable number.

Let's make an example:

(Image by author)
(Image by author)

We can then apply the "derivative of a sum" rule:

(Image by author)
(Image by author)

Since the derivative of a variable with respect to another is 0, we get:

(Image by author)
(Image by author)

Vector by scalar derivative

If we have a function 𝑓, which outputs a vector and want to take the derivative of it with respect to a single variable, we get a column vector as result.

(Image by author)
(Image by author)

Lets consider the following vector valued function we want to take the derivative of.

(Image by author)
(Image by author)

Vector by vector derivative

When taking the derivative of a vector valued function with respect to a vector of variables, we get a matrix. I use a function with 2 output values and 3 input variables as example. But you can use any number of output values and input variables.

(Image by author)
(Image by author)

You can think of it like combining the "scalar by vector" and the "vector by scalar derivative". We vary the elements of the function output along the rows and the elements of the variables along the columns.

(Image by author)
(Image by author)

The resulting matrix has the dimensions:(size of output, size of input)

Now we go through a quick example.

(Image by author)
(Image by author)

Summary

(Image by author)
(Image by author)

Vector chain rule

Let's consider the following scenario. Vector 𝑓 is a function of vector 𝑔. Vector 𝑔 itself is a function of vector π‘₯. So the derivative of 𝑓(𝑔(π‘₯)) with respect to π‘₯ is calculated the following way:

(Image by author)
(Image by author)

We can see that the vector chain rule looks almost the same as the scalar chain rule. The dot product remains in the formula and we have to construct the "vector by vector" derivative matrices.

Here's a little example:

(Image by author)
(Image by author)

We calculate the partial derivatives.

(Image by author)
(Image by author)

And now we expand the dot product.

(Image by author)
(Image by author)

One last simplification and we get the result.

(Image by author)
(Image by author)

Dot product of Constants

We can treat constant matrices and vectors almost like scalar constants. Factoring them out is allowed. However keep in mind, that the order matters for dot products between two non-scalars.

(Image by author)
(Image by author)

We can now factor out the matrix 𝑋, if it is a constant and does not depend on 𝑀.

(Image by author)
(Image by author)

Know we construct the vector by vector derivative matrix.

(Image by author)
(Image by author)

The matrix with ones on the diagonal and zeros elsewhere is called the Identity matrix. Everything multiplied by it remains the same. Therefore we the final result:

(Image by author)
(Image by author)

You can get the same result, if you do the dot product yourself instead of relying on me telling you about the identity matrix.

What is interesting is that although the whole equation contained vectors and matrices we got the same result we would have expected based on scalar calculus. However this is not always the case since not all rules of scalar calculus also hold for multivariate calculus. There exists no vector product rule to name one example.

Epilogue

I am glad you took the red pill and joined me on my journey deep down into the field of matrix calculus. If this is new for you, then you should practice a lot and go through a few examples yourself. I hope you enjoyed this blog post and wish you all the best.

Vincent MΓΌller

Backpropagation in Neural Networks

Einstein index notation

Deep Q learning is no rocket science

How you can use GPT-J

Want to connect and support me?

Linkedin https://www.linkedin.com/in/vincent-m%C3%BCller-6b3542214/ Facebook https://www.facebook.com/profile.php?id=100072095823739 Twitter https://twitter.com/Vincent02770108 Medium https://medium.com/@Vincent.Mueller Become medium member and support me (part of your membership fees go directly to me) https://medium.com/@Vincent.Mueller/membership

Related Articles