forked from TheIndependentCode/Neural-Network
-
Notifications
You must be signed in to change notification settings - Fork 0
/
activations.py
38 lines (30 loc) · 1.07 KB
/
activations.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
import numpy as np
from layer import Layer
from activation import Activation
class Tanh(Activation):
def __init__(self):
def tanh(x):
return np.tanh(x)
def tanh_prime(x):
return 1 - np.tanh(x) ** 2
super().__init__(tanh, tanh_prime)
class Sigmoid(Activation):
def __init__(self):
def sigmoid(x):
return 1 / (1 + np.exp(-x))
def sigmoid_prime(x):
s = sigmoid(x)
return s * (1 - s)
super().__init__(sigmoid, sigmoid_prime)
class Softmax(Layer):
def forward(self, input):
tmp = np.exp(input)
self.output = tmp / np.sum(tmp)
return self.output
def backward(self, output_gradient, learning_rate):
# This version is faster than the one presented in the video
n = np.size(self.output)
return np.dot((np.identity(n) - self.output.T) * self.output, output_gradient)
# Original formula:
# tmp = np.tile(self.output, n)
# return np.dot(tmp * (np.identity(n) - np.transpose(tmp)), output_gradient)