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
//! Provides utility functionality when working with common activation (or transfer) functions.

use ndarray::NdFloat;

/// Represents an activation function.
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde_support", derive(Serialize, Deserialize))]
pub enum Activation {
	/// Identity: *ƒ(x) = x*
	Identity,

	/// Binary Step:
	/// *ƒ(x) = 0* **if** *x < 0*
	/// *ƒ(x) = 1* **if** *x ≥ 0*
	BinaryStep,

	/// Logistic function
	Logistic,

	/// Tangens Hyperbolicus (**tanh**): *ƒ(x) = tanh(x)*
	Tanh,

	/// Arcus Tangens (**atan**): *ƒ(x) = atan(x)*
	ArcTan,

	/// SoftSign: *ƒ(x) = x ⋅ (1 + |x|)⁻¹*
	SoftSign,

	/// ReLU:
	/// *ƒ(x) = 0* **if** *x < 0*
	/// *ƒ(x) = x* **else**
	ReLU,

	/// SoftPlus: *ƒ(x) = __ln__(1 + eˣ)*
	SoftPlus,

	/// Bent Identity: *ƒ(x) = ½(__sqrt__(x² + 1) - 1) + x*
	BentIdentity,

	/// Sinusoid: *ƒ(x) = __sin__(x)*
	Sinusoid,

	/// Gaussian:  *ƒ(x) = e⁻ˣˣ*
	Gaussian,
}

impl Activation {
	/// Returns `f(x)` with a given `x` and `f` as the base function.
	pub fn base<F: NdFloat>(self, x: F) -> F {
		use self::details::*;
		use self::Activation::*;
		match self {
			Identity     => identity(x),
			BinaryStep   => binary_step(x),
			Logistic     => logistic(x),
			Tanh         => tanh(x),
			ArcTan       => arctan(x),
			SoftSign     => softsign(x),
			ReLU         => relu(x),
			SoftPlus     => softplus(x),
			BentIdentity => bent_identity(x),
			Sinusoid     => sinusoid(x),
			Gaussian     => gaussian(x),
		}
	}

	/// Returns `dx(x)` with a given `x` and `dx` as the derived function.
	pub fn derived<F: NdFloat>(self, x: F) -> F {
		use self::details::*;
		use self::Activation::*;
		match self {
			Identity     => identity_dx(x),
			BinaryStep   => binary_step_dx(x),
			Logistic     => logistic_dx(x),
			Tanh         => tanh_dx(x),
			ArcTan       => arctan_dx(x),
			SoftSign     => softsign_dx(x),
			ReLU         => relu_dx(x),
			SoftPlus     => softplus_dx(x),
			BentIdentity => bent_identity_dx(x),
			Sinusoid     => sinusoid_dx(x),
			Gaussian     => gaussian_dx(x),
		}
	}
}

mod details {
	use ndarray::NdFloat;

	/// Identity: *ƒ(x) = x*
	pub fn identity<F: NdFloat>(x: F) -> F {
		x
	}
	/// Derivation of the Identity: *ƒ(x) = 1*
	pub fn identity_dx<F: NdFloat>(_: F) -> F {
		F::one()
	}

	/// Binary Step:
	/// *ƒ(x) = 0* **if** *x < 0*
	/// *ƒ(x) = 1* **if** *x ≥ 0*
	pub fn binary_step<F: NdFloat>(x: F) -> F {
		if x < F::zero() { F::zero() } else { F::one() }
	}
	/// Derivation of Binary Step: *ƒ(x) = 0, x ≠ 0*
	pub fn binary_step_dx<F: NdFloat>(x: F) -> F {
		if x != F::zero() {
			F::zero()
		} else {
			F::infinity()
		}
	}

	/// Logistic or Sigmoid
	pub fn logistic<F: NdFloat>(x: F) -> F {
		softplus_dx(x)
	}
	/// Derivation of Logistic or Sigmoid
	pub fn logistic_dx<F: NdFloat>(x: F) -> F {
		logistic(x) * (F::one() - logistic(x))
	}

