Live data from Hacker News

The matrix calculus you need for deep learning (2018)

arxiv.org

31–40 of 43 posts

Re: The matrix calculus you need for deep learning (2018)

#31
Do people do it this way? Isn't using index notation + Einstein summation convention way easier and more powerful? You only need to remember two rules:

1. dx_i/dx_k = [i==j] where [i==j] is 0 if i != j and 1 if i == j

2. (AB)_ij = A_ik B_kj

You don't even need the second rule if the function you want to differentiate is in index notation in the first place.

The example of the other comment:

f(x) = x^T A x = x_i A_ij x_j

df/dx_k = d/dx_k (x_i A_ij x_j) = [i==k] A_ij x_j + x_i A_ij [j==k] = A_kj x_j + x_i A_ik.

You can also skip the intermediate [i==k] step and immediately set the indices of other factors in a term equal. For example, differentiating with respect to A is immediate, even though the method from the article can't even handle it directly:

df/A_kl = x_k x_l

Re: The matrix calculus you need for deep learning (2018)

#32
post #25

If you know single variable calculus and basic linear algebra, then you can often muddle through by just remembering that the derivative of, (1) f: R^n -> R is a vector (2) f: R -> R^n is a vector (3) f: R^m -> R^n is an n x m matrix (the Jacobian) (*) f(x)=x^tAx is f'(x)=(A + A^t)x (this is an example of (1)) (**) and that the derivative (gradient) of (1) gives you (3) with m=n, and in this case the derivative of (3…

I struggling in DL when we started having to do tensor multiplications on the homework. Good with 2D matrices but the higher dimensional math gave me a lot of trouble when it should have been straight forward. Any tips there?

If you squint a bit tensor multiplications are still basically just matrix multiplications. At each coordinate in the output you'll place the sum of a bunch of element-wise products. Here are a few examples, some of which you've seen before and some of which you might not have:

- Vector inner product (dot product) -- n-element vectors V and W are combined to create the single element (V_1 x W_1 + V_2 x W_2 + ...). Let's reframe this as a type of tensor contraction (multiplication). You have an (n,) tensor and another (n,) tensor (both "1D"), and you're getting rid of the first dimension in each of them, so the product is an (,) tensor ("0D"), and each element is computed by summing up n element-wise multiplications.

- Vector outer product -- we have the same vectors V and W as before, but the result is a "2D" (n,n) matrix M where M_a,b = V_a x W_b. Reframing this as a type of tensor contraction, you aren't getting rid of, summing over, or contracting any dimensions, so the product includes every dimension from both inputs -- it's (n,n). Each element is computed by summing up 1 element-wise multiplication.

- Matrix product -- Consider A an (i,j) matrix and B a (j,k) matrix. Standard matrix multiplication yields a result M (i,k). As a tensor multiplication, you're contracting (summing over) the "j" entries, so each output element is the sum of j element-wise multiplications. In particular, M_a,b = (A_a,1 x B_1,b + A_a,2 x B_2,b + ...).

- Kronecker product (block matrix product) -- Consider the same matrices A and B as before. We aren't going to contract over any indices, so the result is a "4D" object M (i,j,j,k). To compute M_a,b,c,d you find all elements from A and B with those indices fixed, pair and multiply them, and sum them together. In particular, M_a,b,c,d = A_a,b x B_c,d.

- Something higher dimensional -- Consider an input tensor A with shape (i,j,k,m,n) and another B with shape (m,n,w,y,z). As I'm sure you've noticed, even for objects as simple as vectors there are several choices of tensor multiplication (contraction) available. As with any other operation, the problem you're describing will govern which one you use (just like how you'd normally have a very clear reason for choosing an inner vs outer product in a vector problem). It's still interesting to consider what our options are though. The general rule is that sizes have to line up on both sides, so for this problem we could contract over {}, {m}, {n}, or {m,n}. The outputs for each of those products would have shapes, respectively, of (i,j,k,m,n,m,n,w,y,z) [everything], (i,j,k,n,n,w,y,z) [no m], (i,j,k,m,m,w,y,z) [no n], or (i,j,k,w,y,z) [no m or n]. I'm going to skip over the first three because they're enough like things you've seen before (kronecker product for the first, matrix product for the other two) that I don't think they're worth the comment space, but the last one is novel in some sense. Our methodology for multiplication is still straightforward though. Suppose the result is called M and we want to compute its value at some coordinate M_a,b,c,d,e,f. We'll pair up elements that look like A_a,b,c,?,? with elements that look like B_?,?,d,e,f (note that the inner, contracted coordinates need to match), multiply them together, and add them up. In particular, we get something like (A_a,b,c,1,1 x B_1,1,d,e,f + A_a,b,c,1,2 x B_1,2,d,e,f + A_a,b,c,2,1 x B_2,1,d,e,f + A_a,b,c,2,2 x B_2,2,d,e,f + ...). There are m x n element-wise products being added up to achieve _each_ coordinate of the output.

Re: The matrix calculus you need for deep learning (2018)

#33

