-
Notifications
You must be signed in to change notification settings - Fork 161
/
Copy path02_MachineLearning_Backpropagation.py
383 lines (239 loc) · 9.68 KB
/
02_MachineLearning_Backpropagation.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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
# coding: utf-8
# # Neural Networks: Backpropagation with Pure Python
# Example code for the lecture series "Machine Learning for Physicists" by Florian Marquardt
#
# Lecture 2
#
# See https://machine-learning-for-physicists.org and the current course website linked there!
# This notebook shows how to:
# - implement backpropagation in pure python
# This is basically the code shown in the lecture, with a tiny bit of clean-up and extra comments.
# ### Imports: only numpy and matplotlib
# In[1]:
# get the "numpy" library for linear algebra
# In the lecture videos, I do this:
#
# from numpy import *
#
# WARNING: It is generally considered bad
# programming style to "import *", as it
# can lead to confusion. For me, I
# (1) ALWAYS import numpy
# (2) NEVER import any other package in this * way
# Therefore, there is never confusion for me, and
# it makes my code a bit more readable (for me).
# However, since 99% of people are using the
# syntax "import numpy as np" and then
# access "np.exp()" etc., you
# should probably also use "np" once you start
# exchanging code with others. I convert
# back to the np. syntax when I turn my
# converged code into a module.
#
# It is apparently officially accepted to explicitly
# list all the functions you need from numpy:
from numpy import array, zeros, exp, random, dot, shape, reshape, meshgrid, linspace, transpose
import matplotlib.pyplot as plt # for plotting
import matplotlib
matplotlib.rcParams['figure.dpi']=300 # highres display
# # Backpropagation
# ## Implement backpropagation for a general (fully connected) network
# In[2]:
def net_f_df(z): # calculate f(z) and f'(z)
val=1/(1+exp(-z)) # sigmoid
return(val,exp(-z)*(val**2)) # return both f and f'
# In[3]:
def forward_step(y,w,b): # calculate values in next layer, from input y
z=dot(y,w)+b # w=weights, b=bias vector for next layer
return(net_f_df(z)) # apply nonlinearity and return result
# In[4]:
def apply_net(y_in): # one forward pass through the network
global Weights, Biases, NumLayers
global y_layer, df_layer # for storing y-values and df/dz values
y=y_in # start with input values
y_layer[0]=y
for j in range(NumLayers): # loop through all layers
# j=0 corresponds to the first layer above the input
y,df=forward_step(y,Weights[j],Biases[j]) # one step
df_layer[j]=df # store f'(z) [needed later in backprop]
y_layer[j+1]=y # store f(z) [also needed in backprop]
return(y)
# In[5]:
def apply_net_simple(y_in): # one forward pass through the network
# no storage for backprop (this is used for simple tests)
y=y_in # start with input values
y_layer[0]=y
for j in range(NumLayers): # loop through all layers
# j=0 corresponds to the first layer above the input
y,df=forward_step(y,Weights[j],Biases[j]) # one step
return(y)
# In[6]:
def backward_step(delta,w,df):
# delta at layer N, of batchsize x layersize(N))
# w between N-1 and N [layersize(N-1) x layersize(N) matrix]
# df = df/dz at layer N-1, of batchsize x layersize(N-1)
return( dot(delta,transpose(w))*df )
# In[7]:
def backprop(y_target): # one backward pass through the network
# the result will be the 'dw_layer' matrices that contain
# the derivatives of the cost function with respect to
# the corresponding weight
global y_layer, df_layer, Weights, Biases, NumLayers
global dw_layer, db_layer # dCost/dw and dCost/db (w,b=weights,biases)
global batchsize
delta=(y_layer[-1]-y_target)*df_layer[-1]
dw_layer[-1]=dot(transpose(y_layer[-2]),delta)/batchsize
db_layer[-1]=delta.sum(0)/batchsize
for j in range(NumLayers-1):
delta=backward_step(delta,Weights[-1-j],df_layer[-2-j])
dw_layer[-2-j]=dot(transpose(y_layer[-3-j]),delta)
db_layer[-2-j]=delta.sum(0)/batchsize
# In[8]:
def gradient_step(eta): # update weights & biases (after backprop!)
global dw_layer, db_layer, Weights, Biases
for j in range(NumLayers):
Weights[j]-=eta*dw_layer[j]
Biases[j]-=eta*db_layer[j]
# In[9]:
def train_net(y_in,y_target,eta): # one full training batch
# y_in is an array of size batchsize x (input-layer-size)
# y_target is an array of size batchsize x (output-layer-size)
# eta is the stepsize for the gradient descent
global y_out_result
y_out_result=apply_net(y_in)
backprop(y_target)
gradient_step(eta)
cost=((y_target-y_out_result)**2).sum()/batchsize
return(cost)
# ## Setup for a particular set of layer sizes
# In[10]:
# set up all the weights and biases
NumLayers=3 # does not count input-layer (but does count output)
LayerSizes=[2,20,30,1] # input-layer,hidden-1,hidden-2,...,output-layer
# initialize random weights and biases for all layers (except input of course)
Weights=[random.uniform(low=-1,high=+1,size=[ LayerSizes[j],LayerSizes[j+1] ]) for j in range(NumLayers)]
Biases=[random.uniform(low=-1,high=+1,size=LayerSizes[j+1]) for j in range(NumLayers)]
# In[11]:
# set up all the helper variables
y_layer=[zeros(LayerSizes[j]) for j in range(NumLayers+1)]
df_layer=[zeros(LayerSizes[j+1]) for j in range(NumLayers)]
dw_layer=[zeros([LayerSizes[j],LayerSizes[j+1]]) for j in range(NumLayers)]
db_layer=[zeros(LayerSizes[j+1]) for j in range(NumLayers)]
# In[12]:
# define the batchsize
batchsize=100
# ## Train the net on one single batch repeatedly (not so good)
# In[13]:
y_in=random.uniform(low=-1,high=+1,size=[batchsize,LayerSizes[0]])
y_target=random.uniform(low=-1,high=+1,size=[batchsize,LayerSizes[-1]])
# In[14]:
# one training step:
train_net(y_in,y_target,.0001) # returns cost function value
# In[15]:
eta=0.001
batches=200
costs=zeros(batches) # array to store the costs
for k in range(batches):
costs[k]=train_net(y_in,y_target,eta)
# In[16]:
# this will show a very simple decrease, because
# we are not yet stochastically sampling inputs
# (it is always the SAME input! so the network
# only becomes good for that input)
plt.plot(costs)
plt.show()
# ### Produce random batches: randomly sample a function defined on a 2D square
# In[17]:
# For a change: Set up rectified linear units (relu)
# instead of sigmoid
def net_f_df(z): # calculate f(z) and f'(z)
val=z*(z>0)
return(val,z>0) # return both f and f'
# In[18]:
# set up all the weights and biases
NumLayers=2 # does not count input-layer (but does count output)
LayerSizes=[2,100,1] # input-layer,hidden-1,hidden-2,...,output-layer
Weights=[random.uniform(low=-0.1,high=+0.1,size=[ LayerSizes[j],LayerSizes[j+1] ]) for j in range(NumLayers)]
Biases=[zeros(LayerSizes[j+1]) for j in range(NumLayers)]
# set up all the helper variables
y_layer=[zeros(LayerSizes[j]) for j in range(NumLayers+1)]
df_layer=[zeros(LayerSizes[j+1]) for j in range(NumLayers)]
dw_layer=[zeros([LayerSizes[j],LayerSizes[j+1]]) for j in range(NumLayers)]
db_layer=[zeros(LayerSizes[j+1]) for j in range(NumLayers)]
# In[20]:
# define the batchsize
batchsize=100
# In[21]:
def myFunc(x0,x1):
r2=x0**2+x1**2
return(exp(-5*r2)*abs(x1+x0))
xrange=linspace(-0.5,0.5,40)
X0,X1=meshgrid(xrange,xrange)
plt.imshow(myFunc(X0,X1),interpolation='nearest',origin='lower')
plt.show()
# In[22]:
def make_batch():
global batchsize
inputs=random.uniform(low=-0.5,high=+0.5,size=[batchsize,2])
targets=zeros([batchsize,1]) # must have right dimensions
targets[:,0]=myFunc(inputs[:,0],inputs[:,1])
return(inputs,targets)
# In[23]:
# try to evaluate the (randomly initialized) network
# on some area in the 2D plane
test_batchsize=shape(X0)[0]*shape(X0)[1]
testsample=zeros([test_batchsize,2])
testsample[:,0]=X0.flatten()
testsample[:,1]=X1.flatten()
testoutput=apply_net_simple(testsample)
myim=plt.imshow(reshape(testoutput,shape(X0)),origin='lower',interpolation='none')
plt.show()
# In[24]:
# now train on randomly sampled points
# to make the network reproduce better and
# better this 2D function!
eta=.001
batches=2000
costs=zeros(batches)
for k in range(batches):
y_in,y_target=make_batch()
costs[k]=train_net(y_in,y_target,eta)
plt.plot(costs)
plt.title("Cost function during training")
plt.show()
# # Animate the network results during training
# In[27]:
# start fresh:
# set up all the weights and biases
NumLayers=2 # does not count input-layer (but does count output)
LayerSizes=[2,100,1] # input-layer,hidden-1,hidden-2,...,output-layer
Weights=[random.uniform(low=-0.1,high=+0.1,size=[ LayerSizes[j],LayerSizes[j+1] ]) for j in range(NumLayers)]
Biases=[zeros(LayerSizes[j+1]) for j in range(NumLayers)]
# set up all the helper variables
y_layer=[zeros(LayerSizes[j]) for j in range(NumLayers+1)]
df_layer=[zeros(LayerSizes[j+1]) for j in range(NumLayers)]
dw_layer=[zeros([LayerSizes[j],LayerSizes[j+1]]) for j in range(NumLayers)]
db_layer=[zeros(LayerSizes[j+1]) for j in range(NumLayers)]
# In[30]:
# import functions for updating display
# (simple animation)
from IPython.display import clear_output
from time import sleep
eta=0.01 # learning rate
nsteps=100
costs=zeros(nsteps)
for j in range(nsteps):
clear_output(wait=True)
fig,ax=plt.subplots(ncols=2,nrows=1,figsize=(8,4)) # prepare figure
ax[1].axis('off') # no axes
# the crucial lines:
y_in,y_target=make_batch() # random samples (points in 2D)
costs[j]=train_net(y_in,y_target,eta) # train network (one step, on this batch)
testoutput=apply_net_simple(testsample) # check the new network output in the plane
img=ax[1].imshow(reshape(testoutput,shape(X0)),interpolation='nearest',origin='lower') # plot image
ax[0].plot(costs)
ax[0].set_title("Cost during training")
ax[0].set_xlabel("number of batches")
ax[1].set_title("Current network prediction")
plt.show()
sleep(0.1)