Back to index

python-biopython  1.60
test_NNExclusiveOr.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 """Test function to teach the neural network an XOR function.
00003 
00004 This is a very basic test of Neural Network functionality.
00005 """
00006 # Neural Network code we'll be using
00007 from Bio.NeuralNetwork.Training import TrainingExample
00008 from Bio.NeuralNetwork.BackPropagation import Layer
00009 from Bio.NeuralNetwork.BackPropagation.Network import BasicNetwork
00010 
00011 VERBOSE = 0
00012 
00013 
00014 def main():
00015     """Train a neural network, and then test it to see how it does.
00016 
00017     Since we have so few examples, we use all of them for training,
00018     validation and testing.
00019     """
00020     print "Setting up training examples..."
00021     # set up the training examples
00022     examples = []
00023     examples.append(TrainingExample([0, 0], [0]))
00024     examples.append(TrainingExample([0, 1], [1]))
00025     examples.append(TrainingExample([1, 0], [1]))
00026     examples.append(TrainingExample([1, 1], [0]))
00027 
00028     # create the network
00029     output = Layer.OutputLayer(1)
00030     hidden = Layer.HiddenLayer(3, output)
00031     input = Layer.InputLayer(2, hidden)
00032     
00033     network = BasicNetwork(input, hidden, output)
00034 
00035     print "Training the network..."
00036     # train it
00037     learning_rate = .5
00038     momentum = .1
00039     network.train(examples, examples, stopping_criteria, learning_rate,
00040                   momentum)
00041 
00042     print "Predicting..."
00043     # try predicting
00044     for example in examples:
00045         prediction = network.predict(example.inputs)
00046         if VERBOSE:
00047             print "%s;%s=> %s" % (example.inputs, example.outputs, prediction)
00048 
00049 def stopping_criteria(num_iterations, validation_error, training_error):
00050     """Define when to stop iterating.
00051     """
00052     if num_iterations % 100 == 0:
00053         if VERBOSE:
00054             print "error:", validation_error
00055     if num_iterations >= 2000:
00056         return 1
00057 
00058     return 0
00059 
00060 main()