-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathForward_BP.py
151 lines (130 loc) · 6.05 KB
/
Forward_BP.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
# -*- coding: utf-8 -*-
"""
Created on Sat Apr 15 12:32:21 2023
@author: premchand
"""
import pandas as pd
import random
import math
import numpy as np
import sys
import logging
LOG_FILENAME = 'example.log'
logging.basicConfig(filename=LOG_FILENAME,level=logging.DEBUG)
# network initialization
def networkInitialization(numberOfInputs, numberOfOutputs, numberOfHiddenLayers, numberOfNeuronsPerLayer):
weightList = list()
for index in range(numberOfHiddenLayers + 1):
if index == 0:
hiddenLayer = [[random.random() for i in range(numberOfInputs + 1)] for i in
range(numberOfNeuronsPerLayer[index])]
elif index == numberOfHiddenLayers:
hiddenLayer = [[random.random() for i in range(numberOfNeuronsPerLayer[index - 1] + 1)] for i in
range(numberOfOutputs)]
else:
hiddenLayer = [[random.random() for i in range(numberOfNeuronsPerLayer[index - 1] + 1)] for i in
range(numberOfNeuronsPerLayer[index])]
weightList.append(hiddenLayer)
return weightList
# activation
def activate(weights, inputs):
activation = weights[-1]
for i in range(len(weights) - 1):
activation += weights[i] * inputs[i]
return transfer(activation)
# Sigmoid Function
def transfer(activation):
return 1.0 / (1.0 + math.exp(-activation))
# Forward Propagation
def forwardPropagate(weightList, dataRow):
inputs = dataRow[:-1]
eachNeuronOutputs = list()
eachNeuronOutputs.append(dataRow[:-1])
for eachLayerIndex in range(len(weightList)):
newInputs = []
for eachNeuronIndex in range(len(weightList[eachLayerIndex])):
activation = activate(weightList[eachLayerIndex][eachNeuronIndex], inputs)
newInputs.append(activation)
inputs = newInputs
eachNeuronOutputs.append(newInputs)
return eachNeuronOutputs
# BackWard Propagate
def backwardPropagate(outputs, expectedOutput, weightList, learningRate):
# expectedOutput = [0,1]
deltaList = []
for indexOutputLayer in reversed(range(len(outputs))):
newDelta = list()
if indexOutputLayer == len(outputs) - 1:
for index in range(len(outputs[indexOutputLayer])):
delta = outputs[indexOutputLayer][index] * (1 - outputs[indexOutputLayer][index]) * (
expectedOutput[index] - outputs[indexOutputLayer][index])
newDelta.append(delta)
deltaList = newDelta
elif indexOutputLayer == 0:
for index in range(len(outputs[indexOutputLayer])):
for indexWeight in range(len(weightList[indexOutputLayer])):
weightList[indexOutputLayer][indexWeight][index] += learningRate * deltaList[indexWeight] * \
outputs[indexOutputLayer][index]
for indexWeight in range(len(weightList[indexOutputLayer])):
weightList[indexOutputLayer][indexWeight][-1] += learningRate * deltaList[indexWeight]
else:
for index in range(len(outputs[indexOutputLayer])):
sum = 0
for indexWeight in range(len(weightList[indexOutputLayer])):
sum += weightList[indexOutputLayer][indexWeight][index] * deltaList[indexWeight]
weightList[indexOutputLayer][indexWeight][index] += learningRate * deltaList[indexWeight] * \
outputs[indexOutputLayer][index]
delta = outputs[indexOutputLayer][index] * (1 - outputs[indexOutputLayer][index]) * sum
newDelta.append(delta)
for indexWeight in range(len(weightList[indexOutputLayer])):
weightList[indexOutputLayer][indexWeight][-1] += learningRate * deltaList[indexWeight]
deltaList = newDelta
def train_network(weightList, traininigDataSet, learningRate, noOfIteration, numberOfOutputs):
for iter in range(noOfIteration):
sum_error = 0
for row in traininigDataSet:
outputs = forwardPropagate(weightList, row)
expected = [0 for i in range(numberOfOutputs)]
expected[int(row[-1]) - 1] = 1
#actuals = maxOutput(outputs[len(outputs) - 1])
# sum_error += (sum([(expected[i] - actuals[i]) ** 2 for i in range(len(expected))]) / 2)
sum_error += sum([(expected[i] - outputs[len(outputs) - 1][i]) ** 2 for i in range(len(expected))])
backwardPropagate(outputs,expected,weightList,learningRate)
sum_error = sum_error/len(traininigDataSet)
print('Iteration=%d, Error=%.8f' % (iter+1, sum_error))
precisedError = '%.8f' % sum_error
if float(precisedError) == 0.0:
break
def printWeights(weights):
for layer in range(len(weights)):
print("Layer " + str(layer) + ":")
for col in range(len(weights[layer][0])):
neuronWeights = []
for row in range(len(weights[layer])):
neuronWeights.append(weights[layer][row][col])
if(col == len(weights[layer][0])-1):
print("\t Bias Term :" + str(neuronWeights))
else:
print("\t Neuron " + str(col+1)+ " : " + str(neuronWeights))
def maxOutput(Lastoutputs):
actuals = [0 for i in range(len(Lastoutputs))]
index = Lastoutputs.index(max(Lastoutputs))
actuals[index] = 1;
return actuals
def testTheModel(dataSet, weightList,numberOfOutputs):
sumError = 0
count = 0
for data in dataSet:
outputs = forwardPropagate(weightList,data)
expected = [0 for i in range(numberOfOutputs)]
expected[int(data[-1]) - 1] = 1
sumError += sum([(expected[i] - outputs[len(outputs) - 1][i]) ** 2 for i in range(len(expected))])
actuals = maxOutput(outputs[len(outputs) - 1])
if actuals == expected:
count = count + 1
sumError = sumError / len(dataSet)
accuracy = count/len(dataSet)
outs = []
outs.append(sumError)
outs.append(accuracy)
return outs