Gradient computation
The example you find is calculating the gradient for the ReLU function, whose gradient is
$$\text{ReLU}'(x)=\left\{
\begin{array}{c l} 
     1 &amp; \text{if } x&gt;0\\
     0 &amp; \text{if } x&lt;0
\end{array}\right.$$ 

Therefore when x&lt;0, you make the gradient 0 by grad_x[x &lt; 0] = 0.