Why the Lasso cost function isn't differentiable at $\theta_i=0$ and what is the effect of $g$? I'm reading Hands-On Machine Learning by Aurélien Géron.  The author states that the Lasso cost function isn't differentiable at $\theta_i=0$ so we use a subgradient vector $g$ instead of the gradient vector.  The cost function is:
$$J(\boldsymbol{\theta}) = \text{MSE}(\boldsymbol{\theta}) + \alpha \sum_{i=1}^n |\theta_i|,$$
and the subgradient function is:
$$g(\boldsymbol{\theta},J) = \nabla_\boldsymbol{\theta} \ \text{MSE}(\boldsymbol{\theta}) + \alpha \begin{bmatrix} \text{sgn}(\theta_1) \\ \text{sgn}(\theta_2) \\ \vdots \\ \text{sgn}(\theta_n) \\ \end{bmatrix}.$$
where $\text{sgn}$ is the sign function.  Can someone please explain why the cost function $J$ isn't differentiable at $\theta_i = 0$, and explain the effect of the function $g$?
 A: This is an issue that arises when differentiating the absolute value function.  Consider the function:
$$f(\theta) = |\theta|.$$
If you take the derivative in the classical limiting sense then you get the directional derivatives:
$$\begin{align}
f_\downarrow'(\theta) 
&\equiv \lim_{\Delta \downarrow 0} \frac{f(\theta+\Delta)-f(\theta)}{\Delta} \\[12pt]
&= \lim_{\Delta \downarrow 0} \frac{|\theta+\Delta|-|\theta|}{\Delta} \\[12pt]
&= \begin{cases}
-1 & & & \text{if } \theta < 0, \\[6pt]
 1 & & & \text{if } \theta \geqslant 0, \\[6pt]
\end{cases} \\[16pt]
f_\uparrow'(\theta) 
&\equiv \lim_{\Delta \uparrow 0} \frac{f(\theta+\Delta)-f(\theta)}{\Delta} \\[12pt]
&= \lim_{\Delta \uparrow 0} \frac{|\theta+\Delta|-|\theta|}{\Delta} \\[12pt]
&= \begin{cases}
-1 & & & \text{if } \theta \leqslant 0, \\[6pt]
 1 & & & \text{if } \theta > 0. \\[6pt]
\end{cases} \\[16pt]
\end{align}$$
Since the directional derivatives are not the same at $\theta = 0$, there is no overall "derivative" of the function at this point in the usual (strong) sense.  However, if one considers a weaker form of derivative (e.g., the weak derivative or the Radon-Nikodym derivative, which is a kind of anti-integral) then one can take the derivative as:
$$f'(\theta) = \text{sgn}(\theta).$$
This latter result is the weak derivative of the absolute value function, and it is also a valid Radon-Nikodym derivative of the function.  It is what we usually take to be "the derivative" of the absolute value function.  Observe that it matches with the classical (strong) definition of the derivative at all points $\theta \neq 0$ and it is also well-defined at $\theta = 0$.
The subgradient vector $\boldsymbol{g}$ in this case gives the weak partial deriviatives of the cost function, using the above weak derivative of the absolute value function.  The function is also a Radon-Nikodym derivative of the cost function, so if you integrate the subgradient you will get back to the original cost function.  You can reasonably regard this as "the derivative" of the cost function in the present case.
