/
activation.py
176 lines (124 loc) · 3.81 KB
/
activation.py
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
"""
Activation function module.
"""
__docformat__ = "restructuredtext en"
## Copyright (c) 2009 Emmanuel Goossaert
##
## This file is part of npy.
##
## npy 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.
##
## npy 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 npy. If not, see <http://www.gnu.org/licenses/>.
import math
from error import ErrorOutputDifference
from error import ErrorLinear
from label import LabelMax
from factory import FactoryMixin
from factory import Factory
from exception import *
class Activation(FactoryMixin):
"""
Activation function class.
"""
prefix = 'ac_'
def __init__(self):
"""
Initializer.
"""
FactoryMixin.__init__(self)
def compute_activation(self, inputs, weights):
"""
Compute the value of the activation function
for the given sequences of inputs and weights.
**This method MUST NOT be overridden by subclasses.**
:Parameters:
inputs : sequence of floats
Input data to be treated by the activation function.
weights : sequence of floats
Weights to be used by the activation function.
:Returns:
Value of the activation function.
"""
value = 0
for input, weight in zip(inputs, weights):
value = value + input * weight
return self.activation_function(value)
def activation_function(self, x):
"""
Activation function.
:Parameters:
x : float
input value
:Returns:
float : the value of the activation function for the given x value.
"""
pass
def activation_derivative(self, x):
"""
Derivative of the activation function.
:Parameters:
x : float
input value
:Returns:
float : the value of the activation derivative for the given
x value.
"""
pass
class ActivationLinear(Activation):
"""
Linear activation function
"""
def __init__(self):
Activation.__init__(self)
self._set_name("ac_linear")
def activation_function(self, x):
return x
def activation_derivative(self, x):
return 1
@staticmethod
def build_instance():
return ActivationLinear()
class ActivationPerceptron(Activation):
"""
Perceptron activation function
"""
def __init__(self):
Activation.__init__(self)
self._set_name("ac_perceptron")
def activation_function(self, x):
if x > 0:
return 1
else:
return -1
def activation_derivative(self, x):
return 1
@staticmethod
def build_instance():
return ActivationPerceptron()
class ActivationSigmoid(Activation):
"""
Sigmoid activation function
"""
def __init__(self):
Activation.__init__(self)
self._set_name("ac_sigmoid")
def activation_function(self, x):
return 1 / (1 + math.exp(-x))
def activation_derivative(self, x):
return x * (1 - x)
@staticmethod
def build_instance():
return ActivationSigmoid()
# Declare the activation functions to the Factory
Factory.declare_instance(ActivationLinear())
Factory.declare_instance(ActivationPerceptron())
Factory.declare_instance(ActivationSigmoid())