Written on January 19, 2019
NOTE: This post is automatically translated from Farsi using GPT 5.5
It is obvious from everything about me how much I love mathematics. Nevertheless, it was in the second year of my master’s that I was forced to seriously go after differentiating functions with matrix inputs. In the end, it was a small 14-page booklet by a professor of agricultural economics that got my work going.
Now, after more than 12 years, differentiating this kind of function is no longer considered a serious concern. These days, toolkits like PyTorch and TensorFlow themselves take derivatives for you from the function you have built, and do a thousand other things too. Nevertheless, having knowledge of differentiating functions with matrix variables can still be useful for those who wrestle with the details of optimizing their models (at least this statement is a good excuse for me to write about it!)
Examples of functions with matrix inputs are abundant. One of the most famous is our very own Gaussian distribution function. The multivariate Gaussian distribution function:
If we have a series of samples from this distribution and want to estimate the matrix , we will be dealing with functions whose input is a matrix.
Other examples of these functions are various kinds of neural network models. In most types of neural networks, there are a series of weight matrices, and for optimization and finding the model parameters, we will end up dealing with functions of these very weight matrices.
To see one example through to the end, let us go back again to estimating the matrix . We have samples of , which we denote by and up to . We want to find a that maximizes the probability of occurrence of these samples. We also assume that the s are all IID; that is, they all have the same distribution and are also independent of one another. With these explanations:
is the function that must be maximized with respect to its input, namely . A function with a matrix input from which we must take the derivative with respect to the input, set it equal to zero, and find the optimum value.
If we want to differentiate functions with matrix inputs in a principled way, we will end up dealing with tensors and tensor operators. But there is also a simpler way. This simple way is based on a neat perspective: “When computing a derivative, the arrangement of the elements of a matrix is a matter of secondary importance.”
When we approach differentiation with respect to matrices with this view, we no longer pay heed to the arrangement of the elements of the matrix! Every matrix is a large vector together with two numbers that represent how the elements are arranged. For example:
If you pay attention, in this new representation nothing has really been lost. We can reconstruct the first matrix again. We have a vector that is the columns of the original matrix glued together, and an ordered pair that represents the dimensions of the matrix.
You may ask what use this is. But I will not answer that question right now! First, I should say that gluing together the columns of a matrix is a mathematical operator that even has a name. In matrix differentiation, it is called the operator.
Let us return to the question above: when we represent matrices as vectors, differentiation with respect to matrices becomes differentiation with respect to vectors, which is fundamentally familiar, and we know this from Mathematics 2 in the first year of university. You remember the Jacobian matrix.
In Mathematics 2 in the first year of university, we saw that to differentiate functions with respect to vectors, by definition we proceed as follows:
Well, we use this very same method to take derivatives with respect to matrices too. We consider the derivative with respect to a matrix to be the derivative with respect to the -ed form of that matrix:
More precisely:
And that’s that. We no longer need tensor algebra, nor those covectors and contravectors! The only thing that descends from tensor algebra down to the level of matrices and that we need is the Kronecker product. For the time being, forget the Kronecker product. First let us enjoy this simplification a bit; there will be time for a deeper look.
Let us differentiate a matrix with respect to itself. To do this, we must turn it into a vector:
Then it is enough to compute the Jacobian matrix:
The Jacobian in this case simply becomes the identity matrix with dimensions . Well, this was exactly what was expected. Now, a derivative that is somewhat more complicated. Suppose we know the single-variable function . We apply it to all the elements of the matrix , and we call this operation as well:
What does the derivative become? With calculations similar to the calculation of , we have:
This means a matrix whose only nonzero elements are on the main diagonal, and whose values are the values of the derivatives of at the elements of .
The only thing I should add in this opening post is that differentiation with respect to matrices is also a linear operator. This means that scalar multiplication and addition have the distributive property with respect to this operator:
So far, we have seen that if we temporarily ignore the form of the arrangement of variables inside a matrix, we arrive at a set of well-structured definitions for differentiation with respect to a matrix. To do this, we use the operator, which glues together the columns of the input matrix and creates a long vector. For convenience, from now on, wherever I write , what I mean is that same . In the next post, I will address more interesting parts of differentiation with respect to matrices.