You do not need any matrix calculus for deep learning and micrograd ( https://github.com/karpathy/micrograd ), which implements backpropagation for neural nets in 100 lines of code, is a proof. Everything else is just vectorization.

Yes, you'll probably need more information-theory and statistics than linear algebra. The focus seems to be in the wrong place.

Re: The matrix calculus you need for deep learning (2018)

#34
post #9

Earlier quoted context omitted.

When I was like 14 years old one one my math teachers explained to us what calculus was using zero equations. He made everything look really easy. At college, it was the complete opposite. My professor started with limits and convergence, didn't even bother to explain why we were studying the subject. Kudos for people who actually explain things.

My high school physics teacher explained calculus to me by plotting a velocity curve, then saying the tangential line at a point (derivative) is acceleration and the area from 0 to that point under the curve (integral) was the distance travelled. Made all of calculus very easy for me to grok going forward.

I missed the day where the word tangent must have been explained, then failed basic trigonometry and felt like an idiot for the rest of high school.

Re: The matrix calculus you need for deep learning (2018)

#35

If you know single variable calculus and basic linear algebra, then you can often muddle through by just remembering that the derivative of, (1) f: R^n -> R is a vector (2) f: R -> R^n is a vector (3) f: R^m -> R^n is an n x m matrix (the Jacobian) (*) f(x)=x^tAx is f'(x)=(A + A^t)x (this is an example of (1)) (**) and that the derivative (gradient) of (1) gives you (3) with m=n, and in this case the derivative of (3…

I think it conceptually helps a lot to distinguish between row and column vectors, even if they are coalesce in any production code you might write. (0a) An (nxm) matrix A represents a linear transformation f(x)=Ax from R^m -> R^n (0b) A linear transformation f(x) = Ax is its own derivative, f'(x) = Ax (3) The derivative of a function f : R^m -> R^n is a linear transformation with the same "type" R^m -> R^n as the or…

In (2) I think you meant to write "f : R -> R^n"

Re: The matrix calculus you need for deep learning (2018)

#36
post #31

Do people do it this way? Isn't using index notation + Einstein summation convention way easier and more powerful? You only need to remember two rules: 1. dx_i/dx_k = [i==j] where [i==j] is 0 if i != j and 1 if i == j 2. (AB)_ij = A_ik B_kj You don't even need the second rule if the function you want to differentiate is in index notation in the first place. The example of the other comment: f(x) = x^T A x = x_i A_ij…

I think you got the indices "k" and "j" mixed up in rule 1.

Re: The matrix calculus you need for deep learning (2018)

#37
post #24
post #6

is there an online course for maths? like undergrad maths?

I’ve seen “The Infinite Napkin” passed around in math circles. I can’t vouch for it and I haven’t read much of it: https://web.evanchen.cc/napkin.html

Er, +1000

Man, if I had forty hours instead of forty minutes, I bet I could actually have explained this all.

This book is my attempt at those forty hours.

This project has evolved to more than just forty hours.

Re: The matrix calculus you need for deep learning (2018)

#38

Earlier quoted context omitted.

I think it conceptually helps a lot to distinguish between row and column vectors, even if they are coalesce in any production code you might write. (0a) An (nxm) matrix A represents a linear transformation f(x)=Ax from R^m -> R^n (0b) A linear transformation f(x) = Ax is its own derivative, f'(x) = Ax (3) The derivative of a function f : R^m -> R^n is a linear transformation with the same "type" R^m -> R^n as the or…

In (2) I think you meant to write "f : R -> R^n"

You're right, thanks!

Re: The matrix calculus you need for deep learning (2018)

#39

If you know single variable calculus and basic linear algebra, then you can often muddle through by just remembering that the derivative of, (1) f: R^n -> R is a vector (2) f: R -> R^n is a vector (3) f: R^m -> R^n is an n x m matrix (the Jacobian) (*) f(x)=x^tAx is f'(x)=(A + A^t)x (this is an example of (1)) (**) and that the derivative (gradient) of (1) gives you (3) with m=n, and in this case the derivative of (3…

I think it conceptually helps a lot to distinguish between row and column vectors, even if they are coalesce in any production code you might write. (0a) An (nxm) matrix A represents a linear transformation f(x)=Ax from R^m -> R^n (0b) A linear transformation f(x) = Ax is its own derivative, f'(x) = Ax (3) The derivative of a function f : R^m -> R^n is a linear transformation with the same "type" R^m -> R^n as the or…

> (0b) A linear transformation f(x) = Ax is its own derivative, f'(x) = Ax

f'(x) is just A , not Ax,

f'(x) != f(x)

Re: The matrix calculus you need for deep learning (2018)

#40
The idea is great but the many notational typos are going to confuse the hell out of people who don't already know this math. (e.g. References to vector variables are often not bolded even as they are introducing bold as the notation for vectors, reusing x excessively as a variable in multiple contexts in the same expression, etc.)

I went to graduate school for physics so I know all of this math cold but reading this paper caused me much confusion until I figured out they had a ton of typos in it.

Caveat reader.

Upon closer inspection, I realized they are not so much typos as poor UI implementation of the rendering. The visual difference between bold and italic when this page renders in either Chrome or Firefox is so subtle, it's very hard to see. Many of the equations and in-line variable references are rendered with svg so one cannot even inspect the page source to determine the intended font weight. Regardless, it's very confusing to read because of that. Not exactly what you want when trying to explain vector calculus. :)

P.S. I'll try to submit some comments to the authors to see if they can get this cleared up before it causes too much confusion.

Post reply on HN