Contents

Matrices and Derivatives: Solving Some Problems

Written on January 29, 2019

NOTE: This post is automatically translated from Farsi using GPT 5.5

The main use of differentiation is finding extremum points; but the way derivatives are used to reach an extremum can be either through iterative optimization methods or by formally solving equations. To see an example of each of these cases, first we estimate the value of the correlation matrix from a number of samples drawn from a Gaussian distribution, and then we compute the gradient of an MLP with respect to its weights.

Estimating the correlation matrix

The formula for the multivariate Gaussian distribution is what appears below:

p(x|μ,Σ)=det(2πΣ)12exp(12(xμ)TΣ1(xμ))

Well now, if we have samples x1 through xn from this distribution, what values of μ and Σ maximize the probability of these samples occurring?

Assuming the samples are independent of one another, the probability of all of them occurring becomes:

p(x1,x2,xn|μ,Σ)=p(xi|μ,Σ)log(p(x1,x2,xn|μ,Σ))=log(p(xi|μ,Σ))

When we can do with the log-probability the same thing we can do with the probability, why not go for it? Especially for the Gaussian distribution, where the exponential function plays a fundamental role. Well, by substituting the probability distribution relation into the logarithm formulas, we have:

log(p(x1,x2,xn|μ,Σ))=[log(det(2πΣ)12)12(xiμ)TΣ1(xiμ)]

If we simplify it even further, it becomes:

y=log(p(x1,x2,xn|μ,Σ))=n2log(2mπm)n2log(det(Σ))12(xiμ)TΣ1(xiμ)

For this simplification, I first took the determinant expression, which had nothing to do with the summation index, out of the summation, which naturally means it gets multiplied by the number of times the sum is taken (that is, n). Then I pulled 12 out of the logarithm, where it appears as a multiplier. In the next step, I simplified det(2πΣ) using the relation det(kA)=kndet(A). Finally, I moved the coefficient 12 from inside the summation to the outside, which is possible because of the distributive property of multiplication (have you noticed that n is the number of samples and m is the number of elements in each sample?)

Now all we need to do is take two derivatives and set them equal to zero. One with respect to μ (a vector) and the other with respect to Σ (a matrix). We start with the vector first:

yμ=12(Σ1+ΣT)((xi)nμ)=0μ=1nxi

It was computed that quickly! Do you know why I paid no attention to Σ1 and its counterpart ΣT? Because according to the formula, these matrices are invertible, and their product with a vector becomes zero in only one case, namely when that vector itself is zero; that is, (xi)nμ=0. But the second derivative, which is also more interesting:

yΣ=yΣ1Σ1ΣyΣ1=n2vecT(ΣT)12((xiμ)T(xiμ)T)Σ1Σ=(ΣTΣ1)yΣ=n2vecT(ΣT)(ΣTΣ1)+12((xiμ)T(xiμ)T)(ΣTΣ1)

Do not be frightened by the lengthiness of the formula above; there is nothing inside it. It is enough to use the following relation for simplification:

vec(AXB)=(BTA)vec(X)

Or its equivalent:

vecT(AXB)=vecT(X)(BAT)

By substitution, we arrive at the following result:

yΣ=n2vecT(ΣT)+12[((xiμ)T(xiμ)T)](ΣTΣ1)=0

Or, in other words:

vecT(ΣT)(ΣTΣ1)1=1n((xiμ)T(xiμ)T)=0

Again, the same relation vec(AXB). In the first post, as I said, this relation is very useful in differentiation:

vecT(ΣT)(ΣTΣ1)1=vecT(ΣT)(ΣTΣ)=vecT(ΣTΣTΣT)=vecT(ΣT)

Eureka, eureka:

vecT(ΣT)=1n((xiμ)T(xiμ)T)

Again, vec(AXB)! If X is a scalar number, A is a row vector (n×1), and B is a column vector, what relation is obtained?

vec(αab)=vec(aαb)=(bTa)vec(α)=α(bTa)vecT(αab)=vecT(aαb)=vecT(α)(baT)=α(baT)

Isn’t it beautiful? Accordingly:

