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 o Input Layer 29 30 o Hidden Layer 31 32 o Output Layer 33 """
34 - def __init__(self, input_layer, hidden_layer, output_layer):
35 """Initialize the network with the three layers. 36 """ 37 self._input = input_layer 38 self._hidden = hidden_layer 39 self._output = output_layer
40
41 - def train(self, training_examples, validation_examples, 42 stopping_criteria, learning_rate, momentum):
43 """Train the neural network to recognize particular examples. 44 45 Arguments: 46 47 o training_examples -- A list of TrainingExample classes that will 48 be used to train the network. 49 50 o validation_examples -- A list of TrainingExample classes that 51 are used to validate the network as it is trained. These examples 52 are not used to train so the provide an independent method of 53 checking how the training is doing. Normally, when the error 54 from these examples starts to rise, then it's time to stop 55 training. 56 57 o stopping_criteria -- A function, that when passed the number of 58 iterations, the training error, and the validation error, will 59 determine when to stop learning. 60 61 o learning_rate -- The learning rate of the neural network. 62 63 o momentum -- The momentum of the NN, which describes how much 64 of the prevoious weight change to use. 65 """ 66 num_iterations = 0 67 while True: 68 num_iterations += 1 69 training_error = 0.0 70 for example in training_examples: 71 # update the predicted values for all of the nodes 72 # based on the current weights and the inputs 73 # This propagates over the entire network from the input. 74 self._input.update(example.inputs) 75 76 # calculate the error via back propagation 77 self._input.backpropagate(example.outputs, 78 learning_rate, momentum) 79 80 # get the errors in our predictions 81 for node in range(len(example.outputs)): 82 training_error += \ 83 self._output.get_error(example.outputs[node], 84 node + 1) 85 86 # get the current testing error for the validation examples 87 validation_error = 0.0 88 for example in validation_examples: 89 predictions = self.predict(example.inputs) 90 91 for prediction_num in range(len(predictions)): 92 real_value = example.outputs[prediction_num] 93 predicted_value = predictions[prediction_num] 94 validation_error += \ 95 0.5 * math.pow((real_value - predicted_value), 2) 96 97 # see if we have gone far enough to stop 98 if stopping_criteria(num_iterations, training_error, 99 validation_error): 100 break
101
102 - def predict(self, inputs):
103 """Predict outputs from the neural network with the given inputs. 104 105 This uses the current neural network to predict outputs, no 106 training of the neural network is done here. 107 """ 108 # update the predicted values for these inputs 109 self._input.update(inputs) 110 111 outputs = [] 112 for output_key in sorted(self._output.values): 113 outputs.append(self._output.values[output_key]) 114 return outputs
115