-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnorms.py
277 lines (217 loc) · 7.2 KB
/
norms.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
# functions to compute norms and projections
from __future__ import division
from __future__ import unicode_literals
from __future__ import print_function
from __future__ import absolute_import
from builtins import * # NOQA
from future import standard_library
standard_library.install_aliases() # NOQA
import numpy as np
import math
from numpy import linalg as LA
import cupy as cp
from cupy import linalg as LA_GPU
import chainerrl
from chainerrl import explorers
from logging import getLogger
def reducebudget_l1(used, budget):
new_budget = max(0, budget - used)
return new_budget
def reducebudget_l2(used, budget):
new_budget = math.sqrt(budget**2 - used**2)
new_budget = max(0, new_budget)
# print('used:',used)
# print('remainder',new_budget)
return new_budget
def random_delta(n_dim, budget):
real_budget = np.random.uniform(low=0, high=budget, size=1)
x = np.random.randint(low=-100, high=100, size=n_dim)
t = sum(np.abs(item) for item in x)
x = [item / t for item in x]
x = [item * real_budget for item in x]
x = [item[0] for item in x]
return x
def l1_time_project2(y_orig, budget):
y_abs = list(map(abs, y_orig))
u = sorted(y_abs, reverse=True)
binK = 1
K = 1
bin_list = [0] * len(u)
for i in range(1, len(u) + 1):
if (sum([u[r] for r in range(i)]) - budget) / i < u[i - 1]:
bin_list[i - 1] = binK
binK += 1
if sum(bin_list) > 0:
K = np.argmax(bin_list) + 1
tau = (sum([u[i] for i in range(K)]) - budget) / K
xn = [max(item - tau, 0) for item in y_abs]
l1_norm_y = np.linalg.norm(y_orig, 1)
for i in range(len(y_orig)):
if l1_norm_y > budget:
y_orig[i] = np.sign(y_orig[i]) * xn[i]
return y_orig
def l1_spatial_project2(y_orig, budget):
y_abs = list(map(abs, y_orig))
u = sorted(y_abs, reverse=True)
binK = 1
K = 1
bin_list = [0] * len(u)
for i in range(1, len(u) + 1):
if (sum([u[r] for r in range(i)]) - budget) / i < u[i - 1]:
bin_list[i - 1] = binK
binK += 1
if sum(bin_list) > 0:
K = np.argmax(bin_list) + 1
tau = (sum([u[i] for i in range(K)]) - budget) / K
xn = [max(item - tau, 0) for item in y_abs]
l1_norm_y = np.linalg.norm(y_orig, 1)
for i in range(len(y_orig)):
if l1_norm_y > budget:
y_orig[i] = np.sign(y_orig[i]) * xn[i]
return y_orig
def l1_time_project(y_orig, delta):
y = list(map(abs, y_orig))
v = [y[0]]
vb = []
rho = y[0] - delta
for i in range(2, len(y)):
if y[i] > rho:
rho = rho + (y[i] - rho) / (np.linalg.norm(v, 1) + 1)
if rho > y[i] - delta:
v.append(y[i])
else:
vb = vb + v
v = [y[i]]
rho = y[i] - delta
if len(vb) > 0:
for item in vb:
if item > rho:
v.append(item)
rho = rho + (item - rho) / np.linalg.norm(v, 1)
v_proj = 0
while not v_proj == np.linalg.norm(v, 1):
v_proj = np.linalg.norm(v, 1)
for i, item in enumerate(v):
if item <= rho:
y_loc = v.pop(i)
rho = rho + (rho - y_loc) / np.linalg.norm(v, 1)
tau = rho
xn = [max(item - tau, 0) for item in y]
l1_norm_y = np.linalg.norm(y_orig, 1)
for i in range(len(y_orig)):
if l1_norm_y > delta:
y_orig[i] = np.sign(y_orig[i]) * xn[i]
return y_orig
def l2_spatial_norm(x):
return LA.norm(x, 2)
def l1_spatial_norm(x):
return LA.norm(x, 1)
def l2_spatial_norm_gpu(x):
return LA_GPU.norm(x, 2)
def l2_time_norm(x):
return LA.norm(x, 2)
def linf_spatial_norm(x):
return LA.norm(x, np.inf)
def linf_time_norm(x):
return LA.norm(x, np.inf)
def l2_spatial_project(x, distance):
norm = l2_spatial_norm(x)
# print('x',x)
# print('l2 norm', diff)
# print('dist',distance)
if norm <= distance:
delta = x
else:
delta = (x / norm) * distance
return delta
def l2_spatial_project_gpu(x, distance):
norm = l2_spatial_norm_gpu(x)
# print('x',x)
# print('l2 norm', diff)
if norm <= distance:
delta = x
else:
delta = (x / norm) * distance
return delta
def l2_time_project(x, distance):
norm = l2_time_norm(x)
# print('x',x)
# print('l2 norm', diff)
# print('dist',distance)
if norm <= distance:
delta = x
else:
delta = (x / norm) * distance
return delta
def linf_spatial_project(x, distance):
# print('x', x)
norm = linf_spatial_norm(x)
# print('norm', norm)
if norm <= distance:
x_hat = x
else:
x_hat = np.clip(x, -distance, distance)
# print('xhat',x_hat)
return x_hat
def linf_time_project(x, distance):
# print('x', x)
norm = linf_time_norm(x)
if norm <= distance:
x_hat = x
else:
x_hat = np.clip(x, -distance, distance)
# print('xhat',x_hat)
return x_hat
class DecayAdditiveOU(explorers.AdditiveOU):
"""Additive Ornstein-Uhlenbeck process.
Used in https://arxiv.org/abs/1509.02971 for exploration.
Args:
mu (float): Mean of the OU process
theta (float): Friction to pull towards the mean
sigma (float or ndarray): Scale of noise
start_with_mu (bool): Start the process without noise
"""
def __init__(self, mu=0.0, theta=0.15, sigma=0.3, start_with_mu=False,
end_sigma=0.2, decay_steps=1e3, logger=getLogger(__name__)):
self.mu = mu
self.theta = theta
self.sigma = sigma
self.start_with_mu = start_with_mu
self.logger = logger
self.ou_state = None
self.steps = 0
self.decay_steps = decay_steps
self.delta = (self.sigma - end_sigma) / decay_steps
def decay_sigma(self):
self.sigma = self.sigma - self.delta
return self.sigma
def select_action(self, t, greedy_action_func, action_value=None):
if self.steps < self.decay_steps:
self.sigma = self.decay_sigma()
self.steps += 1
a = greedy_action_func()
if self.ou_state is None:
if self.start_with_mu:
self.ou_state = np.full(a.shape, self.mu, dtype=np.float32)
else:
sigma_stable = (self.sigma
/ np.sqrt(2 * self.theta - self.theta ** 2))
self.ou_state = np.random.normal(
size=a.shape, loc=self.mu, scale=sigma_stable).astype(np.float32)
else:
self.evolve()
noise = self.ou_state
self.logger.debug('t:%s noise:%s', t, noise)
return a + noise
if __name__ == '__main__':
x = [1, 2, 3, 4, 5, -5, 0]
x = random_delta(2, 1)
# print(l1_norm(x))
# print(l2_norm(x))
# print(linf_norm(x))
# x_hat = l1_project(x, 5)
# print(x_hat)
# x_hat = l2_project(x, 5)
# print(x_hat)
# x_hat = linf_project(x, 6)
# print(x_hat)