-
Notifications
You must be signed in to change notification settings - Fork 1
/
figure_da.py
182 lines (136 loc) · 5.54 KB
/
figure_da.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
#!/usr/bin/env python
# coding: utf-8
__author__ = 'Alain Rakotomamonjy'
import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns
color_pal_t = sns.color_palette("colorblind", 11).as_hex()
color_pal = color_pal_t.copy()
colors = ["black", "salmon pink", "neon pink", "cornflower","cobalt blue"
,"blue green", "aquamarine", "dark yellow", "golden yellow", "reddish pink" , "reddish purple"]
color_pal = sns.xkcd_palette(colors)
plt.close("all")
pathres = './result/da/'
data = 'mnist'
if data == 'toy':
n_vec = [200, 500, 1000, 2000, 000, 5000]
nb_p_vec = 7
p = 2
regcl = 100
else:
data = 'mnist'
n_vec = [200, 500, 1000, 2000, 3000, 4000]
nb_p_vec = 7
p = 20
regcl = 10
method_vec = ['wda', 'swda']
legend_vec_accur = ['Sinkhorn', 'dec=1.5', 'dec=2', 'dec=5', 'dec=10', 'dec=20', 'dec=50', 'dec=100', 'No Adapt']
legend_vec_time = ['dec=1.5', 'dec=2', 'dec=5', 'dec=10', 'dec=20', 'dec=50', 'dec=100']
t = 1
in_sample = 0
Mres = np.zeros((len(n_vec), nb_p_vec+2))
Sres = np.zeros((len(n_vec), nb_p_vec+2))
Mtime = np.zeros((len(n_vec), nb_p_vec+1))
Stime = np.zeros((len(n_vec), nb_p_vec+1))
Mgain = np.zeros((len(n_vec), nb_p_vec+1))
Sgain = np.zeros((len(n_vec), nb_p_vec+1))
for i_k, n in enumerate(n_vec):
if data == 'toy':
filename = 'da_{:}_n{:d}_regcl{:2.2f}'.format(data, n, regcl)
else:
filename = 'da_{:}_n{:d}_regcl{:d}'.format(data, n, regcl)
# reading files and performance
res = np.load(pathres + filename + '.npz')
aux = res['bc_sink']
nb_computed = np.where(aux)[0].shape[0]
print(nb_computed)
print(' \t\t {:2.2f} \t\t{:d}'.format( aux[np.where(aux)].sum() / nb_computed*100, nb_computed))
Mres[i_k,0] = aux[np.where(aux)].mean()
Sres[i_k,0] = aux[np.where(aux)].std()
aux = res['bc_screen']
nb_computed = np.where(np.sum(aux,axis=1))[0].shape[0]
print(nb_computed)
mean_perf = aux[np.where(np.sum(aux,axis=1))[0]].mean(axis=0)
std_perf = aux[np.where(np.sum(aux,axis=1))[0]].std(axis=0)
Mres[i_k,1:nb_p_vec+1] = mean_perf # stocking average perf for all decimation
Sres[i_k,1:nb_p_vec+1] = std_perf
aux = res['bc_none']
nb_computed = np.where(aux)[0].shape[0]
print(nb_computed)
print(' \t\t {:2.2f} \t\t{:d}'.format(aux[np.where(aux)].sum() / nb_computed * 100, nb_computed))
Mres[i_k,nb_p_vec+1] = aux[np.where(aux)].mean()
Sres[i_k,nb_p_vec+1] = aux[np.where(aux)].std()
# reading computation time
aux = res['time_sink']
nb_computed = np.where(aux)[0].shape[0]
print(nb_computed)
print(' \t\t {:2.2f} \t\t{:d}'.format(aux[np.where(aux)].sum() / nb_computed*100, nb_computed))
Mtime[i_k,0] = aux[np.where(aux)].mean()
Stime[i_k,0] = aux[np.where(aux)].std()
# computing gain
aux2 = res['time_screen']
aux1 = aux.reshape(-1,1)/res['time_screen']
nb_computed = np.where(np.isnan(np.sum(aux1,axis=1)) == False)[0].shape[0]
print(nb_computed)
mean_perf = aux1[ np.where(np.isnan(np.sum(aux1,axis=1)) == False)[0]].mean(axis=0)
std_perf = aux1[np.where(np.isnan(np.sum(aux1,axis=1)) == False)[0]].std(axis=0)
# keeping gain for all p
Mtime[i_k,1:] = aux2[np.where(np.sum(aux2,axis=1))].mean(axis=0)
Stime[i_k,1:] =aux2[np.where(np.sum(aux2,axis=1))].std(axis=0)
Mgain[i_k,1:] = mean_perf
Sgain[i_k,1:] = std_perf
#%% figure for accuracy
plt.figure(0)
ax = []
markert = ['o','p','s','d','h','o','p','<','>','8','P']
colort = color_pal
for i in range(nb_p_vec+2):
ax1, = plt.plot(n_vec, Mres[:,i],label=str(i), lw = 2, marker = markert[i], markersize=10, c=colort[i])
#error=Sres[:,i]
#plt.fill_between(n_vec, Mres[:,i]-error, Mres[:,i]+error, color=colort[i],alpha = 0.1)
ax.append(ax1)
if t==1:
plt.ylim((0.4,1))
else:
plt.ylim((0.4,1))
plt.xlabel('Number of samples', fontsize = 16)
plt.ylabel('Accuracy', fontsize = 16)
plt.xticks(fontsize=12)
plt.yticks(fontsize=12)
plt.legend(legend_vec_accur)
plt.title('OTDA+Knn on {:}'.format(data))
plt.grid(color='k', linestyle=':', linewidth=1,alpha=0.5)
filename = 'da_accur_{:}_regcl{:d}.pdf'.format(data,regcl)
plt.savefig('figure/' + filename,dpi=600)
# %% figure for gain
plt.figure(1)
for i in range(1,nb_p_vec+1):
ax1, = plt.plot(n_vec, Mgain[:,i], label=str(i), lw=2, marker=markert[i], markersize=12, c=colort[i])
#error=Stime[:,i]
#plt.fill_between(n_vec, Mtime[:,i]-error, Mtime[:,i]+error, color=colort[i],alpha = 0.1)
ax.append(ax1)
plt.xlabel('Number of samples', fontsize=16)
plt.ylabel('Running Time Gain', fontsize=16)
plt.xticks(fontsize=14)
plt.yticks(fontsize=14)
plt.legend(legend_vec_time)
plt.title('Screened OTDA on {:}'.format(data))
plt.grid(color='k', linestyle=':', linewidth=1,alpha=0.5)
filename = 'da_gain_{:}_regcl{:d}.pdf'.format(data,regcl)
plt.savefig('figure/' + filename,dpi=600)
# %% figure for timing
plt.figure(2)
for i in range(0,nb_p_vec+1):
ax1, = plt.semilogy(n_vec, Mtime[:,i],label=str(i), lw=2, marker = markert[i], markersize=12, c=colort[i])
#error=Stime[:,i]
#plt.fill_between(n_vec, Mtime[:,i]-error, Mtime[:,i]+error, color=colort[i],alpha = 0.1)
ax.append(ax1)
plt.xlabel('Number of samples', fontsize=16)
plt.ylabel('Running Time (s)', fontsize=16)
plt.xticks(fontsize=14)
plt.yticks(fontsize=14)
plt.legend(legend_vec_accur)
plt.title('Screened OTDA on {:}'.format(data))
plt.grid(color='k', linestyle=':', linewidth=1, alpha=0.5)
filename = 'da_time_{:}_regcl{:d}.pdf'.format(data, regcl)
plt.savefig('figure/' + filename, dpi=600)