TITLE: How to find the gradient of norm square
QUESTION [6 upvotes]: How to find the gradient of the function $f(x) = ||g(x)||^2$  
where $x \in \Bbb R^d$ and $g: \Bbb R^d \to \Bbb R$.

REPLY [9 votes]: I will assume $g:\mathbb R^d \to \mathbb R^n$.
It's nice to avoid using components.  Notice that
\begin{equation}
f(x) = h(g(x))
\end{equation}
where $h(x) = \|x\|^2$.
The chain rule tells us that
\begin{align}
f'(x) &= h'(g(x)) g'(x) \\
&= 2 \underbrace{g(x)^T}_{1 \times n} \underbrace{g'(x)}_{n \times d}.
\end{align}
If we use the convention that $\nabla f(x)$ is a column vector, rather than a row vector, then
\begin{equation}
\nabla f(x) = f'(x)^T = 2 g'(x)^T g(x).
\end{equation}