-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnet.py
53 lines (45 loc) · 2.01 KB
/
net.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
import layer
# the depth of a NN is defined as sum of hidden layers and the output layer
# e.g. if the depth is 3, the net has an input layer, 2 hidden layers and one output layer
# so the definitive layers count is 4
class Net:
def __init__(self):
self.inputLayer = None
self.hiddenLayers = []
self.outputLayer = None
self.learningRate = 0.0006
self.learningRateBias = 0.0006
def addHiddenLayer(self, layer):
self.hiddenLayers.append(layer)
if len(self.hiddenLayers) == 1:
self.inputLayer.connectNextLayer(self.hiddenLayers[0])
self.hiddenLayers[0].connectPreviousLayer(self.inputLayer)
else:
self.hiddenLayers[len(self.hiddenLayers) - 2].connectNextLayer(self.hiddenLayers[len(self.hiddenLayers) - 1])
self.hiddenLayers[len(self.hiddenLayers) - 1].connectPreviousLayer(self.hiddenLayers[len(self.hiddenLayers) - 2])
def setInputLayer(self, inputLayer):
self.inputLayer = inputLayer
def setOutputLayer(self, outputLayer):
self.outputLayer = outputLayer
self.hiddenLayers[len(self.hiddenLayers) - 1].connectNextLayer(self.outputLayer)
self.outputLayer.connectPreviousLayer(self.hiddenLayers[len(self.hiddenLayers) - 1])
def __str__(self):
hiddenLayersString = ""
hiddenLayerCount = 0
for hiddenLayer in self.hiddenLayers:
hiddenLayerCount += 1
hiddenLayersString += "Hidden Layer " + str(hiddenLayerCount) + ":\n" + str(hiddenLayer) + "\n"
return "Input Layer:\n" + str(self.inputLayer) + "\n" + hiddenLayersString + "Output Layer:\n" + str(self.outputLayer)
def getDefaultInputVector(self):
return [0 for _ in range(0, len(self.inputLayer.weights))]
def perform(self):
for hiddenLayer in self.hiddenLayers:
hiddenLayer.transfer()
self.outputLayer.transfer()
def backpropagate(self, targetOutputVector):
self.outputLayer.backpropagate(targetOutputVector)
for hiddenLayer in reversed(self.hiddenLayers):
hiddenLayer.backpropagate(targetOutputVector)
self.outputLayer.updateWeights()
for hiddenLayer in reversed(self.hiddenLayers):
hiddenLayer.updateWeights()