(xiμ)T(xiμ)T=vecT((xiμ)(xiμ)T)vecT(ΣT)=1nvecT((xiμ)(xiμ)T)vecT(ΣT)=vecT(1n((xiμ)(xiμ)T))Σ=ΣT=1n((xiμ)(xiμ)T)

Here you should ask why Σ=ΣT? The answer is that when ΣT=1n((xiμ)(xiμ)T), its transpose is equal to itself.

Derivative with respect to the weights of an MLP

The layers of an MLP network can be represented as follows:

yn+1=ϕ(Wnyn+bn)

Depending on how many layers our MLP has, when we want to train it, we compare the output of the m-th layer with the desired results and compute the error. Suppose we consider the least mean squares error criterion:

j(W0,b0,W1,b1,Wm1,bm1)=12ymy2=12(ymy)T(ymy)

We use the machinery we built for differentiation to compute jWi. Computing the derivative with respect to the biases is completely similar:

jWi=jym(m1iyj+1yj)yiWi

Now it is enough to compute each component.

jym=(ymy)Tyj+1yj=diag(vec(ϕ(Wjyj+bj)))WjyiWi=(yiTI)

I leave it to you to figure out how I have computed these using the material I presented in the previous three posts (this is a good exercise in itself.) But the discussion becomes sweeter from here on. In order to continue, I need to introduce a new relation.

ab=diag(a)b(ab)T=bTdiag(a)

The symbol represents Hadamard, or element-wise, multiplication. On the other hand, when Wjyj+bj is a vector, if ϕ is also applied to it, the output is still a vector, so the whole derivative above can be written like this:

jWi=(ymy)T(diag(ϕ(Wjyj+bj))Wj)(yiTI)

It is not a bad idea to see what that in the middle actually does to the error value (ymy). We start with the error itself:

ejT=(ymy)Tdiag(ϕ(Wjyj+bj))Wj=((ymy)ϕ(Wjyj+bj))TWj

It is as if each element of the error is weighted by the value of the derivative of the function ϕ at that same element, and then the resulting vector is multiplied from the left by the weight matrix of the previous layer. This is exactly backpropagation of error. Each element of the error is connected, through the corresponding weight, to one of the nodes in the lower layer. The same operation can be continued:

ej1T=ejTdiag(ϕ(Wj1yj1+bj1))Wj1=(ejϕ(Wjyj+bj))TWj1

Again, the error value of the upper layer is weighted by the derivative of ϕ in the lower layer and propagated to the layer below. If we proceed in the same fashion, we reach the error of the desired layer, namely ei:

jWi=eiT(yiI)=vecT(eiyiT)

It is a good exercise to see why the relation above holds. Examining what form vec(ei) has in this case is the starting point. Anyway.

Ultimately, what is this derivative value used for? We use it in optimization and in moving toward the trained value of the weights. In gradient-based optimization methods, the gradient of the error with respect to the parameters is always added with a negative ratio. This is the basis of all gradient-based optimization methods. But here, the gradient with respect to Wi does not have dimensions similar to Wi; how should we add the two?

If we had gone to tensors from the beginning, this problem would not have arisen. The derivative value would also have dimensions equal to Wi itself (of course because j is a scalar number.) Even now, nothing special has happened. Do you remember that in order to define this kind of differentiation, which we have reviewed together over these four posts, we agreed from the beginning to represent every matrix with an ordered pair (vec(A),(m,n)). Now we have the derivative, and we also have the dimensions of the matrices Wi. It is enough to re-represent the computed derivative with the dimensions of the original matrix. The number of elements of the derivative is, by definition, the same.

The interesting point in the story is that up to now, as I write this post, without exception, rearranging the derivative for me has been equivalent to looking inside the vec operator. Look at this very MLP example:

jWi=eiT(yiI)=vecT(eiyiT)

The value eiyiT has dimensions exactly equal to the original matrix!

Moral of the story

In the end, I offer these results as a summary:

1-There is no need for tensor computations to take a derivative with respect to a matrix 2-Taking derivatives is good mental exercise 3-In principle, most of the time you do not need to take derivatives with respect to matrices yourself!