-
Notifications
You must be signed in to change notification settings - Fork 1
/
advtrain.py
39 lines (33 loc) · 1.01 KB
/
advtrain.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
import cvxpy as cp
import numpy as np
def compute_q(p):
if p != np.Inf and p > 1:
q = p / (p - 1)
elif p == 1:
q = np.Inf
else:
q = 1
return q
class AdversarialTraining:
def __init__(self, X, y, p):
m, n = X.shape
q = compute_q(p)
# Formulate problem
param = cp.Variable(n)
param_norm = cp.pnorm(param, p=q)
adv_radius = cp.Parameter(name='adv_radius', nonneg=True)
abs_error = cp.abs(X @ param - y)
adv_loss = 1 / m * cp.sum((abs_error + adv_radius * param_norm) ** 2)
prob = cp.Problem(cp.Minimize(adv_loss))
self.prob = prob
self.adv_radius = adv_radius
self.param = param
self.warm_start = False
def __call__(self, adv_radius, **kwargs):
try:
self.adv_radius.value = adv_radius
self.prob.solve(warm_start=self.warm_start, **kwargs)
v = self.param.value
except:
v = np.zeros(self.param.shape)
return v