-
Notifications
You must be signed in to change notification settings - Fork 0
/
activationFunction.py
159 lines (136 loc) · 7.44 KB
/
activationFunction.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
import numpy as np
class activationFunction(object):
def __init__(self, technology='numpy'):
self.technology = technology
class linear(activationFunction):
def getActivation(self):
def getActivationTF(prevLayerActivation, weights, biases):
try:
import tensorflow as tf
return tf.matmul(prevLayerActivation, tf.transpose(weights)) + biases
except ImportError:
raise ImportError(
'TensorFlow is not installed on your computer. Please use other technology for building your network or install tensorflow.')
if self.technology == 'tensorflow': return getActivationTF
else:
raise ImportError('Technology '+self.technology+' currently is not supported. Please check spelling or switch to another technology.')
def getGradient(self):
return None
class convolve2d(activationFunction):
def getActivation(self, strides, padding='SAME'):
def getActivationTF(prevLayerActivation, weights, biases, strides=strides, padding=padding, gpu=True):
try:
import tensorflow as tf
return tf.nn.conv2d(prevLayerActivation, weights, strides=strides, padding=padding, use_cudnn_on_gpu=gpu) + biases
except ImportError:
raise ImportError(
'TensorFlow is not installed on your computer. Please use other technology for building your network or install tensorflow.')
if self.technology == 'tensorflow': return getActivationTF
else:
raise ImportError('Technology '+self.technology+' currently is not supported. Please check spelling or switch to another technology.')
def getGradient(self):
return None
class logistic(activationFunction):
def getActivation(self):
def getActivationNumpy(z):
return np.true_divide(1., (1 + np.exp(-z)))
def getActivationTF(z):
try:
import tensorflow as tf
return tf.nn.sigmoid(z)
except ImportError:
raise ImportError(
'TensorFlow is not installed on your computer. Please use other technology for building your network or install tensorflow.')
if self.technology=='numpy': return getActivationNumpy
if self.technology == 'tensorflow': return getActivationTF
else:
raise ImportError('Technology '+self.technology+' currently is not supported. Please check spelling or switch to another technology.')
def getGradient(self):
def getGradientNumpy(dEda, activation):
return np.array(activation * (1 - activation) * dEda)
if self.technology == 'numpy': return getGradientNumpy
if self.technology == 'tensorflow': return None
else:
raise ImportError('There is no gradient function for '+self.technology+'. You can add it by yourself to activationFunction.py or change another technology.')
class softmax(activationFunction):
def getActivation(self):
def getActivationNumpy(z):
exp_z = np.exp(z)
sum_exp_z = np.sum(exp_z, axis=1)
return np.true_divide(exp_z, np.array([sum_exp_z]).T)
def getActivationTF(z):
try:
import tensorflow as tf
return tf.nn.softmax(z)
except ImportError:
raise ImportError(
'TensorFlow is not installed on your computer. Please use other technology for building your network or install tensorflow.')
if self.technology=='numpy': return getActivationNumpy
if self.technology == 'tensorflow': return getActivationTF
else:
raise ImportError('Technology '+self.technology+' currently is not supported. Please check spelling or switch to another technology.')
def getGradient(self):
def getGradientNumpy(dEda, activation):
return np.array(activation * (1 - activation) * dEda)
if self.technology == 'numpy': return getGradientNumpy
if self.technology == 'tensorflow': return None
else:
raise ImportError('There is no gradient function for '+self.technology+'. You can add it by yourself to activationFunction.py or change another technology.')
class relu(activationFunction):
def getActivation(self):
def getActivationNumpy(z):
return (z>0)*z + (z<0)*0
def getActivationTF(z):
try:
import tensorflow as tf
return tf.nn.relu(z)
except ImportError:
raise ImportError(
'TensorFlow is not installed on your computer. Please use other technology for building your network or install tensorflow.')
if self.technology=='numpy': return getActivationNumpy
if self.technology == 'tensorflow': return getActivationTF
else:
raise ImportError('Technology '+self.technology+' currently is not supported. Please check spelling or switch to another technology.')
def getGradient(self):
def getGradientNumpy(dEda, activation):
return dEda*(activation>0)+0*(activation<0)
if self.technology == 'numpy': return getGradientNumpy
if self.technology == 'tensorflow': return None
else:
raise ImportError('There is no gradient function for '+self.technology+'. You can add it by yourself to activationFunction.py or change another technology.')
class lrelu(activationFunction):
def getActivation(self):
def getActivationNumpy(z):
return (z>0)*z + (z<0)*0.01*z
def getActivationTF(z):
try:
import tensorflow as tf
return tf.add(tf.mul(z>0,z), tf.mul(z<0,0.01*z))
except ImportError:
raise ImportError(
'TensorFlow is not installed on your computer. Please use other technology for building your network or install tensorflow.')
if self.technology=='numpy': return getActivationNumpy
if self.technology == 'tensorflow': return getActivationTF
else:
raise ImportError('Technology '+self.technology+' currently is not supported. Please check spelling or switch to another technology.')
def getGradient(self):
def getGradientNumpy(dEda, activation):
return dEda*(activation>0)+0.01*dEda*(activation<0)
if self.technology == 'numpy': return getGradientNumpy
if self.technology == 'tensorflow': return None
else:
raise ImportError('There is no gradient function for '+self.technology+'. You can add it by yourself to activationFunction.py or change another technology.')
class elu(activationFunction):
def getActivation(self):
def getActivationTF(z):
try:
import tensorflow as tf
return tf.nn.elu(z)
except ImportError:
raise ImportError(
'TensorFlow is not installed on your computer. Please use other technology for building your network or install tensorflow.')
if self.technology == 'tensorflow': return getActivationTF
else:
raise ImportError('Technology '+self.technology+' currently is not supported. Please check spelling or switch to another technology.')
def getGradient(self):
return None