Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from neuron import SigmoidNeuron
- class Network:
- def __init__(self, layers, learningRate = 0.2):
- self.layers = [[]] * layers
- # trainingInput will actually contain a tuple
- # with both the input vector and the output
- # vector
- self.trainingInputs = []
- self.learningRate = learningRate
- def addToLayer(self, layer, neuron):
- self.layers[layer].append(neuron)
- def addTrainingVector(self, tinput, toutput):
- self.trainingInputs.append((tinput, toutput))
- def getNetworkOutput(self, inputVector):
- # the input neurons are a special case,
- # no processing is done.
- assert len(inputVector) == len(self.layers[0])
- layerinput = inputVector
- layerOutput = []
- # Go through every layer from the first
- for i in range(1, len(self.layers)):
- # Go through every neuron in the layer
- for neuron in self.layers[i]:
- assert neuron.getInputCount() == len(layerInput)
- output = neuron.update(layerInput)
- layerOutput.append(output)
- # And then feed the output forward
- layerInput = layerOutput
- # layerOutput now contains the final network output
- return layerOutput
- def getError(self, networkOutput, expectedOutput):
- # quadratic error function, handles the difference
- # in input to output better than a linear differencing
- return 0.5 * (expectedOutput - networkOutput) ** 2
- def trainNetwork(self):
- # Go through every training set
- for inputVector, outputVector in self.trainingInputs:
- networkOutput = getNetworkOutput(inputVector)
- # Go through the layers in reverse
- for layer in reversed(range(1, len(self.layers))):
- # if it's the output layer
- if layer == len(self.layers) - 1:
- for idx, neuron in enumerate(self.layers[layer]):
- o = networkOutput[idx]
- delta = o * * (1.0 - output) * (outputVector[idx] - o)
- else:
- # go through every neuron
- for idx, neuron in enumerate(self.layers[layer]):
- # update weights
- weights = neuron.getWeights()
- delta = neuron.thisDerivative()
- def getOutputLayerErrors(self, network, expected):
- assert len(network) == len(expected)
- errors = []
- for idx in xrange(len(self.layer[len(self.layers) - 1])):
- errors.append(getError(network[idx], expected[idx]))
- return errors
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement