# Neurons (Activation Functions)¶

Neurons can be attached to any layer. The neuron of each layer will affect the output in the forward pass and the gradient in the backward pass automatically unless it is an identity neuron. Layers have an identity neuron by default .

class Neurons.Identity

An activation function that does not change its input.

class Neurons.ReLU

Rectified Linear Unit. During the forward pass, it inhibits all inhibitions below some threshold $$\epsilon$$, typically 0. In other words, it computes point-wise $$y=\max(\epsilon, x)$$. The point-wise derivative for ReLU is

$\begin{split}\frac{dy}{dx} = \begin{cases}1 & x > \epsilon \\ 0 & x \leq \epsilon\end{cases}\end{split}$
epsilon

Specifies the minimum threshold at which the neuron will truncate. Default 0.

Note

ReLU is actually not differentiable at $$\epsilon$$. But it has subdifferential $$[0,1]$$. Any value in that interval can be taken as a subderivative, and can be used in SGD if we generalize from gradient descent to subgradient descent. In the implementation, we choose the subgradient at $$x==0$$ to be 0.

class Neurons.LReLU

Leaky Rectified Linear Unit. A Leaky ReLU can help fix the “dying ReLU” problem. ReLU’s can “die” if a large enough gradient changes the weights such that the neuron never activates on new data.

$\begin{split}\frac{dy}{dx} = \begin{cases}1 & x > 0 \\ 0.01 & x \leq 0\end{cases}\end{split}$
class Neurons.Sigmoid

Sigmoid is a smoothed step function that produces approximate 0 for negative input with large absolute values and approximate 1 for large positive inputs. The point-wise formula is $$y = 1/(1+e^{-x})$$. The point-wise derivative is

$\frac{dy}{dx} = \frac{-e^{-x}}{\left(1+e^{-x}\right)^2} = (1-y)y$
class Neurons.Tanh

Tanh is a transformed version of Sigmoid, that takes values in $$\pm 1$$ instead of the unit interval. input with large absolute values and approximate 1 for large positive inputs. The point-wise formula is $$y = (1-e^{-2x})/(1+e^{-2x})$$. The point-wise derivative is

$\frac{dy}{dx} = 4e^{2x}/(e^{2x} + 1)^2 = (1-y^2)$
class Neurons.Exponential

The exponential function.

$y = exp(x)$
  This is actually not true: not all layers in Mocha support neurons. For example, data layers currently does not have neurons, but this feature could be added by simply adding a neuron property to the data layer type. However, for some layer types like loss layers or accuracy layers, it does not make much sense to have neurons.
{"ft_lang_label":"__label__en","ft_lang_prob":0.7659438,"math_prob":0.99638176,"size":2511,"snap":"2021-31-2021-39","text_gpt3_token_len":692,"char_repetition_ratio":0.10889509,"word_repetition_ratio":0.092307694,"special_character_ratio":0.27678216,"punctuation_ratio":0.09615385,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9992511,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-07-25T18:43:14Z\",\"WARC-Record-ID\":\"<urn:uuid:153ee3ed-644e-452e-96e3-4e9537041552>\",\"Content-Length\":\"17110\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:539a5644-7c74-4870-9a19-3fe4d00b93ac>\",\"WARC-Concurrent-To\":\"<urn:uuid:6d795f6d-0d61-43de-b072-d747b31c779d>\",\"WARC-IP-Address\":\"104.17.33.82\",\"WARC-Target-URI\":\"https://mochajl.readthedocs.io/en/latest/user-guide/neuron.html\",\"WARC-Payload-Digest\":\"sha1:2XFSOXIGDTA76Y4J34UMJJJ4K3CW5VDY\",\"WARC-Block-Digest\":\"sha1:WVWE77WT2LWL7CKQGS5QI2M7KYNIPTGA\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-31/CC-MAIN-2021-31_segments_1627046151760.94_warc_CC-MAIN-20210725174608-20210725204608-00226.warc.gz\"}"}