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