	/// Tangens Hyperbolicus (**tanh**): *ƒ(x) = tanh(x)*
	pub fn tanh<F: NdFloat>(x: F) -> F {
		x.tanh()
	}
	/// Derivation of Tangens Hyperbolicus (**tanh⁻¹**): *ƒ(x) = 1 - tanh²(x)*
	pub fn tanh_dx<F: NdFloat>(x: F) -> F {
		let fx = tanh(x);
		F::one() - fx * fx
	}

	/// Arcus Tangens (**atan**): *ƒ(x) = atan(x)*
	pub fn arctan<F: NdFloat>(x: F) -> F {
		x.atan()
	}
	/// Derivation of Arcus Tangens (**atan⁻¹**): *ƒ(x) = (x² + 1)⁻¹*
	pub fn arctan_dx<F: NdFloat>(x: F) -> F {
		F::one() / (x * x + F::one())
	}

	/// SoftSign: *ƒ(x) = x ⋅ (1 + |x|)⁻¹*
	pub fn softsign<F: NdFloat>(x: F) -> F {
		x / (F::one() + x.abs())
	}
	/// Derivation of SoftSign: *ƒ(x) = ( (1 + |x|)² )⁻¹*
	pub fn softsign_dx<F: NdFloat>(x: F) -> F {
		let dx = F::one() + x.abs();
		F::one() / (dx * dx)
	}

	/// ReLU:
	/// *ƒ(x) = 0* **if** *x < 0*
	/// *ƒ(x) = x* **else**
	pub fn relu<F: NdFloat>(x: F) -> F {
		if x < F::zero() { F::zero() } else { x }
	}

	/// Derivation of ReLU:
	/// *ƒ(x) = 0* **if** *x < 0*
	/// *ƒ(x) = 1* **else**
	pub fn relu_dx<F: NdFloat>(x: F) -> F {
		if x < F::zero() { F::zero() } else { F::one() }
	}

	/// SoftPlus: *ƒ(x) = __ln__(1 + eˣ)*
	pub fn softplus<F: NdFloat>(x: F) -> F {
		x.exp().ln_1p()
	}
	/// Derivation of SoftPlus: *ƒ(x) = (1 + e⁻ˣ)⁻¹*
	pub fn softplus_dx<F: NdFloat>(x: F) -> F {
		F::one() / (F::one() + (-x).exp())
	}

	/// Bent Identity: *ƒ(x) = ½(__sqrt__(x² + 1) - 1) + x*
	pub fn bent_identity<F: NdFloat>(x: F) -> F {
		let two = F::from(2.0).unwrap();
		(((x * x) + F::one()).sqrt() - F::one()) / two + x
	}
	/// Derivation of Bent Identity: *ƒ(x) = x ⋅ (2 * __sqrt__(x² + 1))⁻¹ + 1*
	pub fn bent_identity_dx<F: NdFloat>(x: F) -> F {
		let two = F::from(2.0).unwrap();
		x / (two * ((x * x) + F::one()).sqrt()) + F::one()
	}

	/// Sinusoid: *ƒ(x) = __sin__(x)*
	pub fn sinusoid<F: NdFloat>(x: F) -> F {
		x.sin()
	}
	/// Derivation of Sinusoid: *ƒ(x) = __cos__(x)*
	pub fn sinusoid_dx<F: NdFloat>(x: F) -> F {
		x.cos()
	}

	/// Gaussian:  *ƒ(x) = e⁻ˣˣ*
	pub fn gaussian<F: NdFloat>(x: F) -> F {
		(-x * x).exp()
	}
	/// Derivation of Gaussian:  *ƒ(x) = -2xe⁻ˣˣ*
	pub fn gaussian_dx<F: NdFloat>(x: F) -> F {
		let two = F::from(2.0).unwrap();
		-two * x * gaussian(x)
	}
}

