-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathactivate.py
More file actions
37 lines (31 loc) · 947 Bytes
/
Copy pathactivate.py
File metadata and controls
37 lines (31 loc) · 947 Bytes
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
from module import Layers
import numpy as np
class Relu(Layers):
def __init__(self,name):
super(Relu,self).__init__(name)
def forward(self,input):
self.input = input
return np.maximum(input, 0)
def backward(self,grad_out):
grad_out[self.input<0]=0
return grad_out
class Sigmoid(Layers):
def __init__(self, name):
super(Sigmoid,self).__init__(name)
def forward(self,input):
self.output = 1/(1+np.exp(-input))
return self.output
def backward(self,grad):
grad = grad * self.output*(1-self.output)
return grad
class Tanh(Layers):
def __init__(self, name):
super(Tanh,self).__init__(name)
def forward(self,input):
a = np.exp(input)
b = np.exp(-input)
self.output = (a-b)/(a+b)
return self.output
def backward(self,grad):
grad = grad * (1-self.output*self.output)
return grad