TITLE: Not understanding derivative of a matrix-matrix product.
QUESTION [36 upvotes]: I am trying to figure out a the derivative of a matrix-matrix multiplication, but to no avail. This document seems to show me the answer, but I am having a hard time parsing it and understanding it.
Here is my problem: We have $\mathbf{D} \in \Re^{m n}$, $\mathbf{W} \in \Re^{m q}$, and $\mathbf{X} \in \Re^{q n}$. Furthermore, $\mathbf{D} = \mathbf{W}\mathbf{X}$. (NOT an element wise multiplication - a normal matrix-matrix multiply).
I am trying to derive the derivative of $\mathbf{D}$, w.r.t $\mathbf{W}$, and the derivative of $\mathbf{D}$, w.r.t $\mathbf{X}$.
My class note this is taken from seems to indicate that
$$
\frac{\delta \mathbf{D}}{\delta \mathbf{W}} = \mathbf{X}^{T} \text{ and that } \frac{\delta \mathbf{D}}{\delta \mathbf{X}} = \mathbf{W}^{T}, 
$$
but I am floored as to how he derived this. Furthermore, in taking the derivatives, we are asking ourselves how every element in $\mathbf{D}$ changes with perturbations by every element in, say, $\mathbf{X}$, - so wouldn't the resulting combinations blow up to be a-lot more than what $\mathbf{W}^{T}$ has? I cant even see how the dimensionality is right here.
EDIT: Id like to add the context of this question. It's coming from here, and here is my marked screen-shot of my problem. How are they deriving those terms? (Note: I understand the chain-rule aspect, and I am not wondering about that. I am asking about the simpler intermediate step).

Thanks.

REPLY [10 votes]: Like most articles on Machine Learning / Neural Networks, the linked document is an awful mixture of code snippets and poor mathematical notation.
If you read the comments preceding the code snippet, you'll discover that dX does not refer to an increment or differential of $X,$ or to
the matrix-by-matrix derivative  $\frac{\partial W}{\partial X}.\;$
Instead it is supposed to represent $\frac{\partial \phi}{\partial X}$, i.e. the gradient of an unspecified objective function $\Big({\rm i.e.}\;\phi(D)\Big)$ with respect to one of the factors of the matrix argument: $\;D=WX$.
Likewise, dD does not refer to an increment (or differential) of D but to the gradient $\frac{\partial \phi}{\partial D}$
Here is a short derivation of the mathematical content of the code snippet.
$$\eqalign{
D &= WX \\
dD &= dW\,X + W\,dX
 \quad&\big({\rm differential\,of\,}D\big) \\
\frac{\partial\phi}{\partial D} &= G
 \quad&\big({\rm gradient\,wrt\,}D\big) \\
d\phi &= G:dD
 \quad&\big({\rm differential\,of\,}\phi\big) \\
 &= G:dW\,X \;+ G:W\,dX \\
 &= GX^T\!:dW + W^TG:dX \\
\frac{\partial\phi}{\partial W} &= GX^T
 \quad&\big({\rm gradient\,wrt\,}W\big) \\
\frac{\partial\phi}{\partial X} &= W^TG
 \quad&\big({\rm gradient\,wrt\,}X\big) \\
}$$
Unfortunately, the author decided to use the following variable names in the code:

 dD   for $\;\frac{\partial\phi}{\partial D}$
 dX   for $\;\frac{\partial\phi}{\partial X}$
 dW   for $\;\frac{\partial\phi}{\partial W}$

With this in mind, it is possible to make sense of the code snippet
$$\eqalign{
{\bf dW} &= {\bf dD}\cdot{\bf X}^T \\
{\bf dX} &= {\bf W}^T\cdot{\bf dD} \\
}$$
but the notation is extremely confusing for anyone who is mathematically inclined.

(NB: This answer simply reiterates points made in GeorgSaliba's excellent post)