-
Notifications
You must be signed in to change notification settings - Fork 0
/
Python Feed Forward
85 lines (70 loc) · 3.26 KB
/
Python Feed Forward
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
from random import seed
from random import random
import numpy as np
from math import exp
# initialize WeightIH and DeltaWeightIH
def InitialiseIH(smallwt, NumHidden, NumInput):
DeltaWeightIH = np.zeros([(NumInput + 1), (NumHidden + 1)])
WeightIH = np.zeros([(NumInput + 1), (NumHidden + 1)])
for j in range(NumHidden + 1):
for i in range(NumInput + 1):
WeightIH[i][j] = 2.0 * (random() - 0.5) * smallwt
while WeightIH[i][j] == 0.:
WeightIH[i][j] = 2.0 * (random() - 0.5) * smallwt
print(DeltaWeightIH)
print(WeightIH)
def InitialiseHO(smallwt, NumOutput, NumHidden):
DeltaWeightHO = np.zeros([(NumHidden + 1), (NumOutput + 1)])
WeightHO = np.zeros([(NumHidden + 1), (NumOutput + 1)])
for k in range(NumOutput + 1):
for j in range(NumHidden + 1):
WeightHO[j][k] = 2.0 * (random() - 0.5) * smallwt
while WeightHO[j][k] == 0.:
WeightHO[j][k] = 2.0 * (random() - 0.5) * smallwt
print(DeltaWeightHO)
print(WeightHO)
Weights1 = InitialiseIH(0.05, 2, 1)
Weights2 = InitialiseHO(0.05, 2, 1)
def shufftle(Input, NumOutput, NumHidden, NumInput, WeightIH, WeightHO, eta, alpha):
n = int(len(Input) / 2)
np.random.shuffle(Input)
Train = Input[:n]
Test = Input[n:]
NumPattern = len(Input)
Error = 0.0
for p in range(NumPattern + 1):
Hidden = np.zeros([(NumPattern + 1), (NumHidden + 1)])
Output = np.zeros([(NumPattern + 1), (NumOutput + 1)])
Target = np.zeros([(NumPattern + 1), (NumOutput + 1)])
SumH = np.zeros([(NumPattern + 1), (NumHidden + 1)])
SumO = np.zeros([(NumPattern + 1), (NumOutput + 1)])
DeltaO = np.zeros([(NumOutput + 1)])
SumDOW = np.zeros([(NumHidden + 1)])
DeltaH = np.zeros([(NumHidden + 1)])
DeltaWeightIH = np.zeros([(NumInput + 1), (NumHidden + 1)])
DeltaWeightHO = Output = np.zeros([(NumHidden + 1), (NumOutput + 1)])
for j in range(NumHidden + 1):
for i in range (NumInput + 1):
SumH[i][j] = Input[p][i] * WeightIH[i][j]
Hidden[p][j] = 1.0/(1.0 + exp(-SumH[p][j]))
#compute output unit activations and errors
for k in range(NumOutput + 1):
SumO[p][k] = WeightHO[1][k]
for j in range(NumHidden + 1):
SumO[p][k] += Hidden[p][j] * WeightHO[j][k]
Output[p][k] = 1.0 / (1.0 + exp(-SumO[p][k]))
Error += 0.5 * (Target[p][k] - Output[p][k]) * (Target[p][k] - Output[p][k])
DeltaO[k] = (Target[p][k] - Output[p][k]) * Output[p][k] * (1.0 - Output[p][k])
#'back-propagate' errors to hidden layer
for j in range(NumHidden + 1):
SumDOW[j] = 0.0
for k in range(NumOutput + 1):
SumDOW[j] += WeightHO[j][k] * DeltaO[k]
DeltaH[j] = SumDOW[j] * Hidden[p][j] * (1.0 - Hidden[p][j])
#update weights WeightIH
for j in range(NumHidden + 1):
DeltaWeightIH[0][j] = eta * DeltaH[j] + alpha * DeltaWeightIH[0][j]
WeightIH[0][j] += DeltaWeightIH[0][j]
for i in range(NumInput + 1):
DeltaWeightIH[i][j] = eta * Input[p][i] * DeltaH[j] + alpha * DeltaWeightIH[i][j];
WeightIH[i][j] += DeltaWeightIH[i][j]