File size: 7,954 Bytes
db1f0f8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
---

title: "Activation functions in Neural Networks"
author: "Sébastien De Greef"
format:
  revealjs: 
    theme: solarized
    navigation-mode: grid 
    controls-layout: bottom-right
    controls-tutorial: true

---

# Activation functions

When choosing an activation function, consider the following:

-   **Non-saturation:** Avoid activations that saturate (e.g., sigmoid, tanh) to prevent vanishing gradients.

-   **Computational efficiency:** Choose activations that are computationally efficient (e.g., ReLU, Swish) for large models or real-time applications.

-   **Smoothness:** Smooth activations (e.g., GELU, Mish) can help with optimization and convergence.

-   **Domain knowledge:** Select activations based on the problem domain and desired output (e.g., softmax for multi-class classification).

-   **Experimentation:** Try different activations and evaluate their performance on your specific task.

## Sigmoid {#sec-sigmoid}

**Strengths:** Maps any real-valued number to a value between 0 and 1, making it suitable for binary classification problems.

**Weaknesses:** Saturates (i.e., output values approach 0 or 1) for large inputs, leading to vanishing gradients during backpropagation.

**Usage:** Binary classification, logistic regression.

::: columns
::: {.column width="50%"}
$$ 
\sigma(x) = \frac{1}{1 + e^{-x}}
$$

``` python

def sigmoid(x):

    return 1 / (1 + np.exp(-x))

```
:::

