-
Notifications
You must be signed in to change notification settings - Fork 0
/
mlpModule.py
58 lines (42 loc) · 2.27 KB
/
mlpModule.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
__author__ = 'lope'
import numpy as np
import math
class MLP:
def sigmoid(self, x):
return float(1 / (1 + math.exp(-x)))
def __init__(self,nInput,nHidden,nOutput):
self.WList = [np.random.rand(nHidden, nInput+1), np.random.rand(nOutput, nHidden+1)]
self.nInput = nInput;
self.nHidden = nHidden;
self.nOutput = nOutput;
self.sigmoid = np.vectorize(self.sigmoid)
def compute2Arg(self, v1,v2):
return self.compute([v1,v2])[0]
def compute(self, inputValues):
inputValues = np.append([1], inputValues)
self.lastInput = inputValues;
self.lastHiddenOutput = np.dot(self.WList[0], np.transpose(inputValues))
self.lastHiddenOutput = self.sigmoid(self.lastHiddenOutput)
self.lastHiddenOutput = np.append([1], self.lastHiddenOutput)
self.lastOutput = np.dot(self.WList[1], self.lastHiddenOutput)
self.lastOutput = self.sigmoid(self.lastOutput)
return self.lastOutput
class Backpropagation:
def __init__(self, mlp, alpha, regularization):
self.DeltaList = [np.zeros((mlp.nHidden, mlp.nInput+1)), np.zeros((mlp.nOutput, mlp.nHidden+1))]
self.alpha = alpha;
self.regularization = regularization;
self.mlp = mlp
def iterate(self, inputValues, outputValues):
for i in range(len(inputValues)):
self.mlp.compute(inputValues[i])
delta3 = np.subtract( self.mlp.lastOutput, outputValues[i]);
Od3 = np.dot((self.mlp.WList[1].transpose()),delta3)
gz2 = np.multiply(self.mlp.lastHiddenOutput, (1-self.mlp.lastHiddenOutput))
delta2 = np.multiply(Od3, gz2)
self.DeltaList[1] = np.add(self.DeltaList[1], np.outer(delta3, self.mlp.lastHiddenOutput))
self.DeltaList[0] = np.add(self.DeltaList[0], np.delete(np.outer(delta2, self.mlp.lastInput),0,0))
self.DeltaList[0] = self.DeltaList[0]/len(inputValues) + self.regularization*self.mlp.WList[0]
self.DeltaList[1] = self.DeltaList[1]/len(inputValues) + self.regularization*self.mlp.WList[1]
self.mlp.WList[0] = self.mlp.WList[0]-self.alpha*self.DeltaList[0]
self.mlp.WList[1] = self.mlp.WList[1]-self.alpha*self.DeltaList[1]