-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathRegressionTool.py
More file actions
162 lines (125 loc) · 3.88 KB
/
RegressionTool.py
File metadata and controls
162 lines (125 loc) · 3.88 KB
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
import numpy as np
import matplotlib.pyplot as plt
import pickle
from race.race import *
from race.hashes import *
from race.optimization import *
from sklearn import linear_model
from sklearn.preprocessing import StandardScaler
import sys
import random
import pickle
import os
# for diabetes: 200 iters, eta = 0.5, sigma = 0.5, beta = 0.5, 8 components
# for naval: 500 iters, eta = 0.1, all same as above
# testfile = 'data/regression/diabetes/diabetes_validation.csv'
# datafile = 'data/regression/diabetes/diabetes.csv'
testfile = 'data/regression/naval/naval_validation.txt'
datafile = 'data/regression/naval/naval.txt'
reps = 8000
p = 4
sigma = 0.5
eta = 0.1
n_iters = 500
beta = 0.5
n_components = 8
use_prp = True
use_intercept = True
n_experiment_repetitions = 10
# epsilon = np.geomspace(0.001,1.0,10)
epsilon = [0.00001,0.00002,0.00005,0.0001,0.0002,0.0005,0.001,0.002,0.005,0.01,0.02,0.05,0.1,0.2,0.5,1,2,5,10]
def max_scale(x,y,xv,yv): # train features, train y, test features, test y
# scale based on the max abs components
sx = np.max(abs(x), axis=0) # scaling factor x : sx
sy = np.max(abs(y))
x /= sx
y /= sy
xv /= sx
yv /= sy
return (x,y,xv,yv)
def load(datafile,testfile):
name = os.path.splitext(os.path.basename(datafile))[0]
if name == "diabetes":
X = np.loadtxt(datafile,delimiter = ',')
Xv = np.loadtxt(testfile,delimiter = ',')
y = X[:,-1]
x = X[:,:-1]
xv = Xv[:,:-1] # x test (xv)
yv = Xv[:,-1] # y test (yv)
return max_scale(x,y,xv,yv)
if name == "naval":
X = np.loadtxt(datafile)
Xv = np.loadtxt(testfile)
x = X[:, :-2]
y = X[:,-1]
xv = Xv[:,:-2] # x test (xv)
yv = Xv[:,-1] # y test (yv)
return max_scale(x,y,xv,yv)
if name == "indoor":
X = np.loadtxt(datafile,delimiter = ',')
Xv = np.loadtxt(testfile,delimiter = ',')
x = X[:, :-9]
y = X[:, -8] # latitude and longitude in -9 and -8
xv = Xv[:, :-9]
yv = Xv[:, -8]
y_offset = np.min(y)
x_offset = np.min(x,axis = 0)
y -= y_offset
x -= x_offset
xv -= x_offset
yv -= y_offset
return max_scale(x,y,xv,yv)
x,y,x_test,y_test = load(datafile,testfile)
testset = format_dataset(x_test,y_test,intercept = use_intercept)
dataset = format_dataset(x,y,intercept = use_intercept)
N = testset.shape[0]
d = testset.shape[1]
# Construct LSH function
np.random.seed(42)
LSH = FastSRPMulti(reps,d,p)
# Construct RACE sketch
filename = os.path.splitext(datafile)[0]+'-RACE-'+str(p)+'-'+str(reps)+'.pickle'
if os.path.isfile(filename):
with open(filename, 'rb') as handle:
S = pickle.load(handle)
else:
S = RACE(reps,2**p)
S = construct_race_sketch(dataset, LSH, S, prp = use_prp, verbose = True)
with open(filename, 'wb') as handle:
pickle.dump(S, handle, protocol=pickle.HIGHEST_PROTOCOL)
def R0(theta):
return regression_loss(theta,testset)
# Sanity check baselines
baseline_model = optimal_linregress(x,y,intercept = use_intercept)
m = np.append(baseline_model,-1)
m = np.reshape(m,(d,1))
optimal_loss = regression_loss(m,testset) * 1.0/N
trivial_loss = np.sum(y_test**2)*1.0/N
mean_loss = np.sum( (y_test - np.mean(y))**2 )*1.0/N
print("Optimal Loss: ",optimal_loss)
print("Trivial Loss: ",trivial_loss)
print("Mean Loss: ",mean_loss)
# Sweep over privacy parameter
results = []
for ep in epsilon:
errs = np.zeros(n_experiment_repetitions)
for i in range(n_experiment_repetitions):
print("Epsilon = ",ep,end = ',')
sys.stdout.flush()
np.random.seed(i)
S.set_epsilon(ep)
theta, surrogate_losses, real_losses = accelerated_race_zgd(S, LSH, n_iters, eta, beta, sigma, n_components,
verbose = False, loss = None, dual = (not use_prp))
loss = regression_loss(theta,testset)*1.0/N
print(" Loss = ",loss)
sys.stdout.flush()
errs[i] = loss
errmean = np.mean(errs)
errstd = np.std(errs)
print("Epsilon = ",ep,"Mean = ",np.mean(errs),"STD = ",np.std(errs))
sys.stdout.flush()
results.append((errmean,errstd))
print('')
print('RESULTS:')
print('')
print(results)