-
Notifications
You must be signed in to change notification settings - Fork 2
/
squash.go
73 lines (63 loc) · 1.9 KB
/
squash.go
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
package automata
import "math"
// Squasher implements a squashing function which can be used as an activation function.
// Squashing functions modify inputs allowing neurons to model non-linear relationships.
// This typically involves clamping/bounding the output at one or both ends.
//
// These functions can be very sensitive to the weights which are applied to them which
// can make training difficult because you need to discover the precise weightings which will
// squash the input in the way you want. Some functions can exacerbate the Vanishing Gradient
// problem.
//
// Common squash functions are already implemented.
type Squasher interface {
// Squash an input x into a suitable output. If 'derivate' is true, the derivative should
// be returned.
Squash(x float64, derivate bool) float64
}
// SquashLogistic implements the logistic function.
type SquashLogistic struct{}
// Squash x.
func (s *SquashLogistic) Squash(x float64, derivate bool) float64 {
fx := 1.0 / (1.0 + math.Exp(-x))
if !derivate {
return fx
}
return fx * (1 - fx)
}
// SquashTanh implements tanh function.
type SquashTanh struct{}
// Squash x.
func (s *SquashTanh) Squash(x float64, derivate bool) float64 {
if derivate {
return 1 - math.Pow(math.Tanh(x), 2)
}
return math.Tanh(x)
}
// SquashIdentity implements the identity function.
type SquashIdentity struct{}
// Squash x.
func (s *SquashIdentity) Squash(x float64, derivate bool) float64 {
if derivate {
return 1
}
return x
}
// SquashRelu implements the ReLU activation function, which is not subject to the Vanishing
// Gradient problem
// .
// https://en.wikipedia.org/wiki/Rectifier_(neural_networks)
type SquashRelu struct{}
// Squash x.
func (s *SquashRelu) Squash(x float64, derivate bool) float64 {
if derivate {
if x > 0 {
return 1
}
return 0
}
if x > 0 {
return x
}
return 0
}