-
Notifications
You must be signed in to change notification settings - Fork 0
/
optimizer.py
65 lines (45 loc) · 1.53 KB
/
optimizer.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
import numpy as np
from numpy import linalg
import generator as gen
#run experiment over sequence of loss function objects
def gradient_descent(loss_seq, projection, alpha):
tracking_error = []
regret = []
x_t = np.ones(loss_seq[0].d)
for loss in loss_seq:
tracking_error.append(loss.tracking_error(x_t))
regret.append(loss.regret(x_t))
x_t = x_t - alpha * loss.gradient(x_t)
x_t = projection(x_t)
return tracking_error, regret
def bandit_descent(loss_seq, projection, alpha, delta, xi):
tracking_error = []
regret = []
x_t = np.ones(loss_seq[0].d) # init play
for loss in loss_seq:
tracking_error.append(loss.tracking_error(x_t))
gradient, d1_loss = d1_point_gradient_loss(x_t, delta, loss)
regret.append(d1_loss - loss.evaluate(loss.xstar))
x_t = x_t - alpha * gradient
x_t = projection(x_t, xi) # project onto set scaled by xi
return tracking_error, regret
def d1_point_gradient_loss(x_t, delta, loss):
d = loss.d
g = np.zeros(d)
reg_query = 0
lxt = loss.evaluate(x_t)
for i in range(d):
e_i = np.zeros(d)
e_i[i] = 1.0
point_loss = loss.evaluate(x_t + delta * e_i)
reg_query += point_loss
g += (point_loss - lxt) * e_i
return 1 / delta * g, 1 / (d+1) * (reg_query + lxt)
def k_point_gradient(x_t, delta, d, loss):
pass
def project_ball(x, radius=1):
norm = linalg.norm(x)
if norm > radius:
return x / norm * radius
else:
return x