Back to index

python-biopython  1.60
Training.py
Go to the documentation of this file.
00001 """Provide classes for dealing with Training Neural Networks.
00002 """
00003 # standard modules
00004 import random
00005 
00006 class TrainingExample(object):
00007     """Hold inputs and outputs of a training example.
00008 
00009     XXX Do I really need this? 
00010     """
00011     def __init__(self, inputs, outputs, name = ""):
00012         self.name = name
00013         self.inputs = inputs
00014         self.outputs = outputs
00015 
00016 class ExampleManager(object):
00017     """Manage a grouping of Training Examples.
00018 
00019     This is meant to make it easy to split a bunch of training examples
00020     into three types of data:
00021 
00022     o Training Data -- These are the data used to do the actual training
00023     of the network.
00024 
00025     o Validation Data -- These data are used to validate the network
00026     while training. They provide an independent method to evaluate how
00027     the network is doing, and make sure the network gets trained independent
00028     of noise in the training data set.
00029 
00030     o Testing Data -- The data which are used to verify how well a network
00031     works. They should not be used at all in the training process, so they
00032     provide a completely independent method of testing how well a network
00033     performs.
00034     """
00035     def __init__(self, training_percent = .4, validation_percent = .4):
00036         """Initialize the manager with the training examples.
00037 
00038         Arguments:
00039 
00040         o training_percent - The percentage of the training examples that
00041         should be used for training the network.
00042 
00043         o validation_percent - Percent of training examples for validating
00044         a network during training.
00045 
00046         Attributes:
00047 
00048         o train_examples - A randomly chosen set of examples for training
00049         purposes.
00050 
00051         o valdiation_examples - Randomly chosesn set of examples for
00052         use in validation of a network during training.
00053 
00054         o test_examples - Examples for training purposes.
00055         """
00056         assert training_percent + validation_percent <= 1.0, \
00057                "Training and validation percentages more than 100 percent"
00058         
00059         self.train_examples = []
00060         self.validation_examples = []
00061         self.test_examples = []
00062 
00063         self.training_percent = training_percent
00064         self.validation_percent = validation_percent
00065 
00066     def add_examples(self, training_examples):
00067         """Add a set of training examples to the manager.
00068 
00069         Arguments:
00070 
00071         o training_examples - A list of TrainingExamples to manage.
00072         """
00073         placement_rand = random.Random()
00074         
00075         # assign exact example randomly to the example types
00076         for example in training_examples:
00077             chance_num = placement_rand.random()
00078             # assign with the specified percentage
00079             if chance_num <= self.training_percent:
00080                 self.train_examples.append(example)
00081             elif chance_num <= (self.training_percent +
00082                                 self.validation_percent):
00083                 self.validation_examples.append(example)
00084             else:
00085                 self.test_examples.append(example)
00086         
00087                 
00088         
00089