Back to index

python-biopython  1.60
StopTraining.py
Go to the documentation of this file.
00001 """Classes to help deal with stopping training a neural network.
00002 
00003 One of the key issues with training a neural network is knowning when to
00004 stop the training of the network. This is tricky since you want to keep
00005 training until the neural network has 'learned' the data, but want to
00006 stop before starting to learn the noise in the data.
00007 
00008 This module contains classes and functions which are different ways to
00009 know when to stop training. Remember that the neural network classifier
00010 takes a function to call to know when to stop training, so the classes
00011 in this module should be instaniated, and then the stop_training function
00012 of the classes passed to the network.
00013 """
00014 
00015 class ValidationIncreaseStop(object):
00016     """Class to stop training on a network when the validation error increases.
00017 
00018     Normally, during training of a network, the error will always decrease
00019     on the set of data used in the training. However, if an independent
00020     set of data is used for validation, the error will decrease to a point,
00021     and then start to increase. This increase normally occurs due to the
00022     fact that the network is starting to learn noise in the training data
00023     set. This stopping criterion function will stop when the validation
00024     error increases.
00025     """
00026     def __init__(self, max_iterations = None, min_iterations = 0,
00027                  verbose = 0):
00028         """Initialize the stopping criterion class.
00029 
00030         Arguments:
00031 
00032         o max_iterations - The maximum number of iterations that
00033         should be performed, regardless of error.
00034 
00035         o min_iterations - The minimum number of iterations to perform,
00036         to prevent premature stoppage of training.
00037 
00038         o verbose - Whether or not the error should be printed during
00039         training.
00040         """
00041         self.verbose = verbose
00042         self.max_iterations = max_iterations
00043         self.min_iterations = min_iterations
00044 
00045         self.last_error = None
00046 
00047     def stopping_criteria(self, num_iterations, training_error,
00048                           validation_error):
00049         """Define when to stop iterating.
00050         """
00051         if num_iterations % 10 == 0:
00052             if self.verbose:
00053                 print "%s; Training Error:%s; Validation Error:%s"\
00054                       % (num_iterations, training_error, validation_error)
00055 
00056         if num_iterations > self.min_iterations:
00057             if self.last_error is not None:
00058                 if validation_error > self.last_error:
00059                     if self.verbose:
00060                         print "Validation Error increasing -- Stop"
00061                     return 1
00062 
00063         if self.max_iterations is not None:
00064             if num_iterations > self.max_iterations:
00065                 if self.verbose:
00066                     print "Reached maximum number of iterations -- Stop"
00067                 return 1
00068 
00069         self.last_error = validation_error
00070         return 0