1 """Represent Neural Networks.
2
3 This module contains classes to represent Generic Neural Networks that
4 can be trained.
5
6 Many of the ideas in this and other modules were taken from
7 Neil Schemenauer's bpnn.py, available from:
8
9 http://www.enme.ucalgary.ca/~nascheme/python/bpnn.py
10
11 My sincerest thanks to him for making this available for me to work from,
12 and my apologies for anything I mangled.
13 """
14
15 import math
16
17
19 """Represent a Basic Neural Network with three layers.
20
21 This deals with a Neural Network containing three layers:
22
23 o Input Layer
24
25 o Hidden Layer
26
27 o Output Layer
28 """
29 - def __init__(self, input_layer, hidden_layer, output_layer):
30 """Initialize the network with the three layers.
31 """
32 self._input = input_layer
33 self._hidden = hidden_layer
34 self._output = output_layer
35
36 - def train(self, training_examples, validation_examples,
37 stopping_criteria, learning_rate, momentum):
38 """Train the neural network to recognize particular examples.
39
40 Arguments:
41
42 o training_examples -- A list of TrainingExample classes that will
43 be used to train the network.
44
45 o validation_examples -- A list of TrainingExample classes that
46 are used to validate the network as it is trained. These examples
47 are not used to train so the provide an independent method of
48 checking how the training is doing. Normally, when the error
49 from these examples starts to rise, then it's time to stop
50 training.
51
52 o stopping_criteria -- A function, that when passed the number of
53 iterations, the training error, and the validation error, will
54 determine when to stop learning.
55
56 o learning_rate -- The learning rate of the neural network.
57
58 o momentum -- The momentum of the NN, which describes how much
59 of the prevoious weight change to use.
60 """
61 num_iterations = 0
62 while 1:
63 num_iterations += 1
64 training_error = 0.0
65 for example in training_examples:
66
67
68
69 self._input.update(example.inputs)
70
71
72 self._input.backpropagate(example.outputs,
73 learning_rate, momentum)
74
75
76 for node in range(len(example.outputs)):
77 training_error += \
78 self._output.get_error(example.outputs[node],
79 node + 1)
80
81
82 validation_error = 0.0
83 for example in validation_examples:
84 predictions = self.predict(example.inputs)
85
86 for prediction_num in range(len(predictions)):
87 real_value = example.outputs[prediction_num]
88 predicted_value = predictions[prediction_num]
89 validation_error += \
90 0.5 * math.pow((real_value - predicted_value), 2)
91
92
93 if stopping_criteria(num_iterations, training_error,
94 validation_error):
95 break
96
98 """Predict outputs from the neural network with the given inputs.
99
100 This uses the current neural network to predict outputs, no
101 training of the neural network is done here.
102 """
103
104 self._input.update(inputs)
105
106 output_keys = self._output.values.keys()
107 output_keys.sort()
108
109 outputs = []
110 for output_key in output_keys:
111 outputs.append(self._output.values[output_key])
112 return outputs
113