#[cfg(test)]
mod tests {
	use super::*;

	#[test]
	fn identity() {
		let act = Activation::Identity;
		relative_eq!(act.base(-1.0), -1.0);
		relative_eq!(act.base(-0.5), -0.5);
		relative_eq!(act.base(0.0), 0.0);
		relative_eq!(act.base(0.5), 0.5);
		relative_eq!(act.base(1.0), 1.0);
		relative_eq!(act.derived(-1.0), 1.0);
		relative_eq!(act.derived(-0.5), 1.0);
		relative_eq!(act.derived(0.0), 1.0);
		relative_eq!(act.derived(0.5), 1.0);
		relative_eq!(act.derived(1.0), 1.0);
	}

	#[test]
	fn binary_step() {
		use num::Float;
		let act = Activation::BinaryStep;
		relative_eq!(act.base(-1.0), 0.0);
		relative_eq!(act.base(-0.5), 0.0);
		relative_eq!(act.base(0.0), 1.0);
		relative_eq!(act.base(0.5), 1.0);
		relative_eq!(act.base(1.0), 1.0);
		relative_eq!(act.derived(-1.0), 0.0);
		relative_eq!(act.derived(-0.5), 0.0);
		relative_eq!(act.derived(0.0), <f64>::infinity());
		relative_eq!(act.derived(0.5), 0.0);
		relative_eq!(act.derived(1.0), 0.0);
	}

	#[test]
	fn logistic() {
		let act = Activation::Logistic;
		relative_eq!(act.base(-1.0), 0.26894143);
		relative_eq!(act.base(-0.5), 0.37754068);
		relative_eq!(act.base(0.0), 0.5);
		relative_eq!(act.base(0.5), 0.62245935);
		relative_eq!(act.base(1.0), 0.7310586);
		relative_eq!(act.derived(-1.0), 0.19661194);
		relative_eq!(act.derived(-0.5), 0.23500371);
		relative_eq!(act.derived(0.0), 0.25);
		relative_eq!(act.derived(0.5), 0.23500371);
		relative_eq!(act.derived(1.0), 0.19661193);
	}

	#[test]
	fn arctan() {
		let act = Activation::ArcTan;
		relative_eq!(act.base(-1.0), -0.7853982);
		relative_eq!(act.base(-0.5), -0.4636476);
		relative_eq!(act.base(0.0), 0.0);
		relative_eq!(act.base(0.5), 0.4636476);
		relative_eq!(act.base(1.0), 0.7853982);
		relative_eq!(act.derived(-1.0), 0.5);
		relative_eq!(act.derived(-0.5), 0.8);
		relative_eq!(act.derived(0.0), 1.0);
		relative_eq!(act.derived(0.5), 0.8);
		relative_eq!(act.derived(1.0), 0.5);
	}

	#[test]
	fn tanh() {
		let act = Activation::Tanh;
		relative_eq!(act.base(-1.0), -0.7615942);
		relative_eq!(act.base(-0.5), -0.46211717);
		relative_eq!(act.base(0.0), 0.0);
		relative_eq!(act.base(0.5), 0.46211717);
		relative_eq!(act.base(1.0), 0.7615942);
		relative_eq!(act.derived(-1.0), 0.41997433);
		relative_eq!(act.derived(-0.5), 0.7864477);
		relative_eq!(act.derived(0.0), 1.0);
		relative_eq!(act.derived(0.5), 0.7864477);
		relative_eq!(act.derived(1.0), 0.41997433);
	}

