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.
The formula for the multivariate Gaussian distribution is what appears below:
Well now, if we have samples through 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:
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:
If we simplify it even further, it becomes:
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, ). Then I pulled out of the logarithm, where it appears as a multiplier. In the next step, I simplified using the relation . Finally, I moved the coefficient from inside the summation to the outside, which is possible because of the distributive property of multiplication (have you noticed that is the number of samples and 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:
It was computed that quickly! Do you know why I paid no attention to and its counterpart ? 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, . But the second derivative, which is also more interesting:
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:
Or its equivalent:
By substitution, we arrive at the following result:
Or, in other words:
Again, the same relation . In the first post, as I said, this relation is very useful in differentiation:
Eureka, eureka:
Again, ! If is a scalar number, is a row vector (), and is a column vector, what relation is obtained?
Isn’t it beautiful? Accordingly:
Here you should ask why ? The answer is that when , its transpose is equal to itself.
The layers of an MLP network can be represented as follows:
Depending on how many layers our MLP has, when we want to train it, we compare the output of the -th layer with the desired results and compute the error. Suppose we consider the least mean squares error criterion:
We use the machinery we built for differentiation to compute . Computing the derivative with respect to the biases is completely similar:
Now it is enough to compute each component.
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.
The symbol represents Hadamard, or element-wise, multiplication. On the other hand, when 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:
It is not a bad idea to see what that in the middle actually does to the error value (). We start with the error itself:
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:
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 :
It is a good exercise to see why the relation above holds. Examining what form 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 does not have dimensions similar to ; 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 itself (of course because 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 . Now we have the derivative, and we also have the dimensions of the matrices . 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 operator. Look at this very MLP example:
The value has dimensions exactly equal to the original matrix!
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!