Advertisement
Guest User

Untitled

a guest
Nov 27th, 2014
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.72 KB | None | 0 0
  1. from neuron import SigmoidNeuron
  2.  
  3.  
  4. class Network:
  5.     def __init__(self, layers, learningRate = 0.2):
  6.         self.layers = [[]] * layers
  7.  
  8.         # trainingInput will actually contain a tuple
  9.         # with both the input vector and the output
  10.         # vector
  11.         self.trainingInputs = []
  12.         self.learningRate = learningRate
  13.  
  14.     def addToLayer(self, layer, neuron):
  15.         self.layers[layer].append(neuron)
  16.  
  17.     def addTrainingVector(self, tinput, toutput):
  18.         self.trainingInputs.append((tinput, toutput))
  19.  
  20.  
  21.     def getNetworkOutput(self, inputVector):
  22.         # the input neurons are a special case,
  23.         # no processing is done.
  24.         assert len(inputVector) == len(self.layers[0])
  25.         layerinput = inputVector
  26.         layerOutput = []
  27.        
  28.         # Go through every layer from the first
  29.         for i in range(1, len(self.layers)):
  30.             # Go through every neuron in the layer
  31.             for neuron in self.layers[i]:
  32.                 assert neuron.getInputCount() == len(layerInput)
  33.                 output = neuron.update(layerInput)
  34.                 layerOutput.append(output)
  35.             # And then feed the output forward
  36.             layerInput = layerOutput
  37.        
  38.         # layerOutput now contains the final network output
  39.         return layerOutput
  40.  
  41.     def getError(self, networkOutput, expectedOutput):
  42.         # quadratic error function, handles the difference
  43.         # in input to output better than a linear differencing
  44.         return 0.5 * (expectedOutput - networkOutput) ** 2
  45.  
  46.     def trainNetwork(self):
  47.         # Go through every training set
  48.         for inputVector, outputVector in self.trainingInputs:
  49.  
  50.             networkOutput = getNetworkOutput(inputVector)
  51.  
  52.             # Go through the layers in reverse
  53.             for layer in reversed(range(1, len(self.layers))):
  54.                 # if it's the output layer
  55.                 if layer == len(self.layers) - 1:
  56.                     for idx, neuron in enumerate(self.layers[layer]):
  57.                         o = networkOutput[idx]
  58.                         delta = o * * (1.0 - output) * (outputVector[idx] - o)
  59.  
  60.                 else:
  61.                     # go through every neuron
  62.                     for idx, neuron in enumerate(self.layers[layer]):
  63.                         # update weights
  64.                         weights = neuron.getWeights()
  65.  
  66.                         delta = neuron.thisDerivative()
  67.  
  68.  
  69.     def getOutputLayerErrors(self, network, expected):
  70.         assert len(network) == len(expected)
  71.         errors = []
  72.         for idx in xrange(len(self.layer[len(self.layers) - 1])):
  73.             errors.append(getError(network[idx], expected[idx]))
  74.         return errors
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement