forked from HIPS/autograd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bench_util.py
45 lines (37 loc) · 1.23 KB
/
bench_util.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
import autograd.numpy.random as npr
import autograd.numpy as np
from autograd import grad
try:
from autograd.misc.flatten import flatten
except ImportError:
from autograd.util import flatten
def time_flatten():
val = {'k': npr.random((4, 4)),
'k2': npr.random((3, 3)),
'k3': 3.0,
'k4': [1.0, 4.0, 7.0, 9.0],
'k5': np.array([4., 5., 6.]),
'k6': np.array([[7., 8.], [9., 10.]])}
vect, unflatten = flatten(val)
val_recovered = unflatten(vect)
vect_2, _ = flatten(val_recovered)
# def time_vspace_flatten():
# val = {'k': npr.random((4, 4)),
# 'k2': npr.random((3, 3)),
# 'k3': 3.0,
# 'k4': [1.0, 4.0, 7.0, 9.0],
# 'k5': np.array([4., 5., 6.]),
# 'k6': np.array([[7., 8.], [9., 10.]])}
# vspace_flatten(val)
def time_grad_flatten():
val = {'k': npr.random((4, 4)),
'k2': npr.random((3, 3)),
'k3': 3.0,
'k4': [1.0, 4.0, 7.0, 9.0],
'k5': np.array([4., 5., 6.]),
'k6': np.array([[7., 8.], [9., 10.]])}
vect, unflatten = flatten(val)
def fun(vec):
v = unflatten(vec)
return np.sum(v['k5']) + np.sum(v['k6'])
grad(fun)(vect)