How to find the gradient of norm square 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$.
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) &amp;= h'(g(x)) g'(x) \\
&amp;= 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}
Well, we know $x = (x_1,...,x_d) $, hence,

$$ ||x||^2 = x_1^2 + ... + x_d^2 $$

Now,

$$ \frac{ \partial f}{\partial x_1 } = 2x_1$$

$$ \frac{ \partial f}{\partial x_2 } = 2x_2 $$

....

$$ \frac{ \partial f}{\partial x_k } = 2x_k $$

Hence, 

$$ grad(f) = (2x_1,....,2x_d) = 2 x $$