-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathnnode2ivp.py
More file actions
452 lines (391 loc) · 16.1 KB
/
nnode2ivp.py
File metadata and controls
452 lines (391 loc) · 16.1 KB
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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
"""
NNODE2IVP - Class to solve 2nd-order ordinary differential equation initial
value problems using a neural network
This module provides the functionality to solve 2nd-order ordinary differential
equation initial value problems using a neural network.
Example:
Create an empty NNODE2IVP object.
net = NNODE2IVP()
Create an NNODE2IVP object for a ODE2IVP object.
net = NNODE2IVP(ode2ivp_obj)
Attributes:
None
Methods:
__init__
__str__
train
run
run_derivative
run_2nd_derivative
Todo:
* Expand base functionality.
* Combine error and gradient code into a single function for speed.
"""
from math import sqrt
import numpy as np
from scipy.optimize import minimize
from ode2ivp import ODE2IVP
from sigma import sigma, dsigma_dz, d2sigma_dz2, d3sigma_dz3
from slffnn import SLFFNN
# Default values for method parameters
DEFAULT_DEBUG = False
DEFAULT_ETA = 0.01
DEFAULT_MAXEPOCHS = 1000
DEFAULT_NHID = 10
DEFAULT_TRAINALG = 'delta'
DEFAULT_UMAX = 1
DEFAULT_UMIN = -1
DEFAULT_VERBOSE = False
DEFAULT_VMAX = 1
DEFAULT_VMIN = -1
DEFAULT_WMAX = 1
DEFAULT_WMIN = -1
DEFAULT_OPTS = {
'debug': DEFAULT_DEBUG,
'eta': DEFAULT_ETA,
'maxepochs': DEFAULT_MAXEPOCHS,
'nhid': DEFAULT_NHID,
'umax': DEFAULT_UMAX,
'umin': DEFAULT_UMIN,
'verbose': DEFAULT_VERBOSE,
'vmax': DEFAULT_VMAX,
'vmin': DEFAULT_VMIN,
'wmax': DEFAULT_WMAX,
'wmin': DEFAULT_WMIN
}
# Vectorize sigma functions.
sigma_v = np.vectorize(sigma)
dsigma_dz_v = np.vectorize(dsigma_dz)
d2sigma_dz2_v = np.vectorize(d2sigma_dz2)
d3sigma_dz3_v = np.vectorize(d3sigma_dz3)
class NNODE2IVP(SLFFNN):
"""Solve a 2nd-order ODE IVP with a neural network."""
# Public methods
def __init__(self, eq, nhid=DEFAULT_NHID):
super().__init__()
self.eq = eq
self.w = np.zeros(nhid)
self.u = np.zeros(nhid)
self.v = np.zeros(nhid)
# Pre-vectorize functions for efficiency.
self.Gf_v = np.vectorize(self.eq.Gf)
self.dG_dyf_v = np.vectorize(self.eq.dG_dyf)
self.dG_dydxf_v = np.vectorize(self.eq.dG_dydxf)
self.dG_d2ydx2f_v = np.vectorize(self.eq.dG_d2ydx2f)
def __str__(self):
s = ''
s += "NNODE2IVP:\n"
s += "%s\n" % self.eq
s += "w = %s\n" % self.w
s += "u = %s\n" % self.u
s += "v = %s\n" % self.v
return s.rstrip()
def train(self, x, trainalg=DEFAULT_TRAINALG, opts=DEFAULT_OPTS):
"""Train the network. """
my_opts = dict(DEFAULT_OPTS)
my_opts.update(opts)
if trainalg == 'delta':
self.__train_delta(x, my_opts)
elif trainalg in ('Nelder-Mead', 'Powell', 'CG', 'BFGS',
'Newton-CG', 'L-BFGS-B', 'TNC', 'SLSQP'):
self.__train_minimize(x, trainalg, my_opts)
else:
print('ERROR: Invalid training algorithm (%s)!' % trainalg)
exit(0)
def run(self, x):
"""Compute the trained solution."""
z = np.outer(x, self.w) + self.u
s = sigma_v(z)
N = s.dot(self.v)
yt = self.__ytf(x, N)
return yt
def run_derivative(self, x):
"""Compute the trained derivative."""
z = np.outer(x, self.w) + self.u
s = sigma_v(z)
s1 = dsigma_dz_v(z)
N = s.dot(self.v)
dN_dx = s1.dot(self.v*self.w)
dyt_dx = self.__dyt_dxf(x, N, dN_dx)
return dyt_dx
def run_2nd_derivative(self, x):
"""Compute the trained 2nd derivative."""
z = np.outer(x, self.w) + self.u
s = sigma_v(z)
s1 = dsigma_dz_v(z)
s2 = d2sigma_dz2_v(z)
N = s.dot(self.v)
dN_dx = s1.dot(self.v*self.w)
d2N_dx2 = s2.dot(self.v*self.w**2)
d2yt_dx2 = self.__d2yt_dx2f(x, N, dN_dx, d2N_dx2)
return d2yt_dx2
# Internal methods below this point
def __ytf(self, x, N):
"""Trial function, x and N are 1xn arrays"""
return self.eq.ic + x*self.eq.ic1 + x**2*N
def __dyt_dxf(self, x, N, dN_dx):
"""First derivative of trial function, all are 1xn arrays"""
return self.eq.ic1 + x**2*dN_dx + 2*x*N
def __d2yt_dx2f(self, x, N, dN_dx, d2N_dx2):
"""2nd derivative of trial function, all are 1xn arrays"""
return x**2*d2N_dx2 + 4*x*dN_dx + 2*N
def __train_delta(self, x, opts=DEFAULT_OPTS):
"""Train the network with the delta method."""
my_opts = dict(DEFAULT_OPTS)
my_opts.update(opts)
# Sanity-check arguments.
assert x.any()
assert opts['maxepochs'] > 0
assert opts['eta'] > 0
assert opts['vmin'] < opts['vmax']
assert opts['wmin'] < opts['wmax']
assert opts['umin'] < opts['umax']
# ---------------------------------------------------------------------
# Determine the number of training points, and change notation for
# convenience.
n = len(x)
H = opts['nhid']
# Create the hidden node weights, biases, and output node weights.
self.w = np.random.uniform(opts['wmin'], opts['wmax'], H)
self.u = np.random.uniform(opts['umin'], opts['umax'], H)
self.v = np.random.uniform(opts['vmin'], opts['vmax'], H)
# Initial parameter deltas are 0.
dE_dv = np.zeros(H)
dE_du = np.zeros(H)
dE_dw = np.zeros(H)
# Train the network.
for epoch in range(opts['maxepochs']):
if opts['debug']:
print('Starting epoch %d.' % epoch)
# Compute the new values of the network parameters.
self.w -= opts['eta']*dE_dw
self.u -= opts['eta']*dE_du
self.v -= opts['eta']*dE_dv
# Compute the input, the sigmoid function, and its
# derivatives, for each hidden node k, for each training
# point i.
z = np.outer(x, self.w) + self.u # n x H array
s = sigma_v(z)
s1 = dsigma_dz_v(z)
s2 = d2sigma_dz2_v(z)
s3 = d3sigma_dz3_v(z)
# Compute the network output and its derivatives, for each
# training point.
N = s.dot(self.v)
dN_dx = s1.dot(self.v*self.w)
d2N_dx2 = s2.dot(self.v*self.w**2)
dN_dw = s1*np.outer(x, self.v)
dN_du = s1*self.v
dN_dv = s
d2N_dwdx = self.v*(s1 + s2*np.outer(x, self.w))
d2N_dudx = self.v*s2*self.w
d2N_dvdx = s1*self.w
d3N_dwdx2 = self.v*(2*s2*self.w + s3*np.outer(x, self.w**2))
d3N_dudx2 = self.v*s3*self.w**2
d3N_dvdx2 = s2*self.w**2
# Compute the value of the trial solution and its derivatives,
# for each training point.
yt = self.__ytf(x, N)
dyt_dx = self.__dyt_dxf(x, N, dN_dx)
d2yt_dx2 = self.__d2yt_dx2f(x, N, dN_dx, d2N_dx2)
dyt_dw = np.broadcast_to(x**2, (H, n)).T*dN_dw
dyt_du = np.broadcast_to(x**2, (H, n)).T*dN_du
dyt_dv = np.broadcast_to(x**2, (H, n)).T*dN_dv
d2yt_dwdx = np.broadcast_to(x**2, (H, n)).T*d2N_dwdx + \
2*np.broadcast_to(x, (H, n)).T*dN_dw
d2yt_dudx = np.broadcast_to(x**2, (H, n)).T*d2N_dudx + \
2*np.broadcast_to(x, (H, n)).T*dN_du
d2yt_dvdx = np.broadcast_to(x**2, (H, n)).T*d2N_dvdx + \
2*np.broadcast_to(x, (H, n)).T*dN_dv
d3yt_dwdx2 = np.broadcast_to(x**2, (H, n)).T*d3N_dwdx2 + \
4*np.broadcast_to(x, (H, n)).T*d2N_dwdx + 2*dN_dw
d3yt_dudx2 = np.broadcast_to(x**2, (H, n)).T*d3N_dudx2 + \
4*np.broadcast_to(x, (H, n)).T*d2N_dudx + 2*dN_du
d3yt_dvdx2 = np.broadcast_to(x**2, (H, n)).T*d3N_dvdx2 + \
4*np.broadcast_to(x, (H, n)).T*d2N_dvdx + 2*dN_dv
# Compute the value of the original differential equation for
# each training point, and its derivatives.
G = self.Gf_v(x, yt, dyt_dx, d2yt_dx2)
dG_dyt = self.dG_dyf_v(x, yt, dyt_dx, d2yt_dx2)
dG_dytdx = self.dG_dydxf_v(x, yt, dyt_dx, d2yt_dx2)
dG_d2ytdx2 = self.dG_d2ydx2f_v(x, yt, dyt_dx, d2yt_dx2)
dG_dw = np.broadcast_to(dG_dyt, (H, n)).T*dyt_dw + \
np.broadcast_to(dG_dytdx, (H, n)).T*d2yt_dwdx + \
np.broadcast_to(dG_d2ytdx2, (H, n)).T*d3yt_dwdx2
dG_du = np.broadcast_to(dG_dyt, (H, n)).T*dyt_du + \
np.broadcast_to(dG_dytdx, (H, n)).T*d2yt_dudx + \
np.broadcast_to(dG_d2ytdx2, (H, n)).T*d3yt_dudx2
dG_dv = np.broadcast_to(dG_dyt, (H, n)).T*dyt_dv + \
np.broadcast_to(dG_dytdx, (H, n)).T*d2yt_dvdx + \
np.broadcast_to(dG_d2ytdx2, (H, n)).T*d3yt_dvdx2
# Compute the error function for this epoch.
E = np.sum(G**2)
# Compute the partial derivatives of the error with respect to
# the network parameters.
dE_dw = 2*np.sum(np.broadcast_to(G, (H, n)).T*dG_dw, axis=0)
dE_du = 2*np.sum(np.broadcast_to(G, (H, n)).T*dG_du, axis=0)
dE_dv = 2*np.sum(np.broadcast_to(G, (H, n)).T*dG_dv, axis=0)
# Record the current RMSE.
rmse = sqrt(E/n)
if opts['verbose']:
print(epoch, rmse)
def __train_minimize(self, x, trainalg, opts=DEFAULT_OPTS):
"""Train the network with minimize(). """
my_opts = dict(DEFAULT_OPTS)
my_opts.update(opts)
# Sanity-check arguments.
assert x.any()
assert opts['vmin'] < opts['vmax']
assert opts['wmin'] < opts['wmax']
assert opts['umin'] < opts['umax']
# ---------------------------------------------------------------------
# Create the hidden node weights, biases, and output node weights.
H = opts['nhid']
self.w = np.random.uniform(opts['wmin'], opts['wmax'], H)
self.u = np.random.uniform(opts['umin'], opts['umax'], H)
self.v = np.random.uniform(opts['vmin'], opts['vmax'], H)
# Assemble the network parameters into a single 1-D vector for
# use by the minimize() method.
p = np.hstack((self.w, self.u, self.v))
# Minimize the error function to get the new parameter values.
if trainalg in ('Nelder-Mead', 'Powell', 'CG', 'BFGS'):
jac = None
elif trainalg in ('Newton-CG', 'L-BFGS-B', 'TNC', 'SLSQP'):
jac = self.__compute_error_gradient
res = minimize(self.__compute_error, p, method=trainalg, jac=jac,
args=(x))
# Unpack the optimized network parameters.
self.w = res.x[0:H]
self.u = res.x[H:2*H]
self.v = res.x[2*H:3*H]
def __compute_error(self, p, x):
"""Compute the error function using the current parameter values."""
# Unpack the network parameters.
H = len(self.w)
w = p[0:H]
u = p[H:2*H]
v = p[2*H:3*H]
# Compute the forward pass through the network.
z = np.outer(x, w) + u
s = sigma_v(z)
s1 = dsigma_dz_v(z)
s2 = d2sigma_dz2_v(z)
N = s.dot(v)
dN_dx = s1.dot(v*w)
d2N_dx2 = s2.dot(v*w**2)
yt = self.__ytf(x, N)
dyt_dx = self.__dyt_dxf(x, N, dN_dx)
d2yt_dx2 = self.__d2yt_dx2f(x, N, dN_dx, d2N_dx2)
G = self.Gf_v(x, yt, dyt_dx, d2yt_dx2)
E2 = np.sum(G**2)
return E2
def __compute_error_gradient(self, p, x):
"""Compute the gradient of the error function wrt network
parameters."""
# Compute the number of training points.
n = len(x)
# Unpack the network parameters.
H = len(self.w)
w = p[0:H]
u = p[H:2*H]
v = p[2*H:3*H]
# Compute the forward pass through the network.
z = np.outer(x, w) + u
s = sigma_v(z)
s1 = dsigma_dz_v(z)
s2 = d2sigma_dz2_v(z)
s3 = d3sigma_dz3_v(z)
N = s.dot(v)
dN_dx = s1.dot(v*w)
d2N_dx2 = s2.dot(v*w**2)
dN_dw = s1*np.outer(x, v)
dN_du = s1*v
dN_dv = s
d2N_dwdx = v*(s1 + s2*np.outer(x, w))
d2N_dudx = v*s2*w
d2N_dvdx = s1*w
d3N_dwdx2 = v*(2*s2*w + s3*np.outer(x, w**2))
d3N_dudx2 = v*s3*w**2
d3N_dvdx2 = s2*w**2
yt = self.__ytf(x, N)
dyt_dx = self.__dyt_dxf(x, N, dN_dx)
d2yt_dx2 = self.__d2yt_dx2f(x, N, dN_dx, d2N_dx2)
dyt_dw = np.broadcast_to(x**2, (H, n)).T*dN_dw
dyt_du = np.broadcast_to(x**2, (H, n)).T*dN_du
dyt_dv = np.broadcast_to(x**2, (H, n)).T*dN_dv
d2yt_dwdx = np.broadcast_to(x**2, (H, n)).T*d2N_dwdx + \
2*np.broadcast_to(x, (H, n)).T*dN_dw
d2yt_dudx = np.broadcast_to(x**2, (H, n)).T*d2N_dudx + \
2*np.broadcast_to(x, (H, n)).T*dN_du
d2yt_dvdx = np.broadcast_to(x**2, (H, n)).T*d2N_dvdx + \
2*np.broadcast_to(x, (H, n)).T*dN_dv
d3yt_dwdx2 = np.broadcast_to(x**2, (H, n)).T*d3N_dwdx2 + \
4*np.broadcast_to(x, (H, n)).T*d2N_dwdx + 2*dN_dw
d3yt_dudx2 = np.broadcast_to(x**2, (H, n)).T*d3N_dudx2 + \
4*np.broadcast_to(x, (H, n)).T*d2N_dudx + 2*dN_du
d3yt_dvdx2 = np.broadcast_to(x**2, (H, n)).T*d3N_dvdx2 + \
4*np.broadcast_to(x, (H, n)).T*d2N_dvdx + 2*dN_dv
G = self.Gf_v(x, yt, dyt_dx, d2yt_dx2)
dG_dyt = self.dG_dyf_v(x, yt, dyt_dx, d2yt_dx2)
dG_dytdx = self.dG_dydxf_v(x, yt, dyt_dx, d2yt_dx2)
dG_d2ytdx2 = self.dG_d2ydx2f_v(x, yt, dyt_dx, d2yt_dx2)
dG_dw = np.broadcast_to(dG_dyt, (H, n)).T*dyt_dw + \
np.broadcast_to(dG_dytdx, (H, n)).T*d2yt_dwdx + \
np.broadcast_to(dG_d2ytdx2, (H, n)).T*d3yt_dwdx2
dG_du = np.broadcast_to(dG_dyt, (H, n)).T*dyt_du + \
np.broadcast_to(dG_dytdx, (H, n)).T*d2yt_dudx + \
np.broadcast_to(dG_d2ytdx2, (H, n)).T*d3yt_dudx2
dG_dv = np.broadcast_to(dG_dyt, (H, n)).T*dyt_dv + \
np.broadcast_to(dG_dytdx, (H, n)).T*d2yt_dvdx + \
np.broadcast_to(dG_d2ytdx2, (H, n)).T*d3yt_dvdx2
dE_dw = 2*np.sum(np.broadcast_to(G, (H, n)).T*dG_dw, axis=0)
dE_du = 2*np.sum(np.broadcast_to(G, (H, n)).T*dG_du, axis=0)
dE_dv = 2*np.sum(np.broadcast_to(G, (H, n)).T*dG_dv, axis=0)
jac = np.hstack((dE_dw, dE_du, dE_dv))
return jac
# -----------------------------------------------------------------------------
if __name__ == '__main__':
# Create training data.
nx = 11
x_train = np.linspace(0, 1, nx)
# Test each training algorithm on each equation.
for ode in ('ode2_ivp_00',):
print('Examining %s.' % ode)
ode2ivp = ODE2IVP(ode)
print(ode2ivp)
# (Optional) analytical solution and derivative
if ode2ivp.yaf:
print('The analytical solution is:')
print(np.vectorize(ode2ivp.yaf)(x_train))
if ode2ivp.dya_dxf:
print('The analytical derivative is:')
print(np.vectorize(ode2ivp.dya_dxf)(x_train))
if ode2ivp.d2ya_dx2f:
print('The analytical 2nd derivative is:')
print(np.vectorize(ode2ivp.d2ya_dx2f)(x_train))
print()
# Create and train the networks.
for trainalg in ('delta', 'Nelder-Mead', 'Powell', 'CG', 'BFGS',
'Newton-CG', 'L-BFGS-B', 'TNC', 'SLSQP'):
print('Training using %s algorithm.' % trainalg)
net = NNODE2IVP(ode2ivp)
np.random.seed(0)
try:
net.train(x_train, trainalg=trainalg)
except (OverflowError, ValueError) as e:
print('Error using %s algorithm on %s!' % (trainalg, ode))
print(e)
print()
continue
print('The optimized network is:')
print(net)
yt = net.run(x_train)
dyt_dx = net.run_derivative(x_train)
d2yt_dx2 = net.run_2nd_derivative(x_train)
print('The trained solution is:')
print('yt =', yt)
print('The trained derivative is:')
print('dyt_dx =', dyt_dx)
print('The trained 2nd derivative is:')
print('d2yt_dx2 =', d2yt_dx2)
print()