-
Notifications
You must be signed in to change notification settings - Fork 1
/
back_prop_learning.py
248 lines (183 loc) · 8.6 KB
/
back_prop_learning.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
"""back_prop_learning.py: Backpropagation algorithm for learning in multilayer networks."""
__author__ = "Jordon Dornbos"
import random
import hypothesis_network
import multilayer_network
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np
iteration_count = 0
def back_prop_learning(examples, network, alpha=0.5, iteration_max=1000, weights_loaded=False, verbose=False):
"""Backpropagation algorithm for learning in multilayer networks.
Args:
examples: A set of examples, each with input vector x and output vector y.
network: A multilayer network with L layers, weights W(j,i), activation function g.
alpha: The learning rate.
iteration_max: The maximum amount of iterations to perform.
weights_loaded: Whether or not weights have already been loaded into the network.
verbose: Whether or not to print data values as the network learns.
Returns:
A hypothesis neural network.
"""
delta = [0] * network.num_nodes() # a vector of errors, indexed by network node
if not weights_loaded:
randomize_weights(network, verbose=verbose)
while True:
learn_loop(delta, examples, network, alpha)
# loop until stopping criterion is satisfied
if stop_learning(iteration_max):
break
return hypothesis_network.HypothesisNetwork(network)
def randomize_weights(network, verbose=False, round=False):
"""Function to randomize perceptron weights.
Args:
network: A multilayer network with L layers, weights W(j,i), activation function g.
verbose: Whether or not to print out the weights that were assigned.
round: Whether or not to round the printed weights.
"""
for l in range(1, network.num_layers()):
for j in range(network.get_layer(l).num_nodes):
for w in range(len(network.get_node_with_layer(l, j).weights)):
network.get_node_with_layer(l, j).weights[w] = random.random()
if verbose:
print 'Randomized weights:'
network.print_weights(round)
def learn_loop(delta, examples, network, alpha):
"""A loop representing the learning process.
Args:
delta: A list of all the delta values for the network.
examples: A set of examples, each with input vector x and output vector y.
network: A multilayer network with L layers, weights W(j,i), activation function g.
alpha: The learning rate.
"""
for example in examples:
load_and_feed(example.x, network)
# compute the error at the output
for j in range(network.output_layer.num_nodes):
delta[network.position_in_network(network.num_layers() - 1, j)] = multilayer_network.sigmoid_derivative(
network.output_layer.nodes[j].in_sum) * (example.y[j] - network.output_layer.nodes[j].output)
# propagate the deltas backward from output layer to input layer
delta_propagation(delta, network)
# update every weight in the network using deltas
update_weights(delta, network, alpha)
def load_and_feed(input, network):
"""Function to load the input into the network and propagate the data through the network.
Args:
input: The values to input into the network.
network: A multilayer network with L layers, weights W(j,i), activation function g.
"""
# propagate the inputs forward to compute the outputs
for i in range(len(network.input_layer.nodes)):
network.input_layer.nodes[i].output = input[i]
# feed the values forward
feed_forward(network)
def feed_forward(network):
"""Function to feed forward values in the network.
Args:
network: A multilayer network with L layers, weights W(j,i), activation function g.
"""
for l in range(1, network.num_layers()):
for j in range(network.get_layer(l).num_nodes):
node = network.get_node_with_layer(l, j)
summation = 0
for i in range(node.num_inputs):
summation += node.weights[i] * network.get_node_with_layer(l - 1, i).output
summation += node.weights[len(node.weights) - 1] # bias input
network.get_node_with_layer(l, j).in_sum = summation
network.get_node_with_layer(l, j).output = multilayer_network.sigmoid(summation)
def delta_propagation(delta, network):
"""Function for backpropagation the delta values.
Args:
delta: A list of all the delta values for the network.
network: A multilayer network with L layers, weights W(j,i), activation function g.
"""
for l in range(network.num_layers() - 2, 0, -1):
for i in range(network.get_layer(l).num_nodes):
summation = 0
next_layer_nodes = network.get_layer(l + 1).nodes
for j in range(len(next_layer_nodes)):
summation += next_layer_nodes[j].weights[i] * delta[network.position_in_network(l + 1, j)]
# "blame" a node as much as its weight
delta[network.position_in_network(l, i)] = multilayer_network.sigmoid_derivative(
network.get_node_with_layer(l, i).in_sum) * summation
def update_weights(delta, network, alpha):
"""Function to update the weights in the network.
Args:
delta: A list of all the delta values for the network.
network: A multilayer network with L layers, weights W(j,i), activation function g.
alpha: The learning rate.
"""
for l in range(1, network.num_layers()):
for j in range(network.get_layer(l).num_nodes):
# adjust the weights
node = network.get_node_with_layer(l, j)
for i in range(node.num_inputs):
node.weights[i] += alpha * network.get_node_with_layer(l - 1, i).output * delta[
network.position_in_network(l, j)]
node.weights[len(node.weights) - 1] += alpha * delta[network.position_in_network(l, j)] # bias input
def stop_learning(iteration_max):
"""Method to determine when to stop learning.
Args:
iteration_max: The maximum amount of iterations to perform.
Returns:
A boolean for whether or not to stop.
"""
# timeout reached
global iteration_count
iteration_count += 1
if iteration_count == iteration_max:
return True
# otherwise, keep going
return False
def learn_and_plot(examples, network, min, max, step, alpha=0.5, iteration_max=1000, weights_loaded=False,
verbose=False, title='Unknown Function'):
"""Function to plot the network during the learning process. I based this code off of examples from the
matplotlib documentation provided online.
Args:
examples: A set of examples, each with input vector x and output vector y.
network: A multilayer network with L layers, weights W(j,i), activation function g.
min: The min x/y coordinate to graph to.
max: The max x/y coordinate to graph to.
step: The x/y step to graph.
alpha: The learning rate.
iteration_max: The maximum amount of iterations to perform.
weights_loaded: Whether or not weights have already been loaded into the network.
verbose: Whether or not to print data values as the network learns.
title: The name of the function being learned.
Returns:
A hypothesis neural network.
"""
delta = [0] * network.num_nodes() # a vector of errors, indexed by network node
if not weights_loaded:
randomize_weights(network, verbose=verbose)
# set up the plot
plt.ion()
fig = plt.figure()
ax = Axes3D(fig)
x = y = np.arange(min, max, step)
x_grid, y_grid = np.meshgrid(x, y)
# do learning
learn_loop(delta, examples, network, alpha)
hypothesis = hypothesis_network.HypothesisNetwork(network)
zs = np.array([(hypothesis.guess([x, y])[0]) for x, y in zip(np.ravel(x_grid), np.ravel(y_grid))])
z_grid = zs.reshape(x_grid.shape)
# display the plot
ax.plot_surface(x_grid, y_grid, z_grid)
fig.suptitle('Neural Network Learning - ' + title, fontsize=14)
plt.show()
step = iteration_max / 20
for i in range(20):
# do learning
for j in range(step):
learn_loop(delta, examples, network, alpha)
hypothesis = hypothesis_network.HypothesisNetwork(network)
# update the plot
zs = np.array([(hypothesis.guess([x, y])[0]) for x, y in zip(np.ravel(x_grid), np.ravel(y_grid))])
z_grid = zs.reshape(x_grid.shape)
ax.clear()
ax.plot_surface(x_grid, y_grid, z_grid)
plt.draw()
# display the final plot
plt.ioff()
plt.show()
return hypothesis