-
Notifications
You must be signed in to change notification settings - Fork 65
/
Copy pathactivation.go
120 lines (99 loc) · 3.09 KB
/
activation.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
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
package deep
import "math"
// Mode denotes inference mode
type Mode int
const (
// ModeDefault is unspecified mode
ModeDefault Mode = 0
// ModeMultiClass is for one-hot encoded classification, applies softmax output layer
ModeMultiClass Mode = 1
// ModeRegression is regression, applies linear output layer
ModeRegression Mode = 2
// ModeBinary is binary classification, applies sigmoid output layer
ModeBinary Mode = 3
// ModeMultiLabel is for multilabel classification, applies sigmoid output layer
ModeMultiLabel Mode = 4
)
// OutputActivation returns activation corresponding to prediction mode
func OutputActivation(c Mode) ActivationType {
switch c {
case ModeMultiClass:
return ActivationSoftmax
case ModeRegression:
return ActivationLinear
case ModeBinary, ModeMultiLabel:
return ActivationSigmoid
}
return ActivationNone
}
// GetActivation returns the concrete activation given an ActivationType
func GetActivation(act ActivationType) Differentiable {
switch act {
case ActivationSigmoid:
return Sigmoid{}
case ActivationTanh:
return Tanh{}
case ActivationReLU:
return ReLU{}
case ActivationLinear:
return Linear{}
case ActivationSoftmax:
return Linear{}
}
return Linear{}
}
// ActivationType is represents a neuron activation function
type ActivationType int
const (
// ActivationNone is no activation
ActivationNone ActivationType = 0
// ActivationSigmoid is a sigmoid activation
ActivationSigmoid ActivationType = 1
// ActivationTanh is hyperbolic activation
ActivationTanh ActivationType = 2
// ActivationReLU is rectified linear unit activation
ActivationReLU ActivationType = 3
// ActivationLinear is linear activation
ActivationLinear ActivationType = 4
// ActivationSoftmax is a softmax activation (per layer)
ActivationSoftmax ActivationType = 5
)
// Differentiable is an activation function and its first order derivative,
// where the latter is expressed as a function of the former for efficiency
type Differentiable interface {
F(float64) float64
Df(float64) float64
}
// Sigmoid is a logistic activator in the special case of a = 1
type Sigmoid struct{}
// F is Sigmoid(x)
func (a Sigmoid) F(x float64) float64 { return Logistic(x, 1) }
// Df is Sigmoid'(y), where y = Sigmoid(x)
func (a Sigmoid) Df(y float64) float64 { return y * (1 - y) }
// Logistic is the logistic function
func Logistic(x, a float64) float64 {
return 1 / (1 + math.Exp(-a*x))
}
// Tanh is a hyperbolic activator
type Tanh struct{}
// F is Tanh(x)
func (a Tanh) F(x float64) float64 { return (1 - math.Exp(-2*x)) / (1 + math.Exp(-2*x)) }
// Df is Tanh'(y), where y = Tanh(x)
func (a Tanh) Df(y float64) float64 { return 1 - math.Pow(y, 2) }
// ReLU is a rectified linear unit activator
type ReLU struct{}
// F is ReLU(x)
func (a ReLU) F(x float64) float64 { return math.Max(x, 0) }
// Df is ReLU'(y), where y = ReLU(x)
func (a ReLU) Df(y float64) float64 {
if y > 0 {
return 1
}
return 0
}
// Linear is a linear activator
type Linear struct{}
// F is the identity function
func (a Linear) F(x float64) float64 { return x }
// Df is constant
func (a Linear) Df(x float64) float64 { return 1 }