	#[test]
	fn softsign() {
		let act = Activation::SoftSign;
		relative_eq!(act.base(-1.0), -0.5);
		relative_eq!(act.base(-0.5), -0.33333334);
		relative_eq!(act.base(0.0), 0.0);
		relative_eq!(act.base(0.5), 0.33333334);
		relative_eq!(act.base(1.0), 0.5);
		relative_eq!(act.derived(-1.0), 0.25);
		relative_eq!(act.derived(-0.5), 0.44444445);
		relative_eq!(act.derived(0.0), 1.0);
		relative_eq!(act.derived(0.5), 0.44444445);
		relative_eq!(act.derived(1.0), 0.25);
	}

	#[test]
	fn relu() {
		let act = Activation::ReLU;
		relative_eq!(act.base(-1.0), 0.0);
		relative_eq!(act.base(-0.5), 0.0);
		relative_eq!(act.base(0.0), 0.0);
		relative_eq!(act.base(0.5), 0.5);
		relative_eq!(act.base(1.0), 1.0);
		relative_eq!(act.derived(-1.0), 0.0);
		relative_eq!(act.derived(-0.5), 0.0);
		relative_eq!(act.derived(0.0), 1.0);
		relative_eq!(act.derived(0.5), 1.0);
		relative_eq!(act.derived(1.0), 1.0);
	}

	#[test]
	fn softplus() {
		let act = Activation::SoftPlus;
		relative_eq!(act.base(-1.0), 0.3132617);
		relative_eq!(act.base(-0.5), 0.474077);
		relative_eq!(act.base(0.0), 0.6931472);
		relative_eq!(act.base(0.5), 0.974077);
		relative_eq!(act.base(1.0), 1.3132616);
		relative_eq!(act.derived(-1.0), 0.26894143);
		relative_eq!(act.derived(-0.5), 0.37754068);
		relative_eq!(act.derived(0.0), 0.5);
		relative_eq!(act.derived(0.5), 0.62245935);
		relative_eq!(act.derived(1.0), 0.7310586);
	}

	#[test]
	fn bent_identity() {
		let act = Activation::BentIdentity;
		relative_eq!(act.base(-1.0), -0.79289323);
		relative_eq!(act.base(-0.5), -0.440983);
		relative_eq!(act.base(0.0), 0.0);
		relative_eq!(act.base(0.5), 0.559017);
		relative_eq!(act.base(1.0), 1.2071068);
		relative_eq!(act.derived(-1.0), 0.6464466);
		relative_eq!(act.derived(-0.5), 0.7763932);
		relative_eq!(act.derived(0.0), 1.0);
		relative_eq!(act.derived(0.5), 1.2236068);
		relative_eq!(act.derived(1.0), 1.3535534);
	}

	#[test]
	fn sinusoid() {
		let act = Activation::Sinusoid;
		relative_eq!(act.base(-1.0), -0.84147096);
		relative_eq!(act.base(-0.5), -0.47942555);
		relative_eq!(act.base(0.0), 0.0);
		relative_eq!(act.base(0.5), 0.47942555);
		relative_eq!(act.base(1.0), 0.84147096);
		relative_eq!(act.derived(-1.0), 0.5403023);
		relative_eq!(act.derived(-0.5), 0.87758255);
		relative_eq!(act.derived(0.0), 1.0);
		relative_eq!(act.derived(0.5), 0.87758255);
		relative_eq!(act.derived(1.0), 0.5403023);
	}

	#[test]
	fn gaussian() {
		let act = Activation::Gaussian;
		relative_eq!(act.base(-1.0), 0.36787945);
		relative_eq!(act.base(-0.5), 0.7788008);
		relative_eq!(act.base(0.0), 1.0);
		relative_eq!(act.base(0.5), 0.7788008);
		relative_eq!(act.base(1.0), 0.36787945);
		relative_eq!(act.derived(-1.0), 0.7357589);
		relative_eq!(act.derived(-0.5), 0.7788008);
		relative_eq!(act.derived(0.0), 0.0);
		relative_eq!(act.derived(0.5), -0.7788008);
		relative_eq!(act.derived(1.0), -0.7357589);
	}
}