Back to index

python-biopython  1.60
Network.py
Go to the documentation of this file.
00001 """Represent Neural Networks.
00002 
00003 This module contains classes to represent Generic Neural Networks that
00004 can be trained.
00005 
00006 Many of the ideas in this and other modules were taken from
00007 Neil Schemenauer's bpnn.py, available from:
00008 
00009 http://www.enme.ucalgary.ca/~nascheme/python/bpnn.py
00010 
00011 My sincerest thanks to him for making this available for me to work from,
00012 and my apologies for anything I mangled.
00013 """
00014 # standard library
00015 import math
00016 
00017 class BasicNetwork(object):
00018     """Represent a Basic Neural Network with three layers.
00019 
00020     This deals with a Neural Network containing three layers:
00021 
00022     o Input Layer
00023 
00024     o Hidden Layer
00025 
00026     o Output Layer
00027     """
00028     def __init__(self, input_layer, hidden_layer, output_layer):
00029         """Initialize the network with the three layers.
00030         """
00031         self._input = input_layer
00032         self._hidden = hidden_layer
00033         self._output = output_layer
00034 
00035     def train(self, training_examples, validation_examples,
00036               stopping_criteria, learning_rate, momentum):
00037         """Train the neural network to recognize particular examples.
00038 
00039         Arguments:
00040 
00041         o training_examples -- A list of TrainingExample classes that will
00042         be used to train the network.
00043 
00044         o validation_examples -- A list of TrainingExample classes that
00045         are used to validate the network as it is trained. These examples
00046         are not used to train so the provide an independent method of
00047         checking how the training is doing. Normally, when the error
00048         from these examples starts to rise, then it's time to stop
00049         training.
00050 
00051         o stopping_criteria -- A function, that when passed the number of
00052         iterations, the training error, and the validation error, will
00053         determine when to stop learning.
00054 
00055         o learning_rate -- The learning rate of the neural network.
00056 
00057         o momentum -- The momentum of the NN, which describes how much
00058         of the prevoious weight change to use.
00059         """
00060         num_iterations = 0
00061         while 1:
00062             num_iterations += 1
00063             training_error = 0.0
00064             for example in training_examples:
00065                 # update the predicted values for all of the nodes
00066                 # based on the current weights and the inputs
00067                 # This propogates over the entire network from the input.
00068                 self._input.update(example.inputs)
00069 
00070                 # calculate the error via back propogation
00071                 self._input.backpropagate(example.outputs,
00072                                           learning_rate, momentum)
00073             
00074                 # get the errors in our predictions
00075                 for node in range(len(example.outputs)):
00076                     training_error += \
00077                              self._output.get_error(example.outputs[node],
00078                                                     node + 1)
00079 
00080             # get the current testing error for the validation examples
00081             validation_error = 0.0
00082             for example in validation_examples:
00083                 predictions = self.predict(example.inputs)
00084 
00085                 for prediction_num in range(len(predictions)):
00086                     real_value = example.outputs[prediction_num]
00087                     predicted_value = predictions[prediction_num]
00088                     validation_error += \
00089                             0.5 * math.pow((real_value - predicted_value), 2)
00090 
00091             # see if we have gone far enough to stop
00092             if stopping_criteria(num_iterations, training_error,
00093                                  validation_error):
00094                 break
00095 
00096     def predict(self, inputs):
00097         """Predict outputs from the neural network with the given inputs.
00098 
00099         This uses the current neural network to predict outputs, no
00100         training of the neural network is done here.
00101         """
00102         # update the predicted values for these inputs
00103         self._input.update(inputs)
00104 
00105         output_keys = self._output.values.keys()
00106         output_keys.sort()
00107 
00108         outputs = []
00109         for output_key in output_keys:
00110             outputs.append(self._output.values[output_key])
00111         return outputs