-
Notifications
You must be signed in to change notification settings - Fork 9
/
test_more.py
executable file
·594 lines (529 loc) · 16.1 KB
/
test_more.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
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
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
#!/usr/bin/env python
from pycppad import *
def pycppad_test_forward_a2():
# start recording a_float operations
x = numpy.array( [ 2. , 3. ] )
a_x = independent(x)
# start recording a2float operations
u = numpy.array( [ a_x[0] , ad(4) ] )
a_u = independent(u)
# stop a2float recording and store operations if f
a_v = numpy.array( [ 2. * a_u[0] * a_u[1] ] )
f = adfun(a_u, a_v) # f(u0, u1) = 2. * u0 * u1
# evaluate the function f(u) using a_float operations
u = numpy.array([a_x[0] , 2.*a_x[1]]) # u0 = x0, u1 = 2 * x1
p = 0
fp = f.forward(p, u)
assert fp[0] == 2. * u[0] * u[1]
# evaluate partial of f with respect to the second component (using a_float)
p = 1
up = numpy.array( [ ad(0) , ad(1) ] )
fp = f.forward(p, up)
assert fp[0] == 2. * u[0] # f_u1(u0, u1) = 2. * u0
# stop a_float recording and store operations if g
a_y = 2. * fp
g = adfun(a_x, a_y) # g(x0, x1) = 2. * f_u1(x0, 2 * x1) = 4 * x0
# evaluate the function g(x) at x = (4, 5) using float operations
p = 0
x = numpy.array( [ 4. , 5. ] )
gp = g.forward(p, x)
assert gp[0] == 4. * x[0]
# evaluate the partial of g with respect to x0 (using float)
p = 1
xp = numpy.array( [ 1. , 0. ] )
gp = g.forward(p, xp)
assert gp[0] == 4.
# evaluate the partial of g with respect to x1 (using float)
p = 1
xp = numpy.array( [ 0. , 1. ] )
gp = g.forward(p, xp)
assert gp[0] == 0.
def pycppad_test_reverse_a2():
# start recording a_float operations
x = numpy.array( [ 2. , 3. ] )
a_x = independent(x)
# start recording a2float operations
u = numpy.array( [ a_x[0] , ad(4) ] )
a_u = independent(u)
# stop a2float recording and store operations if f
a_v = numpy.array( [ 2. * a_u[0] * a_u[1] ] )
f = adfun(a_u, a_v) # f(u0, u1) = 2. * u0 * u1
# evaluate the function f(u) using a_float operations
u = numpy.array([a_x[0] , 2.*a_x[1]]) # u0 = x0, u1 = 2 * x1
p = 0
fp = f.forward(p, u)
assert fp[0] == 2. * u[0] * u[1]
# derivative of f with respect to u (using a_float)
p = 1 # order of derivative
w = numpy.array( [ ad(1) ] ) # vector of weights
fp = f.reverse(p, w) # derivative of f w.r.t u
assert fp[0] == 2. * u[1] # f_u0(u0, u1) = 2. * u1
assert fp[1] == 2. * u[0] # f_u1(u0, u1) = 2. * u0
# stop a_float recording and store operations if g
a_y = 2. * fp # g(x0, x1) = 2 * f_u (x0, 2 * x1)
g = adfun(a_x, a_y) # = [ 8 * x1 , 4 * x0 ]
# evaluate the function g(x) at x = (4, 5) using float operations
p = 0
x = numpy.array( [ 4. , 5. ] )
gp = g.forward(p, x)
assert gp[0] == 8. * x[1]
assert gp[1] == 4. * x[0]
# derivative of the first component of g with respect to x (using float)
p = 1
w = numpy.array( [ 1. , 0. ] )
gp = g.reverse(p, w)
assert gp[0] == 0.
assert gp[1] == 8.
def pycppad_test_compile_with_debugging() :
# Cygwin systems have a problem catching exceptions that seems to be a
# bug in boost-python. We are working on getting this fixed.
import platform
uname = (( platform.uname() )[0] )[0:6]
if not (uname == 'CYGWIN') :
try :
x = numpy.array( [ 1 , 2 ] )
a_x = independent(x)
f = adfun(a_x, a_x)
x = numpy.array( [ 1 ] )
J = f.jacobian(x)
# The Line above should raise a CppAD exception because length of x not 2.
# Currently, CppAD exceptions are returned as ValueError exceptions, but
# it would be better to have a separate name for them.
raise RuntimeError
except ValueError :
# exception should come here
pass
def pycppad_test_compare_op():
delta = 10. * numpy.finfo(float).eps
x_array = numpy.array( range(5) )
y_array = 6. - x_array
for i in range( len(x_array) ) :
x = x_array[i]
y = y_array[i]
a_x = ad(x)
a_y = ad(y)
#
assert (a_x < a_y) == ( x < y )
assert (a_x > a_y) == ( x > y )
assert (a_x <= a_y) == ( x <= y )
assert (a_x >= a_y) == ( x >= y )
assert (a_x == a_y) == ( x == y )
assert (a_x != a_y) == ( x != y )
#
n = 3.
x = numpy.array( [ -2 , +2 ] )
a_x = independent(x)
positive = a_x >= 0
# At some level, each element of positive is being converted to a float
# before interfacing to pycppad * operator.
a_y = ( a_x ** n ) * positive
f = adfun(a_x, a_y)
J = f.jacobian(x)
for j in range( len(a_x) ) :
for i in range( len(a_y) ) :
if i == j and x[i] >= 0 :
assert abs( J[i][j] - n * x[j] ** (n-1) ) < delta
else :
assert J[i][j] == 0.
def pycppad_test_compare_op_a2():
delta = 10. * numpy.finfo(float).eps
x_array = numpy.array( range(5) )
y_array = 6. - x_array
for i in range( len(x_array) ) :
x = x_array[i]
y = y_array[i]
a2x = ad(ad(x))
a2y = ad(ad(y))
#
assert (a2x < a2y) == ( x < y)
assert (a2x > a2y) == ( x > y)
assert (a2x <= a2y) == ( x <= y)
assert (a2x >= a2y) == ( x >= y)
assert (a2x == a2y) == ( x == y)
assert (a2x != a2y) == ( x != y)
#
n = 3.
x = numpy.array( [ -2 , +2 ] )
a_x = numpy.array( [ ad(x[0]) , ad(x[1]) ] )
a2x = independent(x)
positive = a2x >= 0
# At some level, each element of positive is being converted to a float
# before interfacing to pycppad * operator.
a2y = ( a2x ** n ) * positive
f = adfun(a2x, a2y)
J = f.jacobian(x)
for j in range( len(a2x) ) :
for i in range( len(a2y) ) :
if i == j and x[i] >= 0 :
assert abs( J[i][j] - n * x[j] ** (n-1) ) < delta
else :
assert J[i][j] == 0.
def pycppad_test_ad():
x = ad(2.)
y = ad(3.)
z = ad(2.)
# assert that the conditionals work
assert x == x
assert x == z
assert x != y
assert x <= x
assert x <= z
assert x <= y
assert x < y
# assert that conditionals can fail to be true
assert not x == y
assert not x != z
assert not x != x
assert not x >= y
assert not x > y
x = ad(x)
y = ad(y)
z = ad(z)
# assert that the conditionals work
assert x == x
assert x == z
assert x != y
assert x <= x
assert x <= z
assert x <= y
assert x < y
# assert that conditionals can fail to be true
assert not x == y
assert not x != z
assert not x != x
assert not x >= y
assert not x > y
def pycppad_test_array_element_type_is_int():
A = numpy.array([
[ 1, 2, 3 ],
[ 4, 5, 6 ]
])
x = numpy.array( [ 0, 0, 0 ] )
a_x = independent(x)
a_y = numpy.dot(A, a_x)
f = adfun(a_x, a_y)
x = numpy.array( [ 1, 2, 3 ] )
J = f.jacobian(x)
assert numpy.all( A == J )
def pycppad_test_assign_op():
delta = 10. * numpy.finfo(float).eps
x_list = [ -2., -2., 0., 4., 4. ]
y_list = [ -2, 2, 2., .5, -.5 ]
for i in range( len(x_list) ) :
x = x_list[i]
y = y_list[i]
a_y = ad(y)
#
tmp = ad(x)
tmp += a_y
assert abs( tmp - (x + y) ) < delta
#
tmp = ad(x)
tmp -= y
assert abs( tmp - (x - y) ) < delta
#
tmp = x
tmp *= a_y
assert abs( tmp - x * y ) < delta
#
tmp = ad(x)
tmp /= a_y
assert abs( tmp - x / y ) < delta
#
#
x = numpy.array( [ -2 , +2 ] )
a_x = independent(x)
a_y = a_x + 2.
a_y *= 5.
f = adfun(a_x, a_y)
J = f.jacobian(x)
for j in range( len(a_x) ) :
for i in range( len(a_y) ) :
if i == j : assert abs( J[i][j] - 5. ) < delta
else : assert J[i][j] == 0.
def pycppad_test_assign_op_a2() :
delta = 10. * numpy.finfo(float).eps
x_list = [ -2., -2., 0., 4., 4. ]
y_list = [ -2, 2, 2., .5, -.5 ]
for i in range( len(x_list) ) :
x = x_list[i]
y = y_list[i]
a2y = ad(ad(y))
#
tmp = ad(ad(x))
tmp += a2y
assert abs( tmp - (x + y) ) < delta
#
tmp = ad(ad(x))
tmp -= y
assert abs( tmp - (x - y) ) < delta
#
tmp = x
tmp *= a2y
assert abs( tmp - x * y ) < delta
#
tmp = ad(ad(x))
tmp /= a2y
assert abs( tmp - x / y ) < delta
#
#
a_x = ad( numpy.array( [ 2 , 2 ] ) )
a2x = independent(a_x)
a2y = a2x + 2.
a2y *= 5.
f = adfun(a2x, a2y)
J = f.jacobian(a_x)
for j in range( len(a2x) ) :
for i in range( len(a2y) ) :
if i == j : assert abs( J[i][j] - 5. ) < delta
else : assert J[i][j] == 0.
def pycppad_test_numeric_op():
delta = 10. * numpy.finfo(float).eps
x_list = [ -2., -2., 0., 4., 4. ]
y_list = [ -2, 2, 2., .5, -.5 ]
for i in range( len(x_list) ) :
x = x_list[i]
y = y_list[i]
a_x = ad(x)
a_y = ad(y)
#
assert abs( a_x + a_y - (x + y) ) < delta
assert abs( a_x + y - (x + y) ) < delta
assert abs( x + a_y - (x + y) ) < delta
#
assert abs( a_x - a_y - (x - y) ) < delta
assert abs( a_x - y - (x - y) ) < delta
assert abs( x - a_y - (x - y) ) < delta
#
assert abs( a_x * a_y - x * y ) < delta
assert abs( a_x * y - x * y ) < delta
assert abs( x * a_y - x * y ) < delta
#
assert abs( a_x / a_y - x / y ) < delta
assert abs( a_x / y - x / y ) < delta
assert abs( x / a_y - x / y ) < delta
#
assert abs( a_x ** a_y - x ** y ) < delta
assert abs( a_x ** y - x ** y ) < delta
assert abs( x ** a_y - x ** y ) < delta
#
x = numpy.array( [ -2 , +2 ] )
a_x = independent(x)
n = 3
a_y = a_x ** n
f = adfun(a_x, a_y)
J = f.jacobian(x)
for j in range( len(a_x) ) :
for i in range( len(a_y) ) :
if i == j : assert abs( J[i][j] - n * x[j] ** (n-1) ) < delta
else : assert J[i][j] == 0.
def pycppad_test_numeric_op_a2():
delta = 10. * numpy.finfo(float).eps
x_list = [ -2., -2., 0., 4., 4. ]
y_list = [ -2, 2, 2., .5, -.5 ]
for i in range( len(x_list) ) :
x = x_list[i]
y = y_list[i]
a2x = ad( ad(x) )
a2y = ad( ad(y) )
#
assert abs( a2x + a2y - (x + y) ) < delta
assert abs( a2x + y - (x + y) ) < delta
assert abs( x + a2y - (x + y) ) < delta
#
assert abs( a2x - a2y - (x - y) ) < delta
assert abs( a2x - y - (x - y) ) < delta
assert abs( x - a2y - (x - y) ) < delta
#
assert abs( a2x * a2y - x * y ) < delta
assert abs( a2x * y - x * y ) < delta
assert abs( x * a2y - x * y ) < delta
#
assert abs( a2x / a2y - x / y ) < delta
assert abs( a2x / y - x / y ) < delta
assert abs( x / a2y - x / y ) < delta
#
assert abs( a2x ** a2y - x ** y ) < delta
assert abs( a2x ** y - x ** y ) < delta
assert abs( x ** a2y - x ** y ) < delta
#
a_x = ad( numpy.array( [ -2 , +2 ] ) )
a2x = independent(a_x)
n = 3.
a2y = a2x ** n
a_f = adfun(a2x, a2y)
J = a_f.jacobian(a_x)
for j in range( len(a2x) ) :
for i in range( len(a2y) ) :
if i == j : assert abs( J[i][j] - n * a_x[j] ** (n-1) ) < delta
else : J[i][j] == 0.
def pycppad_test_a_float_variable_info():
x = ad(2.)
y = ad(x)
assert x.__str__() == '2'
assert value(x) == 2.
# assert x.id == 1
# assert x.taddr == 0
def pycppad_test_ad_a_float_variable_info():
x = ad(ad(13.0))
assert x.__str__() == '13'
assert value(value(x)) == 13.
# assert x.id == 1
# assert x.taddr == 0
def pycppad_test_trigonometic_functions():
N = 5
x = numpy.array( [ 2.*n*numpy.pi/N for n in range(N) ] )
# cos
ax = independent(x)
ay = numpy.cos(ax)
af = adfun(ax, ay)
J = af.jacobian(x)
assert numpy.sum( abs( numpy.diag( numpy.sin(x)) + J)) == 0
# sin
ax = independent(x)
ay = numpy.sin(ax)
af = adfun(ax, ay)
J = af.jacobian(x)
assert numpy.prod( numpy.diag( numpy.cos(x)) == J)
def pycppad_test_pow():
x = numpy.array( [ 3, 2] )
ax = numpy.array( [ ad(x[0]), ad(x[1])] )
ay = numpy.array([ ax[0]**2, ax[1]**2, ax[0]**2., ax[1]**2., ax[0]**0.5, ax[1]**0.5, ax[0]**ad(2), ax[1]**ad(2)])
y = numpy.array([ x[0]**2, x[1]**2, x[0]**2., x[1]**2., x[0]**0.5, x[1]**0.5, x[0]**2, x[1]**2])
assert numpy.prod(ay == y)
def pycppad_test_multi_level_taping_and_higher_order_forward_derivatives():
ok = True
level = 1
x = numpy.array( [ 2 , 3 ] )
ad_x = independent(x)
# declare level two independent variable vector and start level two recording
level = 2
ad_ad_x = independent(ad_x)
# declare level 2 dependent variable vector and stop level 2 recording
ad_ad_y = numpy.array( [ 2. * ad_ad_x[0] * ad_ad_x[1] ] )
ad_f = adfun(ad_ad_x, ad_ad_y) # f(x0, x1) = 2. * x0 * x1
# evaluate the function f(x) using level one independent variable vector
p = 0
ad_fp = ad_f.forward(p, ad_x)
ok = ok and (ad_fp == 2. * ad_x[0] * ad_x[1])
# evaluate the partial of f with respect to the first component
p = 1
ad_xp = numpy.array( [ ad(1.) , ad(0.) ] )
ad_fp = ad_f.forward(p, ad_xp)
ok = ok and (ad_fp == 2. * ad_x[1])
# declare level 1 dependent variable vector and stop level 1 recording
ad_y = 2. * ad_fp
g = adfun(ad_x, ad_y) # g(x0, x1) = 2. * partial_x0 f(x0, x1) = 4 * x1
# evaluate the function g(x) at x = (4,5)
p = 0
x = numpy.array( [ 4. , 5. ] )
gp = g.forward(p, x)
ok = ok and (gp == 4. * x[1])
# evaluate the partial of g with respect to x0
p = 1
xp = numpy.array( [ 1. , 0. ] )
gp = g.forward(p, xp)
ok = ok and (gp == 0.)
# evaluate the partial of g with respect to x1
p = 1
xp = numpy.array( [ 0. , 1. ] )
gp = g.forward(p, xp)
ok = ok and (gp == 4.)
assert ok
def pycppad_test_multi_level_taping_and_higher_order_reverse_derivatives():
# domain space vector
x = numpy.array([0., 1.])
# declare independent variables and start recording
ax = independent(x);
ay = numpy.array([ax[0] * ax[0] * ax[1]])
# create f : X -> Y and stop recording
af = adfun (ax, ay);
# use first order reverse mode to evaluate derivative of y[0]
# and use the values in X for the independent variables.
w = numpy.zeros(1)
w[0] = 1.
y = af.forward(0, numpy.array([0.,1.]))
dw = af.reverse(1, w);
assert dw[0] == 2.*ax[0]*ax[1]
assert dw[1] == ax[0]*ax[0]
# use zero order forward mode to evaluate y at x = (3, 4)
# and use the template parameter Vector for the vector type
x = numpy.array([3.,4.])
y = af.forward(0,x)
assert y[0] == x[0]*x[0]*x[1]
# use first order reverse mode to evaluate derivative of y[0]
# and using the values in x for the independent variables.
w[0] = 1.
dw = af.reverse(1, w)
assert dw[0] == 2.*x[0]*x[1]
assert dw[1] == x[0]*x[0]
def pycppad_test_jacobian():
N = 4
A = numpy.array([n+1. for n in range(N*N)]).reshape((N,N))
def f(x):
return numpy.dot(A,x)
x = numpy.array([0. for n in range(N) ])
ax = independent(x)
ay = f(ax)
af = adfun (ax, ay);
x = numpy.array([1. for n in range(N)])
J = af.jacobian(x)
assert numpy.prod( A == J )
def pycppad_test_hessian():
N = 4
A = numpy.ones((N,N)) + 2.*numpy.eye(N)
def fun(x):
return numpy.array([0.5* numpy.dot(x,numpy.dot(A,x))])
x = numpy.array([0. for n in range(N) ])
a_x = independent(x)
a_y = fun(a_x)
f = adfun (a_x, a_y);
x = numpy.array([1. for n in range(N)])
w = numpy.array( [ 1. ] )
H = f.hessian(x, w)
assert numpy.prod( A == H )
def pycppad_test_mixed_element_types():
x = numpy.array( [ 1 , 2. ], dtype=object )
ok = False
try :
a_x = independent(x)
except NotImplementedError :
x[0] = 0.
a_x = independent(x)
ok = True
assert ok
#
a_y = numpy.array( [ ad(0) , 1. ], dtype=object )
ok = False
try :
f = adfun(a_x, a_y)
except NotImplementedError :
a_y[1] = ad(1)
f = adfun(a_x, a_y)
ok = True
assert ok
#
import sys
if __name__ == "__main__" :
number_ok = 0
number_fail = 0
list_of_globals = sorted( globals().copy() )
for g in list_of_globals :
if g[:13] == "pycppad_test_" :
ok = True
try :
eval("%s()" % g)
except AssertionError :
ok = False
if ok :
print "OK: %s" % g[13:]
number_ok = number_ok + 1
else :
print "Error: %s" % g[13:]
number_fail = number_fail + 1
if number_fail == 0 :
print "All %d tests passed" % number_ok
sys.exit(0)
else :
print "%d tests failed" % number_fail
sys.exit(1)