::: {.column width="50%"}
{{< embed ActivationFunctions.ipynb#fig-sigmoid >}}
:::
:::

## Hyperbolic Tangent (Tanh) {#sec-tanh}

**Strengths:** Similar to sigmoid, but maps to (-1, 1), which can be beneficial for some models.

**Weaknesses:** Also saturates, leading to vanishing gradients.

**Usage:** Similar to sigmoid, but with a larger output range.

::: columns
::: {.column width="50%"}
$$
\tanh(x) = \frac{e^{x} - e^{-x}}{e^{x} + e^{-x}}
$$

``` python

def tanh(x):

    return np.tanh(x)

```
:::

::: {.column width="50%"}
{{< embed ActivationFunctions.ipynb#fig-tanh >}}
:::
:::

## Rectified Linear Unit (ReLU)

**Strengths:** Computationally efficient, non-saturating, and easy to compute.

**Weaknesses:** Not differentiable at x=0, which can cause issues during optimization.

**Usage:** Default activation function in many deep learning frameworks, suitable for most neural networks.

::: columns
::: {.column width="50%"}
$$
\text{ReLU}(x) = \max(0, x)
$$

``` python

def relu(x):

    return np.maximum(0, x)

```
:::

::: {.column width="50%"}
{{< embed ActivationFunctions.ipynb#fig-relu >}}
:::
:::

## Leaky ReLU

**Strengths:** Similar to ReLU, but allows a small fraction of the input to pass through, helping with dying neurons.

**Weaknesses:** Still non-differentiable at x=0.

**Usage:** Alternative to ReLU, especially when dealing with dying neurons.

::: columns
::: {.column width="50%"}
$$
\text{Leaky ReLU}(x) = 
\begin{cases} 
x & \text{if } x > 0 \\
\alpha x & \text{if } x \leq 0 
\end{cases}
$$

``` python

def leaky_relu(x, alpha=0.01):

    # where α is a small constant (e.g., 0.01)

    return np.where(x > 0, x, x * alpha)

```
:::

::: {.column width="50%"}
{{< embed ActivationFunctions.ipynb#fig-leaky_relu >}}

:::

:::



## Swish



**Strengths:** Self-gated, adaptive, and non-saturating.



**Weaknesses:** Computationally expensive, requires additional learnable parameters.



**Usage:** Can be used in place of ReLU or other activations, but may not always outperform them.



::: columns

::: {.column width="50%"}

$$

\text{Swish}(x) = x \cdot \sigma(x)

$$



``` python

def swish(x):

    return x * sigmoid(x)

```



See also: [sigmoid](#sec-sigmoid)

:::



::: {.column width="50%"}

{{< embed ActivationFunctions.ipynb#fig-swish >}}

:::

:::



## Mish



**Strengths:** Non-saturating, smooth, and computationally efficient.



**Weaknesses:** Not as well-studied as ReLU or other activations.



**Usage:** Alternative to ReLU, especially in computer vision tasks.



::: columns

::: {.column width="50%"}

$$

\text{Mish}(x) = x \cdot \tanh(\text{Softplus}(x))

$$



``` python

def mish(x):

    return x * np.tanh(softplus(x))

```

:::



::: {.column width="50%"}

{{< embed ActivationFunctions.ipynb#fig-mish >}}

:::

:::



See also: [softplus](#softplus) [tanh](#sec-tanh)



## Softmax



**Strengths:** Normalizes output to ensure probabilities sum to 1, making it suitable for multi-class classification.



**Weaknesses:** Only suitable for output layers with multiple classes.



**Usage:** Output layer activation for multi-class classification problems.



::: columns

::: {.column width="50%"}

$$

\text{Softmax}(x_i) = \frac{e^{x_i}}{\sum_{k=1}^{K} e^{x_k}}

$$



``` python

def softmax(x):

    e_x = np.exp(x - np.max(x))

    return e_x / e_x.sum()
```

:::



::: {.column width="50%"}

{{< embed ActivationFunctions.ipynb#fig-softmax >}}

:::

:::



## Softsign



**Strengths:** Similar to sigmoid, but with a more gradual slope.



**Weaknesses:** Not commonly used, may not provide significant benefits over sigmoid or tanh.



**Usage:** Alternative to sigmoid or tanh in certain situations.



::: columns

::: {.column width="50%"}

$$

\text{Softsign}(x) = \frac{x}{1 + |x|}

$$



``` python
def softsign(x):
    return x / (1 + np.abs(x))
```

:::



::: {.column width="50%"}

{{< embed ActivationFunctions.ipynb#fig-softsign >}}

:::

:::



## SoftPlus {#softplus}



**Strengths:** Smooth, continuous, and non-saturating.



**Weaknesses:** Not commonly used, may not outperform other activations.



**Usage:** Experimental or niche applications.



::: columns

::: {.column width="50%"}

$$

\text{Softplus}(x) = \log(1 + e^x)

$$



``` python
def softplus(x):
    return np.log1p(np.exp(x))
```

:::



::: {.column width="50%"}

{{< embed ActivationFunctions.ipynb#fig-softplus >}}

:::

:::



## ArcTan



**Strengths:** Non-saturating, smooth, and continuous.



**Weaknesses:** Not commonly used, may not outperform other activations.



**Usage:** Experimental or niche applications.



::: columns

::: {.column width="50%"}

$$

arctan(x) = arctan(x)

$$



``` python
def arctan(x):
    return np.arctan(x)
```

:::



::: {.column width="50%"}

{{< embed ActivationFunctions.ipynb#fig-arctan >}}

:::

:::



## Gaussian Error Linear Unit (GELU)



**Strengths:** Non-saturating, smooth, and computationally efficient.



**Weaknesses:** Not as well-studied as ReLU or other activations.



**Usage:** Alternative to ReLU, especially in Bayesian neural networks.



::: columns

::: {.column width="50%"}

$$

\text{GELU}(x) = x \cdot \Phi(x)

$$



``` python
def gelu(x):
    return 0.5 * x 
        * (1 + np.tanh(np.sqrt(2 / np.pi) 
        * (x + 0.044715 * np.power(x, 3))))
```

:::



::: {.column width="50%"}

{{< embed ActivationFunctions.ipynb#fig-gelu >}}

:::

:::



See also: [tanh](#sec-tanh)



## Silu (SiLU)



$$

silu(x) = x * sigmoid(x)

$$



**Strengths:** Non-saturating, smooth, and computationally efficient.



**Weaknesses:** Not as well-studied as ReLU or other activations.



**Usage:** Alternative to ReLU, especially in computer vision tasks.



## GELU Approximation (GELU Approx.)



$$

f(x) ≈ 0.5 * x * (1 + tanh(√(2/π) * (x + 0.044715 * x^3)))

$$



**Strengths:** Fast, non-saturating, and smooth.



**Weaknesses:** Approximation, not exactly equal to GELU.



**Usage:** Alternative to GELU, especially when computational efficiency is crucial.



## SELU (Scaled Exponential Linear Unit)



$$

f(x) = \lambda 

\begin{cases} 

x & x > 0 \\

\alpha e^x - \alpha & x \leq 0 

\end{cases}

$$



**Strengths:** Self-normalizing, non-saturating, and computationally efficient.



**Weaknesses:** Requires careful initialization and α tuning.



**Usage:** Alternative to ReLU, especially in deep neural networks.



\listoffigures