forked from shiyongde/Minimax_Curriculum_Learning
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plot_cLearn.py
346 lines (296 loc) · 14.8 KB
/
plot_cLearn.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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
from __future__ import print_function
from __future__ import division
import sys
import os
import time
import string
import random
import numpy as np
import matplotlib.pyplot as plt
def load_news20_result(dataset, datadir='/home/tianyizhou/Dropbox/sensor_room/cLearn/result'):
news20_random = ()
news20_spl = ()
news20_spl_info = ()
news20_spld = ()
news20_spld_info = ()
news20_cLearn = ()
news20_cLearn_info = ()
news20_cLearnk = ()
news20_cLearnk_info = ()
news20_maxmax = ()
news20_minmin = ()
for file in os.listdir(datadir):
if file.startswith(dataset+'random') and file.endswith('.txt'):
news20_random = news20_random + (np.loadtxt(os.path.join(datadir, file)),)
elif file.startswith(dataset+'spl_') and file.endswith('.txt'):
news20_spl = news20_spl + (np.loadtxt(os.path.join(datadir, file)),)
info = file.replace(dataset+'spl_','').replace('_result.txt','').split('_')
news20_spl_info = news20_spl_info + ([int(info[0]), float(info[1])],)
elif file.startswith(dataset+'spld_') and file.endswith('.txt'):
news20_spld = news20_spld + (np.loadtxt(os.path.join(datadir, file)),)
info = file.replace(dataset+'spld_','').replace('_result.txt','').split('_')
news20_spld_info = news20_spld_info + ([int(info[0]), float(info[1])],)
elif file.startswith(dataset+'cLearn_k') and file.endswith('.txt'):
news20_cLearnk = news20_cLearnk + (np.loadtxt(os.path.join(datadir, file)),)
info = file.replace(dataset+'cLearn_k+','').replace('_result.txt','')
news20_cLearnk_info = news20_cLearnk_info + (int(info),)
elif file.startswith(dataset+'cLearn_maxmax') and file.endswith('.txt'):
news20_maxmax = news20_maxmax + (np.loadtxt(os.path.join(datadir, file)),)
elif file.startswith(dataset+'cLearn_minmin') and file.endswith('.txt'):
news20_minmin = news20_minmin + (np.loadtxt(os.path.join(datadir, file)),)
elif file.startswith(dataset+'cLearn') and file.endswith('.txt'):
news20_cLearn = news20_cLearn + (np.loadtxt(os.path.join(datadir, file)),)
info = file.replace(dataset+'cLearn_','').replace('_result.txt','').split('_')
if len(info) == 1 and info[0] == 'result.txt':
news20_cLearn_info = news20_cLearn_info + (0,)
elif len(info) == 1 and info[0] == 'nosub':
news20_cLearn_info = news20_cLearn_info + (1,)
elif len(info) == 2:
news20_cLearn_info = news20_cLearn_info + ([int(info[0]), int(info[1])],)
else:
pass
return news20_random, news20_spl, news20_spl_info, news20_spld, news20_spld_info, news20_cLearn, news20_cLearn_info, news20_cLearnk, news20_cLearnk_info, news20_minmin, news20_maxmax
def data_clean(random, spl, spld, cLearn, cLearnk, minmin, maxmax, clip=0):
for trial in random:
clip = max([clip, trial[0, 0]])
#print(trial[0, 0])
for trial in spl:
clip = max([clip, trial[0, 0]])
#print(trial[0, 0])
for trial in spld:
clip = max([clip, trial[0, 0]])
#print(trial[0, 0])
for trial in cLearn:
clip = max([clip, trial[0, 0]])
#print(trial[0, 0])
for trial in cLearnk:
clip = max([clip, trial[0, 0]])
for trial in maxmax:
clip = max([clip, trial[0, 0]])
for trial in minmin:
clip = max([clip, trial[0, 0]])
print('clip=', clip)
randomC = ()
for trial in random:
randomC = randomC + (trial[trial[:,0]>=clip,:],)
splC = ()
for trial in spl:
splC = splC + (trial[trial[:,0]>=clip,:],)
spldC = ()
for trial in spld:
spldC = spldC + (trial[trial[:,0]>=clip,:],)
cLearnC = ()
for trial in cLearn:
cLearnC = cLearnC + (trial[trial[:,0]>=clip,:],)
cLearnkC = ()
for trial in cLearnk:
cLearnkC = cLearnkC + (trial[trial[:,0]>=clip,:],)
minminC = ()
for trial in minmin:
minminC = minminC + (trial[trial[:,0]>=clip,:],)
maxmaxC = ()
for trial in maxmax:
maxmaxC = maxmaxC + (trial[trial[:,0]>=clip,:],)
return randomC, splC, spldC, cLearnC, cLearnkC, minminC, maxmaxC
def read_files(dataset, datadir = '/home/tianyizhou/Dropbox/sensor_room/cLearn/result', clip = 0):
if dataset == 'news20':
dataset = '20newsgroups_logistic_'
elif dataset == 'mnist':
dataset = 'mnist_lenet5_'
elif dataset == 'cifar10':
dataset = 'cifar10_convnet_'
else:
print('no dataset found!')
random, spl, spl_info, spld, spld_info, cLearn, cLearn_info, cLearnk, cLearnk_info, minmin, maxmax = load_news20_result(dataset, datadir)
random, spl, spld, cLearn, cLearnk, minmin, maxmax = data_clean(random, spl, spld, cLearn, cLearnk, minmin, maxmax, clip)
return random, spl, spl_info, spld, spld_info, cLearn, cLearn_info, cLearnk, cLearnk_info, minmin, maxmax
def plt_error_vs_ntrain(dataset, error, ntrain, random, spl, spl_info, spld, spld_info, cLearn, cLearn_info, cLearnk, cLearnk_info):
if ntrain:
xaxis = 0
xlabel = 'Number of used training samples'
fname1 = 'ntrain'
else:
xaxis = 1
xlabel = 'Number of gradient computation on one sample (sample-wise passes)'
fname1 = 'npass'
if error:
fname0 = 'err'
ylabel = 'Error rate (%)'
else:
fname0 = 'loss'
ylabel = 'Loss'
if dataset == 'news20' and error:
ttid = [4,5]
elif dataset == 'mnist' and error:
ttid = [2,4]
elif dataset == 'cifar10' and not error:
ttid = [2,3]
elif dataset == 'news20' and not error:
ttid = [2,3]
else:
print('dataset or result not found!')
ax = plt.figure(figsize = (18, 10))
#ax = plt.figure(figsize = (18, 8))
#ax = plt.figure(figsize = (13, 8))
ax = plt.subplot(111)
for i in range(len(random)):
trial = random[i]
# train err
ax.plot(trial[:, xaxis], trial[:, ttid[0]], c = 'grey', ls = '-', marker = '.', linewidth=1, alpha=0.5)
# test err
ax.plot(trial[:, xaxis], trial[:, ttid[1]], c = 'grey', ls = '--', marker = '.', linewidth=1, alpha=0.5)
spl_color = ['gold','darkorange','orange','goldenrod']
for i in range(len(spl)):
trial = spl[i]
info = spl_info[i]
# train err
ax.plot(trial[:, xaxis], trial[:, ttid[0]], c = spl_color[i], ls = '-', marker = 's', markersize = 7, linewidth=2, alpha=0.65, label = 'SPL train:'+str(info[0])+','+str(info[1]))
# test err
ax.plot(trial[:, xaxis], trial[:, ttid[1]], c = spl_color[i], ls = '--', marker = 's', markersize = 7, linewidth=2, alpha=0.65, label = 'SPL test:'+str(info[0])+','+str(info[1]))
spld_color = ['lime','cyan','sage','darkturquoise','darkgreen', 'deepskyblue']
for i in range(len(spld)):
trial = spld[i]
info = spld_info[i]
# train err
ax.plot(trial[:, xaxis], trial[:, ttid[0]], c = spld_color[i], ls = '-', marker = '^', markersize = 7, linewidth=2, alpha=0.65, label = 'SPLD train:'+str(info[0])+','+str(info[1]))
# test err
ax.plot(trial[:, xaxis], trial[:, ttid[1]], c = spld_color[i], ls = '--', marker = '^', markersize = 7, linewidth=2, alpha=0.65, label = 'SPLD test:'+str(info[0])+','+str(info[1]))
cLearn_color = ['lightcoral','maroon','fuchsia','crimson','deeppink', 'darkviolet']
j = 0
for i in range(len(cLearn)):
trial = cLearn[i]
info = cLearn_info[i]
if cLearn_info[i] == 0:
cc = 'red'
ll = ['MCL ', 'w/o random']
mk = 'o'
elif cLearn_info[i] == 1:
cc = 'blue'
ll = ['MCL ', 'w/o submodular']
mk = 'o'
else:
cc = cLearn_color[j]
ll = ['MCL+random ', str(info[0])+','+str(info[1])]
mk = 'v'
j += 1
# train err
ax.plot(trial[:, xaxis], trial[:, ttid[0]], c = cc, ls = '-', marker = mk, markersize = 7, linewidth=2, alpha=0.65, label = ll[0] + 'train:'+ll[1])
# test err
ax.plot(trial[:, xaxis], trial[:, ttid[1]], c = cc, ls = '--', marker = mk, markersize = 7, linewidth=2, alpha=0.65, label = ll[0] + 'test:'+ll[1])
cLearnk_color = ['royalblue','steelblue','navy','dodgerblue','lightskyblue']
for i in range(len(cLearnk)):
trial = cLearnk[i]
info = cLearnk_info[i]
# train err
ax.plot(trial[:, xaxis], trial[:, ttid[0]], c = cLearnk_color[i], ls = '-', marker = 'd', markersize = 7, linewidth=2, alpha=0.65, label = 'MCL+k train:'+str(info))
# test err
ax.plot(trial[:, xaxis], trial[:, ttid[1]], c = cLearnk_color[i], ls = '--', marker = 'd', markersize = 7, linewidth=2, alpha=0.65, label ='MCL+k test:'+str(info))
plt.grid()
plt.legend(fontsize='x-small', loc = 1)
plt.ylabel(ylabel)
plt.xlabel(xlabel)
#plt.xlim([5050, 50200])
plt.xlim([0, 1e+7])
plt.yscale('log')
plt.ylim([0.1, 3])
# Shrink current axis by 20%
box = ax.get_position()
ax.set_position([box.x0, box.y0, box.width * 0.8, box.height])
# Put a legend to the right of the current axis
ax.legend(loc='center left', bbox_to_anchor=(1, 0.5))
plt.savefig('./result/'+dataset+'_'+fname0+'_vs_'+fname1+'.eps', format = 'eps', bbox_inches='tight')
plt.show()
def plt_error_cifar10(dataset, ntrain, random, spl, spl_info, spld, spld_info, cLearn, cLearn_info, cLearnk, cLearnk_info, minmin, maxmax):
if ntrain:
xaxis = 0
xlabel = 'Number of used training samples'
fname1 = 'ntrain'
else:
xaxis = 1
xlabel = 'Number of gradient computation on one sample (sample-wise passes)'
fname1 = 'npass'
#ax = plt.figure(figsize = (18, 8))
ax = plt.figure(figsize = (13, 8))
ax = plt.subplot(111)
for i in range(len(random)):
trial = random[i]
# test err
ax.plot(trial[:, xaxis], 100-trial[:, -1], c = 'grey', ls = '--', marker = '.', linewidth=1, alpha=0.5)
spl_color = ['gold','darkorange','orange','goldenrod']
for i in range(len(spl)):
trial = spl[i]
info = spl_info[i]
# test err
ax.plot(trial[:, xaxis], 100-trial[:, -1], c = spl_color[i], ls = '--', marker = 's', markersize = 7, linewidth=2, alpha=0.65, label = 'SPL test:'+str(info[0])+','+str(info[1]))
spld_color = ['lime','cyan','sage','darkturquoise','darkgreen', 'deepskyblue']
for i in range(len(spld)):
trial = spld[i]
info = spld_info[i]
# test err
ax.plot(trial[:, xaxis], 100-trial[:, -1], c = spld_color[i], ls = '--', marker = '^', markersize = 7, linewidth=2, alpha=0.65, label = 'SPLD test:'+str(info[0])+','+str(info[1]))
cLearn_color = ['lightcoral','maroon','fuchsia','crimson','deeppink', 'darkviolet']
j = 0
for i in range(len(cLearn)):
trial = cLearn[i]
info = cLearn_info[i]
if cLearn_info[i] == 0:
cc = 'red'
ll = ['MCL ', 'w/o random']
mk = 'o'
elif cLearn_info[i] == 1:
cc = 'blue'
ll = ['MCL ', 'w/o submodular']
mk = 'o'
else:
cc = cLearn_color[j]
ll = ['MCL+random ', str(info[0])+','+str(info[1])]
mk = 'v'
j += 1
# test err
ax.plot(trial[:, xaxis], 100-trial[:, -1], c = cc, ls = '--', marker = mk, markersize = 7, linewidth=2, alpha=0.65, label = ll[0] + 'test:'+ll[1])
cLearnk_color = ['royalblue','steelblue','navy','dodgerblue','lightskyblue']
for i in range(len(cLearnk)):
trial = cLearnk[i]
info = cLearnk_info[i]
# test err
ax.plot(trial[:, xaxis], 100-trial[:, -1], c = cLearnk_color[i], ls = '--', marker = 'd', markersize = 7, linewidth=2, alpha=0.65, label ='MCL+k test:'+str(info))
minmin_color = ['saddlebrown','steelblue','navy','dodgerblue','lightskyblue']
for i in range(len(minmin)):
trial = minmin[i]
# test err
ax.plot(trial[:, xaxis], 100-trial[:, -1], c = minmin_color[i], ls = '--', marker = '+', markersize = 7, linewidth=2, alpha=0.65, label ='minmin+k test:8')
maxmax_color = ['olivedrab','steelblue','navy','dodgerblue','lightskyblue']
for i in range(len(maxmax)):
trial = maxmax[i]
# test err
ax.plot(trial[:, xaxis], 100-trial[:, -1], c = maxmax_color[i], ls = '--', marker = 'x', markersize = 7, linewidth=2, alpha=0.65, label ='maxmax+k test:8')
plt.grid()
plt.legend(fontsize='medium', loc = 1)
plt.ylabel('Error rate (%)')
plt.xlabel(xlabel)
plt.xlim([5600, 50200])
#plt.xlim([0, 7e+6])
#plt.yscale('log')
#plt.ylim([0, 3])
# Shrink current axis by 20%
#box = ax.get_position()
#ax.set_position([box.x0, box.y0, box.width * 0.8, box.height])
# Put a legend to the right of the current axis
#ax.legend(loc='center left', bbox_to_anchor=(1, 0.5))
plt.savefig('./result1/'+dataset+'_err_vs_'+fname1+'.eps', format = 'eps', bbox_inches='tight')
plt.show()
if __name__ == '__main__':
#random, spl, spl_info, spld, spld_info, cLearn, cLearn_info, cLearnk, cLearnk_info = read_files('news20', '/Users/tianyi/Dropbox/sensor_room/cLearn/result')
#plt_error_vs_ntrain('news20', True, True, random, spl, spl_info, spld, spld_info, cLearn, cLearn_info, cLearnk, cLearnk_info)
#plt_error_vs_ntrain('news20', False, True, random, spl, spl_info, spld, spld_info, cLearn, cLearn_info, cLearnk, cLearnk_info)
#plt_error_vs_ntrain('news20', True, False, random, spl, spl_info, spld, spld_info, cLearn, cLearn_info, cLearnk, cLearnk_info)
#plt_error_vs_ntrain('news20', False, False, random, spl, spl_info, spld, spld_info, cLearn, cLearn_info, cLearnk, cLearnk_info)
#random, spl, spl_info, spld, spld_info, cLearn, cLearn_info, cLearnk, cLearnk_info = read_files('mnist', '/Users/tianyi/Dropbox/sensor_room/cLearn/result')
#plt_error_vs_ntrain('mnist', True, True, random, spl, spl_info, spld, spld_info, cLearn, cLearn_info, cLearnk, cLearnk_info)
#plt_error_vs_ntrain('mnist', True, False, random, spl, spl_info, spld, spld_info, cLearn, cLearn_info, cLearnk, cLearnk_info)
random, spl, spl_info, spld, spld_info, cLearn, cLearn_info, cLearnk, cLearnk_info, minmin, maxmax = read_files('cifar10','/Users/tianyi/Dropbox/sensor_room/cLearn/result1')
#plt_error_vs_ntrain('cifar10', False, True, random, spl, spl_info, spld, spld_info, cLearn, cLearn_info, cLearnk, cLearnk_info)
#plt_error_vs_ntrain('cifar10', False, False, random, spl, spl_info, spld, spld_info, cLearn, cLearn_info, cLearnk, cLearnk_info)
plt_error_cifar10('cifar10', True, random, spl, spl_info, spld, spld_info, cLearn, cLearn_info, cLearnk, cLearnk_info, minmin, maxmax)
#plt_error_cifar10('cifar10', False, random, spl, spl_info, spld, spld_info, cLearn, cLearn_info, cLearnk, cLearnk_info, minmin, maxmax)
# from plot_cLearn import *