-
Notifications
You must be signed in to change notification settings - Fork 2
/
synthetic_exp.py
144 lines (117 loc) · 4.38 KB
/
synthetic_exp.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
#! /usr/bin/env python
# -*- coding: utf-8 -*-
#
# Distributed under terms of the MIT license.
# Copyright 2021 Chao Pan.
import numpy as np
import matplotlib.pyplot as plt
from algos import *
import argparse
import os
import time
import multiprocessing as mp
from multiprocessing import Pool
from tqdm import *
import warnings
import functools
warnings.filterwarnings("ignore")
"""
This is the code for our experiments on sythetic data.
Note that you can comment out the methods that you don't want to test in the Fun2parallel.
This can speed up the experiment.
"""
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--N', type=int, default=100000)
parser.add_argument('--d', type=int, default=2)
parser.add_argument('--gamma', type=float, default=0.01)
parser.add_argument('--R', type=float, default=0.95)
parser.add_argument('--a', type=float, default=0.)
parser.add_argument('--thread', type=int, default=20)
parser.add_argument('--chunksize', type=int, default=1)
parser.add_argument('--Repeat', type=int, default=20)
parser.add_argument('--savepath', type=str)
args = parser.parse_args()
if not os.path.exists(args.savepath):
os.makedirs(args.savepath)
N = args.N
d = args.d
R = args.R
gamma = args.gamma
a = args.a
def Fun2parallel(Dummy=None,N=1000,gamma=0.1,d=2,p=None,w=None,R=0.95,a=0):
# Generate random seed!!!!
pid = mp.current_process()._identity[0]
np.random.seed(pid)
Record = np.zeros((3,5))
# for Rp in tqdm(range(Repeat)):
Flag = False
Gen_count = 0
p_input = p
w_input = w
while not Flag:
Gen_count += 1
if Gen_count > 100:
return Record
N,X,y,w,p = Poincare_Uniform_Data(N,d,gamma,R = R, p = p_input,w=w_input)
if (len(y[y==1])>10) and (len(y[y==-1])>10):
Flag = True
y.astype('int32')
# # Enable this part is you want to use estimated p.
# # However, you can't use Graham scan for the case d>2. You have to use other heuristics.
# # Time Graham scan first
# start = time.time()
# CH1 = ConvexHull(X[:,y==1])
# CH2 = ConvexHull(X[:,y==-1])
# MDpair = minDpair(CH1,CH2)
# p_hat = Weightedmidpt(MDpair[:,0],MDpair[:,1],0.5)
# tGraham = time.time()-start
# Time P2L
start = time.time()
DUMMY = P2L(X)
tP2L = time.time()-start
# our_perceptron part
start = time.time()
w1,Record[2,0] = HP(X,y,p,gamma,R,a=None,option='first')
Record[1,0] = time.time()-start#+tGraham
Record[0,0],_ = Eval(X=X,y=y,p=p,w1 = w1)
# # our_SOP
start = time.time()
xi, C,Record[2,1] = HP(X,y,p,gamma,R,a=a,option='second')
Record[1,1] = time.time()-start#+tGraham
Record[0,1] = Eval(X=X,y=y,p=p,xi=xi, C=C)
# Translate y from {-1,1} to {0,1}
y_SVM = np.round((y+1)/2).astype('int32')
# our_SVM
Acc, Record[1,2], w_Psvm = tangent_hsvm(X.T, y_SVM, X.T, y_SVM, 1000,p=p)
Record[0,2] = Acc*100
# cho_SVM
Acc, ChoTime = cho_hsvm(X.T, y_SVM, X.T, y_SVM, 10)
Record[0,3] = Acc*100
Record[1,3] = ChoTime - tP2L
# Euclidean_SVM
Acc, Record[1,4], w_eu = euclidean_svm(X.T, y_SVM, X.T, y_SVM, 1000)
Record[0,4] = Acc*100
return Record
mypool = Pool(args.thread)
chunksize = args.chunksize
Repeat = args.Repeat
# First axis: Acc, Mistakes,time
# Second axis: method [our_perceptron,our_SOP, our_SVM, cho_SVM, Euclidean_SVM]
Record_meta = np.zeros((3,Repeat))
# pbar = tqdm(total=Repeat)
for alpha in [1,2,3]:
Rp = alpha*R/5
Record_meta = np.zeros((3,5,Repeat))
# pbar = tqdm(total=Repeat)
# theta = np.random.random()
p = np.random.normal(size=d)
p = p/np.linalg.norm(p)*Rp
# w = p/Rp
fn = functools.partial(Fun2parallel,N = N,gamma = gamma,d=d,p=p,w=None,R=R,a=a)
print('N,gamma,alpha,d:'+str(N)+', '+str(gamma)+', '+str(alpha)+', '+str(d)+' Start!')
for ind, res in enumerate(mypool.imap(fn, range(Repeat)), chunksize):
Record_meta[:,:,ind-1] = res
# pbar.update()
np.save(args.savepath+'/Result_N_'+str(N)+'_margin_'+str(gamma)+'_alpha_'+str(alpha)+'_d_'+str(d),Record_meta)
print('N,gamma,alpha,d:'+str(N)+', '+str(gamma)+', '+str(alpha)+', '+str(d)+' Done!')