This repository has been archived by the owner on Dec 11, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
activation_func.go
158 lines (142 loc) · 3.75 KB
/
activation_func.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
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
// activation_func.go implementation of activation functions used in a network.
//
// Copyright (C) 2017 Jin Yeom
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package neat
import (
"math"
)
var (
// ActivationSet is a set of functions that can be used as activation
// functions by neurons.
ActivationSet = map[string]*ActivationFunc{
"linear": Linear(),
"sigmoid": Sigmoid(),
"tanh": Tanh(),
"sin": Sin(),
"cos": Cos(),
"relu": ReLU(),
"log": Log(),
"exp": Exp(),
"abs": Abs(),
"square": Square(),
"cube": Cube(),
"gaussian": Gaussian(0.0, 1.0),
}
)
// ActivationFunc is a wrapper type for activation functions.
type ActivationFunc struct {
Name string `json:"name"` // name of the function
Fn func(x float64) float64 `json:"-"` // activation function
}
// Identity returns the identity function as an activation
// function. This function is only used for sensor nodes.
func Identity() *ActivationFunc {
return &ActivationFunc{
Name: "Identity",
Fn: func(x float64) float64 {
return x
},
}
}
// Sigmoid returns the sigmoid function as an activation function.
func Sigmoid() *ActivationFunc {
return &ActivationFunc{
Name: "Sigmoid",
Fn: func(x float64) float64 {
return 1.0 / (1.0 + math.Exp(-x))
},
}
}
// Tanh returns the hyperbolic tangent function as an activation function.
func Tanh() *ActivationFunc {
return &ActivationFunc{
Name: "Tanh",
Fn: math.Tanh,
}
}
// Sin returns the sin function as an activation function.
func Sin() *ActivationFunc {
return &ActivationFunc{
Name: "Sine",
Fn: math.Sin,
}
}
// Cos returns the cosine function as an activation function.
func Cos() *ActivationFunc {
return &ActivationFunc{
Name: "Cosine",
Fn: math.Cos,
}
}
// ReLU returns a rectifier linear unit as an activation function.
func ReLU() *ActivationFunc {
return &ActivationFunc{
Name: "ReLU",
Fn: func(x float64) float64 {
return math.Max(x, 0.0)
},
}
}
// Log returns the log function as an activation function.
func Log() *ActivationFunc {
return &ActivationFunc{
Name: "Log",
Fn: math.Log,
}
}
// Exp returns the exponential function as an activation function.
func Exp() *ActivationFunc {
return &ActivationFunc{
Name: "Exp",
Fn: math.Exp,
}
}
// Abs returns the absolute value function as an activation function.
func Abs() *ActivationFunc {
return &ActivationFunc{
Name: "Abs",
Fn: math.Abs,
}
}
// Square returns the square function as an activation function.
func Square() *ActivationFunc {
return &ActivationFunc{
Name: "Square",
Fn: func(x float64) float64 {
return x * x
},
}
}
// Cube returns the cube function as an activation function.
func Cube() *ActivationFunc {
return &ActivationFunc{
Name: "Cube",
Fn: func(x float64) float64 {
return x * x * x
},
}
}
// Gaussian returns the Gaussian function as an activation function, given a
// mean and a standard deviation.
func Gaussian(mean, stdev float64) *ActivationFunc {
return &ActivationFunc{
Name: "Gaussian",
Fn: func(x float64) float64 {
return 1.0 / (stdev * math.Sqrt(2*math.Pi)) *
math.Exp(math.Pow((x-mean)/stdev, 2.0)/-2.0)
},
}
}