-
Notifications
You must be signed in to change notification settings - Fork 1
/
derivativetest.py
148 lines (134 loc) · 5.15 KB
/
derivativetest.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
import matplotlib.pyplot as plt
import torch
from torch.nn.utils import parameters_to_vector, vector_to_parameters
def derivativetest(obj, x0):
"""
Test the gradient and Hessian of a function. A large proportion
parallel in the middle of both plots means accuraccy.
INPUTS:
fun: a function handle that gives f, g, Hv
x0: starting point
OUTPUTS:
derivative test plots
"""
device = x0.device
dt = x0.dtype
fk, grad, Hvp = obj(x0)
dx = torch.ones(len(x0), device = device, dtype=dt)
M = 20;
dxs = torch.zeros(M,1, device = device, dtype=dt)
firsterror = torch.zeros(M, device = device, dtype=dt)
order1 = torch.zeros(M-1, device = device, dtype=dt)
seconderror = torch.zeros(M, device = device, dtype=dt)
order2 = torch.zeros(M-1, device = device, dtype=dt)
for i in range(M):
# with torch.no_grad():
# for param in model.parameters():
# param += learning_rate * param.grad
# x = parameters_to_vector(model.parameters())
# vector_to_parameters(x+dx, model.parameters())
x = x0 + dx
f2 = obj(x, 'f')
H0 = Ax(Hvp, dx)
# H0 = parameters_to_vector(Hv)
firsterror[i] = abs(f2 - (fk + torch.dot(
dx, grad)))/abs(fk)
seconderror[i] = abs(f2 - (fk + torch.dot(
dx, grad) + 0.5* torch.dot(dx, H0)))/abs(fk)
print('First Order Error is %8.2e; Second Order Error is %8.2e'% (
firsterror[i], seconderror[i]))
if i > 0:
order1[i-1] = torch.log2(firsterror[i-1]/firsterror[i])
order2[i-1] = torch.log2(seconderror[i-1]/seconderror[i])
dxs[i] = torch.norm(dx)
dx = dx/2
step = [2**(-i-1) for i in range(M)]
plt.figure(figsize=(12,8))
plt.subplot(221)
plt.loglog(step, abs(firsterror.cpu().detach().numpy()),'b', label = '1st Order Err')
plt.loglog(step, (dxs.cpu().detach().numpy())**2,'r', label = 'order')
plt.gca().invert_xaxis()
plt.legend()
plt.subplot(222)
plt.semilogx(step[1:], order1.cpu().detach().numpy(),'b', label = '1st Order')
plt.gca().invert_xaxis()
plt.legend()
plt.subplot(223)
plt.loglog(step, abs(seconderror.cpu().detach().numpy()),'b', label = '2nd Order Err')
plt.loglog(step, (dxs.cpu().detach().numpy())**3,'r', label = 'Order')
plt.gca().invert_xaxis()
plt.legend()
plt.subplot(224)
plt.semilogx(step[1:], order2.cpu().detach().numpy(),'b', label = '2nd Order')
plt.gca().invert_xaxis()
plt.legend()
return plt.show()
def derivativetest_class(obj, x0):
"""
Test the gradient and Hessian of a function. A large proportion
parallel in the middle of both plots means accuraccy.
INPUTS:
fun: a function handle that gives f, g, Hv
x0: starting point
OUTPUTS:
derivative test plots
"""
device = x0.device
dt = x0.dtype
fk = obj(x0).f()
grad = obj(x0).g()
dx = torch.ones(len(x0), device = device, dtype=dt)
M = 20;
dxs = torch.zeros(M,1, device = device, dtype=dt)
firsterror = torch.zeros(M, device = device, dtype=dt)
order1 = torch.zeros(M-1, device = device, dtype=dt)
seconderror = torch.zeros(M, device = device, dtype=dt)
order2 = torch.zeros(M-1, device = device, dtype=dt)
for i in range(M):
# with torch.no_grad():
# for param in model.parameters():
# param += learning_rate * param.grad
# x = parameters_to_vector(model.parameters())
# vector_to_parameters(x+dx, model.parameters())
x = x0 + dx
f2 = obj(x).f()
H0 = obj(x0).Hv(dx)
# H0 = parameters_to_vector(Hv)
firsterror[i] = abs(f2 - (fk + torch.dot(
dx, grad)))/abs(fk)
seconderror[i] = abs(f2 - (fk + torch.dot(
dx, grad) + 0.5* torch.dot(dx, H0)))/abs(fk)
print('First Order Error is %8.2e; Second Order Error is %8.2e'% (
firsterror[i], seconderror[i]))
if i > 0:
order1[i-1] = torch.log2(firsterror[i-1]/firsterror[i])
order2[i-1] = torch.log2(seconderror[i-1]/seconderror[i])
dxs[i] = torch.norm(dx)
dx = dx/2
step = [2**(-i-1) for i in range(M)]
plt.figure(figsize=(12,8))
plt.subplot(221)
plt.loglog(step, abs(firsterror.cpu().detach().numpy()),'b', label = '1st Order Err')
plt.loglog(step, (dxs.cpu().detach().numpy())**2,'r', label = 'order')
plt.gca().invert_xaxis()
plt.legend()
plt.subplot(222)
plt.semilogx(step[1:], order1.cpu().detach().numpy(),'b', label = '1st Order')
plt.gca().invert_xaxis()
plt.legend()
plt.subplot(223)
plt.loglog(step, abs(seconderror.cpu().detach().numpy()),'b', label = '2nd Order Err')
plt.loglog(step, (dxs.cpu().detach().numpy())**3,'r', label = 'Order')
plt.gca().invert_xaxis()
plt.legend()
plt.subplot(224)
plt.semilogx(step[1:], order2.cpu().detach().numpy(),'b', label = '2nd Order')
plt.gca().invert_xaxis()
plt.legend()
return plt.show()
def Ax(A, x):
if callable(A):
Ax = A(x)
else:
Ax =torch.mv(A, x)
return Ax