Package Bio :: Package NeuralNetwork :: Package BackPropagation :: Module Network
[hide private]
[frames] | no frames]

Source Code for Module Bio.NeuralNetwork.BackPropagation.Network

  1  # This code is part of the Biopython distribution and governed by its 
  2  # license.  Please see the LICENSE file that should have been included 
  3  # as part of this package. 
  4  # 
  5   
  6  """Represent Neural Networks. 
  7   
  8  This module contains classes to represent Generic Neural Networks that 
  9  can be trained. 
 10   
 11  Many of the ideas in this and other modules were taken from 
 12  Neil Schemenauer's bpnn.py, available from: 
 13   
 14  http://www.enme.ucalgary.ca/~nascheme/python/bpnn.py 
 15   
 16  My sincerest thanks to him for making this available for me to work from, 
 17  and my apologies for anything I mangled. 
 18  """ 
 19  # standard library 
 20  import math 
 21   
 22   
23 -class BasicNetwork(object):
24 """Represent a Basic Neural Network with three layers. 25 26 This deals with a Neural Network containing three layers: 27 28 - Input Layer 29 - Hidden Layer 30 - Output Layer 31 32 """ 33
34 - def __init__(self, input_layer, hidden_layer, output_layer):
35 """Initialize the network with the three layers.""" 36 self._input = input_layer 37 self._hidden = hidden_layer 38 self._output = output_layer
39
40 - def train(self, training_examples, validation_examples, 41 stopping_criteria, learning_rate, momentum):
42 """Train the neural network to recognize particular examples. 43 44 Arguments: 45 - training_examples -- A list of TrainingExample classes that will 46 be used to train the network. 47 - validation_examples -- A list of TrainingExample classes that 48 are used to validate the network as it is trained. These examples 49 are not used to train so the provide an independent method of 50 checking how the training is doing. Normally, when the error 51 from these examples starts to rise, then it's time to stop 52 training. 53 - stopping_criteria -- A function, that when passed the number of 54 iterations, the training error, and the validation error, will 55 determine when to stop learning. 56 - learning_rate -- The learning rate of the neural network. 57 - momentum -- The momentum of the NN, which describes how much 58 of the prevoious weight change to use. 59 60 """ 61 num_iterations = 0 62 while True: 63 num_iterations += 1 64 training_error = 0.0 65 for example in training_examples: 66 # update the predicted values for all of the nodes 67 # based on the current weights and the inputs 68 # This propagates over the entire network from the input. 69 self._input.update(example.inputs) 70 71 # calculate the error via back propagation 72 self._input.backpropagate(example.outputs, 73 learning_rate, momentum) 74 75 # get the errors in our predictions 76 for node in range(len(example.outputs)): 77 training_error += \ 78 self._output.get_error(example.outputs[node], 79 node + 1) 80 81 # get the current testing error for the validation examples 82 validation_error = 0.0 83 for example in validation_examples: 84 predictions = self.predict(example.inputs) 85 86 for prediction_num in range(len(predictions)): 87 real_value = example.outputs[prediction_num] 88 predicted_value = predictions[prediction_num] 89 validation_error += \ 90 0.5 * math.pow((real_value - predicted_value), 2) 91 92 # see if we have gone far enough to stop 93 if stopping_criteria(num_iterations, training_error, 94 validation_error): 95 break
96
97 - def predict(self, inputs):
98 """Predict outputs from the neural network with the given inputs. 99 100 This uses the current neural network to predict outputs, no 101 training of the neural network is done here. 102 """ 103 # update the predicted values for these inputs 104 self._input.update(inputs) 105 106 outputs = [] 107 for output_key in sorted(self._output.values): 108 outputs.append(self._output.values[output_key]) 109 return outputs
110