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

Source Code for Module Bio.NeuralNetwork.Training

 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  """Provide classes for dealing with Training Neural Networks. 
 7  """ 
 8  # standard modules 
 9  import random 
10   
11   
12 -class TrainingExample(object):
13 """Hold inputs and outputs of a training example. 14 15 XXX Do I really need this? 16 """
17 - def __init__(self, inputs, outputs, name=""):
18 self.name = name 19 self.inputs = inputs 20 self.outputs = outputs
21 22
23 -class ExampleManager(object):
24 """Manage a grouping of Training Examples. 25 26 This is meant to make it easy to split a bunch of training examples 27 into three types of data: 28 29 o Training Data -- These are the data used to do the actual training 30 of the network. 31 32 o Validation Data -- These data are used to validate the network 33 while training. They provide an independent method to evaluate how 34 the network is doing, and make sure the network gets trained independent 35 of noise in the training data set. 36 37 o Testing Data -- The data which are used to verify how well a network 38 works. They should not be used at all in the training process, so they 39 provide a completely independent method of testing how well a network 40 performs. 41 """
42 - def __init__(self, training_percent=.4, validation_percent=.4):
43 """Initialize the manager with the training examples. 44 45 Arguments: 46 47 o training_percent - The percentage of the training examples that 48 should be used for training the network. 49 50 o validation_percent - Percent of training examples for validating 51 a network during training. 52 53 Attributes: 54 55 o train_examples - A randomly chosen set of examples for training 56 purposes. 57 58 o valdiation_examples - Randomly chosesn set of examples for 59 use in validation of a network during training. 60 61 o test_examples - Examples for training purposes. 62 """ 63 assert training_percent + validation_percent <= 1.0, \ 64 "Training and validation percentages more than 100 percent" 65 66 self.train_examples = [] 67 self.validation_examples = [] 68 self.test_examples = [] 69 70 self.training_percent = training_percent 71 self.validation_percent = validation_percent
72
73 - def add_examples(self, training_examples):
74 """Add a set of training examples to the manager. 75 76 Arguments: 77 78 o training_examples - A list of TrainingExamples to manage. 79 """ 80 placement_rand = random.Random() 81 82 # assign exact example randomly to the example types 83 for example in training_examples: 84 chance_num = placement_rand.random() 85 # assign with the specified percentage 86 if chance_num <= self.training_percent: 87 self.train_examples.append(example) 88 elif chance_num <= (self.training_percent + 89 self.validation_percent): 90 self.validation_examples.append(example) 91 else: 92 self.test_examples.append(example)
93