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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
//! Hyperbolic tangent family activation functions
//!
//! This module provides various tanh-based activation functions including
//! standard tanh, softsign, hardtanh, and tanhshrink variants.
use ;
use Tensor;
/// Hyperbolic tangent activation function
///
/// **Mathematical Definition:**
/// ```text
/// tanh(x) = (e^x - e^(-x)) / (e^x + e^(-x)) = (e^(2x) - 1) / (e^(2x) + 1)
/// ```
///
/// **Relationship to Sigmoid:**
/// ```text
/// tanh(x) = 2σ(2x) - 1, where σ is the sigmoid function
/// σ(x) = (1 + tanh(x/2)) / 2
/// ```
///
/// **Derivative:**
/// ```text
/// d/dx tanh(x) = 1 - tanh²(x) = sech²(x)
/// ```
///
/// **Properties:**
/// - **Range**: (-1, 1) - strictly between -1 and 1
/// - **Zero-centered**: Unlike sigmoid, outputs are centered around zero
/// - **Odd function**: tanh(-x) = -tanh(x)
/// - **Monotonic**: Strictly increasing function
/// - **Smooth**: Infinitely differentiable everywhere
/// - **Limit behavior**:
/// - lim(x→∞) tanh(x) = 1
/// - lim(x→-∞) tanh(x) = -1
/// - tanh(0) = 0
///
/// **Applications:**
/// - **Hidden layers**: Better than sigmoid due to zero-centered outputs
/// - **RNNs**: Traditional activation for vanilla RNNs
/// - **LSTM gates**: Used in some LSTM gate implementations
/// - **Normalization**: When you need outputs in [-1, 1] range
/// - **Control systems**: Natural for systems requiring bipolar outputs
///
/// **Advantages:**
/// - **Zero-centered**: Helps with optimization convergence
/// - **Stronger gradients**: Less gradient vanishing compared to sigmoid
/// - **Bounded output**: Prevents exploding activations
/// - **Smooth everywhere**: Better optimization properties than ReLU
///
/// **Disadvantages:**
/// - **Still saturates**: Vanishing gradient problem for |x| > 2.5
/// - **Computationally expensive**: Requires exponential calculations
/// - **Gradient magnitude**: Max gradient is 1, can slow learning
///
/// **Numerical Stability:**
/// For large |x|, direct computation may overflow. Implementation should use:
/// ```text
/// tanh(x) = sign(x) * (1 - 2/(exp(2|x|) + 1)) for |x| > threshold
/// ```
/// Softsign activation function
///
/// **Mathematical Definition:**
/// ```text
/// softsign(x) = x / (1 + |x|)
/// ```
///
/// **Alternative Form:**
/// ```text
/// softsign(x) = x / (1 + abs(x))
/// ```
///
/// **Derivative:**
/// ```text
/// d/dx softsign(x) = 1 / (1 + |x|)²
/// ```
///
/// **Properties:**
/// - **Range**: (-1, 1) - strictly between -1 and 1
/// - **Zero-centered**: Outputs are centered around zero
/// - **Odd function**: softsign(-x) = -softsign(x)
/// - **Monotonic**: Strictly increasing function
/// - **Smooth**: Differentiable everywhere except at x = 0 (but continuous)
/// - **Limit behavior**:
/// - lim(x→∞) softsign(x) = 1
/// - lim(x→-∞) softsign(x) = -1
/// - softsign(0) = 0
///
/// **Comparison to Tanh:**
/// - **Softer saturation**: Slower approach to asymptotes than tanh
/// - **Polynomial denominator**: Uses 1 + |x| instead of exponential
/// - **Gentler gradients**: More gradual gradient decay
/// - **Computational efficiency**: No exponential calculations required
///
/// **Applications:**
/// - **Alternative to tanh**: When you want slower saturation
/// - **Normalization layers**: Simple bounded activation
/// - **Gradient flow**: When you need gentler gradient behavior
/// - **Resource-constrained models**: More efficient than tanh
///
/// **Advantages:**
/// - **Computationally efficient**: No exponential operations
/// - **Gentler saturation**: Slower decay than tanh
/// - **Zero-centered**: Good for optimization
/// - **Smooth gradients**: No abrupt changes in gradient
///
/// **Disadvantages:**
/// - **Still saturates**: Though more slowly than tanh
/// - **Less common**: Fewer empirical studies compared to tanh/sigmoid
/// - **Non-differentiable at zero**: Though continuous and has one-sided derivatives
/// Hardtanh activation function
///
/// **Mathematical Definition:**
/// ```text
/// hardtanh(x) = max(min_val, min(max_val, x))
/// ```
/// Typically with min_val = -1 and max_val = 1:
/// ```text
/// hardtanh(x) = {
/// -1 if x < -1
/// x if -1 ≤ x ≤ 1
/// 1 if x > 1
/// }
/// ```
///
/// **Derivative:**
/// ```text
/// d/dx hardtanh(x) = {
/// 0 if x < min_val
/// 1 if min_val ≤ x ≤ max_val
/// 0 if x > max_val
/// }
/// ```
///
/// **Properties:**
/// - **Piecewise linear**: Three linear segments
/// - **Bounded output**: Strictly bounded between min_val and max_val
/// - **Zero-centered**: When min_val = -max_val
/// - **Computationally efficient**: Simple clipping operation
/// - **Non-differentiable**: At boundary points (min_val, max_val)
///
/// **Comparison to Tanh:**
/// - **Linear center**: Identity function in the middle region
/// - **Hard boundaries**: Abrupt clipping vs smooth asymptotes
/// - **Faster computation**: No exponential calculations
/// - **Sparse gradients**: Gradients are 0 outside the linear region
///
/// **Applications:**
/// - **Mobile/edge computing**: Efficient alternative to tanh
/// - **Quantized networks**: Works well with low-precision arithmetic
/// - **ReLU alternative**: When you need bounded activation
/// - **Hardware implementations**: Easy to implement in fixed-point
///
/// **Advantages:**
/// - **Extremely fast**: Simple min/max operations
/// - **Memory efficient**: No intermediate exponential computations
/// - **Bounded**: Prevents exploding activations
/// - **Hardware friendly**: Easy to implement in specialized hardware
///
/// **Disadvantages:**
/// - **Non-smooth**: Discontinuous derivatives at boundaries
/// - **Sparse gradients**: Zero gradients in saturation regions
/// - **Less expressive**: Linear center may be limiting
/// - **Gradient flow**: Can block gradients completely in saturation
/// Tanhshrink activation function
///
/// **Mathematical Definition:**
/// ```text
/// tanhshrink(x) = x - tanh(x)
/// ```
///
/// **Derivative:**
/// ```text
/// d/dx tanhshrink(x) = 1 - (1 - tanh²(x)) = tanh²(x)
/// ```
///
/// **Properties:**
/// - **Range**: (-∞, ∞) - unbounded output
/// - **Zero at origin**: tanhshrink(0) = 0 - tanh(0) = 0
/// - **Odd function**: tanhshrink(-x) = -tanhshrink(x)
/// - **Monotonic**: Strictly increasing function
/// - **Asymptotic behavior**:
/// - For large positive x: tanhshrink(x) ≈ x - 1
/// - For large negative x: tanhshrink(x) ≈ x + 1
/// - For small x: tanhshrink(x) ≈ x³/3 (Taylor expansion)
///
/// **Physical Interpretation:**
/// Represents the "excess" of the input beyond what tanh can represent.
/// It's the residual after tanh normalization.
///
/// **Gradient Properties:**
/// - **Never zero**: Gradient is always tanh²(x) ≥ 0
/// - **Small near origin**: Gradient approaches 0 as x → 0
/// - **Approaches 1**: Gradient approaches 1 as |x| → ∞
/// - **Self-regularizing**: Automatically reduces gradients near zero
///
/// **Applications:**
/// - **Residual connections**: Where you want to preserve input information
/// - **Attention mechanisms**: As a gating function
/// - **Normalization**: When you need to remove bounded components
/// - **Research models**: Specialized architectures requiring unbounded outputs
///
/// **Advantages:**
/// - **Unbounded**: No saturation issues for large inputs
/// - **Identity-like**: Becomes nearly linear for large |x|
/// - **Smooth gradients**: No abrupt gradient changes
/// - **Self-normalizing**: Automatically handles different input scales
///
/// **Disadvantages:**
/// - **Vanishing gradients**: Very small gradients near zero
/// - **Computational cost**: Requires tanh computation
/// - **Less studied**: Limited empirical validation
/// - **Complex behavior**: Non-intuitive activation pattern