-
Notifications
You must be signed in to change notification settings - Fork 11
/
datasets.py
587 lines (497 loc) · 23.6 KB
/
datasets.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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
import os
import numpy as np
from sklearn.datasets import fetch_openml
from torch.utils.data import Dataset
import logging
def get_mnist():
mnist_data = fetch_openml("mnist_784")
x = mnist_data["data"]
y = mnist_data["target"]
# reshape to (#data, #channel, width, height)
x = np.reshape(x, (x.shape[0], 1, 28, 28)) / 255.
x_tr = np.asarray(x[:60000], dtype=np.float32)
y_tr = np.asarray(y[:60000], dtype=np.int32)
x_te = np.asarray(x[60000:], dtype=np.float32)
y_te = np.asarray(y[60000:], dtype=np.int32)
return (x_tr, y_tr), (x_te, y_te)
def binarize_mnist_class(_trainY, _testY):
trainY = np.ones(len(_trainY), dtype=np.int32)
trainY[_trainY % 2 == 1] = -1
testY = np.ones(len(_testY), dtype=np.int32)
testY[_testY % 2 == 1] = -1
return trainY, testY
class PNMNIST(Dataset):
def __init__(self, split):
(self.x_tr, self.y_tr), (self.x_te, self.y_te) = get_mnist()
self.y_tr, self.y_te = binarize_mnist_class(self.y_tr, self.y_te)
self.split = split
print(self.x_te.shape)
print(self.x_tr.shape)
print(self.y_te.shape)
print(self.y_tr.shape)
def __len__(self):
if (self.split == 'train'):
return self.x_tr.shape[0]
else:
return self.x_te.shape[0]
def __getitem__(self, index):
if (self.split == 'train'):
return self.x_tr[index], self.y_tr[index]
else:
return self.x_te[index], self.y_te[index]
def make_dataset(dataset, n_labeled, n_unlabeled, mode="train", pn=False, seed = None):
def make_PU_dataset_from_binary_dataset(x, y, labeled=n_labeled, unlabeled=n_unlabeled):
labels = np.unique(y)
positive, negative = labels[1], labels[0]
X, Y = np.asarray(x, dtype=np.float32), np.asarray(y, dtype=np.int32)
if seed is not None:
np.random.seed(seed)
perm = np.random.permutation(len(X))
X, Y = X[perm], Y[perm]
assert(len(X) == len(Y))
n_p = (Y == positive).sum()
n_lp = labeled
n_u = unlabeled
if labeled + unlabeled == len(X):
n_up = n_p - n_lp
elif unlabeled == len(X):
n_up = n_p
else:
raise ValueError("Only support |P|+|U|=|X| or |U|=|X|.")
prior = float(n_up) / float(n_u)
Xlp = X[Y == positive][:n_lp]
Xup = np.concatenate((X[Y == positive][n_lp:], Xlp), axis=0)[:n_up]
Xun = X[Y == negative]
X = np.asarray(np.concatenate((Xlp, Xup, Xun), axis=0), dtype=np.float32)
Y = np.asarray(np.concatenate((np.ones(n_lp), -np.ones(n_u))), dtype=np.int32)
T = np.asarray(np.concatenate((np.ones(n_lp + n_up), -np.ones(n_u-n_up))), dtype=np.int32)
# Generate ID
ids = np.array([i for i in range(len(X))])
return X, Y, T, ids, prior
def make_PN_dataset_from_binary_dataset(x, y):
labels = np.unique(y)
positive, negative = labels[1], labels[0]
X, Y = np.asarray(x, dtype=np.float32), np.asarray(y, dtype=np.int32)
if seed is not None:
np.random.seed(seed)
perm = np.random.permutation(len(X))
X, Y = X[perm], Y[perm]
n_p = (Y == positive).sum()
n_n = (Y == negative).sum()
Xp = X[Y == positive][:n_p]
Xn = X[Y == negative][:n_n]
X = np.asarray(np.concatenate((Xp, Xn)), dtype=np.float32)
Y = np.asarray(np.concatenate((np.ones(n_p), -np.ones(n_n))), dtype=np.int32)
ids = np.array([i for i in range(len(X))])
return X, Y, Y, ids
def make_only_PN_train(x, y, n_labeled=n_labeled, prior=0.5):
labels = np.unique(y)
positive, negative = labels[1], labels[0]
X, Y = np.asarray(x, dtype=np.float32), np.asarray(y, dtype=np.int32)
if seed is not None:
np.random.seed(seed)
perm = np.random.permutation(len(X))
X, Y = X[perm], Y[perm]
assert(len(X) == len(Y))
n_n = int(n_labeled * pow(prior / (2 * (1-prior)), 2))
Xp = X[Y == positive][:n_labeled]
Xn = X[Y == negative][:n_n]
X = np.asarray(np.concatenate((Xp, Xn)), dtype=np.float32)
Y = np.asarray(np.concatenate((np.ones(n_labeled), -np.ones(n_n))), dtype=np.int32)
ids = np.array([i for i in range(len(X))])
return X, Y, Y, ids
(_trainX, _trainY), (_testX, _testY) = dataset
prior = None
if (mode == 'train'):
if not pn:
X, Y, T, ids, prior = make_PU_dataset_from_binary_dataset(_trainX, _trainY)
else:
X, Y, T, ids = make_only_PN_train(_trainX, _trainY)
else:
X, Y, T, ids = make_PN_dataset_from_binary_dataset(_testX, _testY)
return X, Y, T, ids, prior
class MNIST_Dataset(Dataset):
def __init__(self, n_labeled, n_unlabeled, trainX, trainY, testX, testY, type="noisy", split="train", mode="N", ids=None, pn=False, increasing=False, replacement=True, top = 0.5, flex = 0, pickout=True, seed = None):
self.X, self.Y, self.T, self.oids, self.prior = make_dataset(((trainX, trainY), (testX, testY)), n_labeled, n_unlabeled, mode=split, pn=pn, seed = seed)
assert np.all(self.oids == np.linspace(0, len(self.X) - 1, len(self.X)))
self.clean_ids = []
# self.Y_origin = self.Y
self.P = self.Y.copy()
self.type = type
if (ids is None):
self.ids = self.oids
else:
self.ids = np.array(ids)
self.split = split
self.mode = mode
self.pos_ids = self.oids[self.Y == 1]
self.pid = self.pos_ids
if len(self.ids) != 0:
self.uid = np.intersect1d(self.ids[self.Y[self.ids] == -1], self.ids)
else:
self.uid = []
print(len(self.uid))
print(len(self.pid))
self.sample_ratio = len(self.uid) // len(self.pid) + 1
print(self.sample_ratio)
print("origin:", len(self.pos_ids), len(self.ids))
self.increasing = increasing
self.replacement = replacement
self.top = top
self.flex = flex
self.pickout = pickout
self.pick_accuracy = []
self.result = -np.ones(len(self))
def copy(self, dataset):
''' Copy random sequence
'''
self.X, self.Y, self.T, self.oids = dataset.X.copy(), dataset.Y.copy(), dataset.T.copy(), dataset.oids.copy()
self.P = self.Y.copy()
def __len__(self):
if self.type == 'noisy':
return len(self.pid) * self.sample_ratio
else:
return len(self.ids)
def set_type(self, type):
self.type = type
def update_prob(self, result):
rank = np.empty_like(result)
rank[np.argsort(result)] = np.linspace(0, 1, len(result))
#print(rank)
if (len(self.pos_ids) > 0):
rank[self.pos_ids] = -1
self.result = rank
def shuffle(self):
perm = np.random.permutation(len(self.uid))
self.uid = self.uid[perm]
perm = np.random.permutation(len(self.pid))
self.pid = self.pid[perm]
def __getitem__(self, idx):
#print(idx)
# self.ids[idx]是真实的行索引
# 始终使用真实的行索引去获得数据
# 1901 保持比例
if self.type == 'noisy':
if (idx % self.sample_ratio == 0):
try:
index = self.pid[idx // self.sample_ratio]
id = self.ids[idx // self.sample_ratio]
except IndexError:
print(idx)
print(self.sample_ratio)
print(len(self.pid))
else:
index = self.uid[idx - (idx // self.sample_ratio + 1)]
id = self.ids[idx - (idx // self.sample_ratio + 1)]
return self.X[index], self.Y[index], self.P[index], self.T[index], id, self.result[index]
else:
return self.X[self.ids[idx]], self.Y[self.ids[idx]], self.P[self.ids[idx]], self.T[self.ids[idx]], self.ids[idx], self.result[self.ids[idx]]
def reset_ids(self):
''' Using all origin ids
'''
self.ids = self.oids.copy()
def set_ids(self, ids):
''' Set specific ids
'''
self.ids = np.array(ids).copy()
if len(ids) > 0:
self.uid = np.intersect1d(self.ids[self.Y[self.ids] == -1], self.ids)
self.pid = np.intersect1d(self.ids[self.Y[self.ids] == 1], self.ids)
if len(self.pid) == 0:
self.sample_ratio = 10000000000
else:
self.sample_ratio = int(len(self.uid) / len(self.pid)) + 1
def reset_labels(self):
''' Reset Y labels
'''
self.P = self.Y.copy()
def update_ids(self, results, epoch, ratio=None, lt = 0, ht = 0):
if not self.replacement or self.increasing:
percent = min(epoch / 100, 1) # 决定抽取数据的比例
else:
percent = 1
if ratio == None:
ratio = self.prior
if self.mode == 'N':
self.reset_labels()
n_neg = int((len(self.oids) - len(self.pos_ids)) * (1 - ratio) * percent) # 决定抽取的数量
if self.replacement:
# 如果替换的话,抽取n_neg个
neg_ids = np.setdiff1d(np.argsort(results), self.pos_ids, assume_unique=True)[:n_neg]
else:
# 否则抽取n_neg - #ids
neg_ids = np.setdiff1d(np.argsort(results), self.ids, assume_unique=True)[:n_neg]
# 变成向量
neg_ids = np.array(neg_ids)
neg_label = self.T[neg_ids] # 获得neg_ids的真实标签
correct = np.sum(neg_label < 1) # 抽取N的时候真实标签为-1
print("Correct: {}/{}".format(correct, len(neg_ids))) # 打印
if self.replacement:
self.ids = np.concatenate([self.pos_ids[:len(self.pos_ids) // 2], neg_ids]) # 如果置换的话,在ids的基础上加上neg_ids
else:
if len(self.ids) == 0: self.ids = np.concatenate([self.ids, self.pos_ids[:len(self.pos_ids) // 2]]) # 如果为空的话则首先加上pos_ids
self.ids = np.concatenate([self.ids, neg_ids])
self.ids = self.ids.astype(int) # 为了做差集
out = np.setdiff1d(self.oids, self.ids) # 计算剩下的ids的数量并返回
#assert len(np.intersect1d(self.ids, out)) == 0 # 要求两者不能有重合
return out
elif self.mode == 'P':
self.reset_labels()
n_pos = int((len(self.oids) - len(self.pos_ids)) * (1 - ratio) * percent) # 决定抽取的数量
if self.replacement:
# 如果替换的话,抽取n_neg个
pos_ids = np.setdiff1d(np.argsort(results), self.pos_ids, assume_unique=True)[-n_pos:]
else:
# 否则抽取n_neg - #ids
pos_ids = np.setdiff1d(np.argsort(results), self.pos_ids, assume_unique=True)[-(n_pos - len(self.ids)):]
# 变成向量
pos_ids = np.array(pos_ids)
pos_label = self.T[pos_ids] # 获得neg_ids的真实标签
correct = np.sum(pos_label == 1) # 抽取N的时候真实标签为-1
self.Y[pos_ids] = 1 # 将他们标注为1
print("Correct: {}/{}".format(correct, len(pos_ids))) # 打印
if self.replacement:
self.ids = np.concatenate([self.pos_ids[:len(self.pos_ids) // 2], pos_ids]) # 如果置换的话,在ids的基础上加上neg_ids
else:
if len(self.ids) == 0: self.ids = np.concatenate([self.ids, self.pos_ids[:len(self.pos_ids) // 2]]) # 如果为空的话则首先加上pos_ids
self.ids = np.concatenate([self.ids, pos_ids])
self.ids = self.ids.astype(int) # 为了做差集
out = np.setdiff1d(self.oids, self.ids) # 计算剩下的ids的数量并返回
#assert len(np.intersect1d(self.ids, out)) == 0 # 要求两者不能有重合
return out
elif self.mode == 'A':
self.reset_labels()
n_all = int((len(self.oids) - len(self.pos_ids)) * (1 - ratio) * percent * self.top) # 决定抽取的数量
confident_num = int(n_all * (1 - self.flex))
noisy_num = int(n_all * self.flex)
if self.replacement:
# 如果替换的话,抽取n_pos个
#print(np.argsort(results))
#print(np.setdiff1d(np.argsort(results), self.pos_ids, assume_unique=True))
al = np.setdiff1d(np.argsort(results), self.pos_ids, assume_unique=True)
neg_ids = al[:confident_num]
pos_ids = al[-confident_num:]
else:
# 否则抽取n_pos - #ids
al = np.setdiff1d(np.argsort(results), self.ids, assume_unique=True)
neg_ids = al[:(confident_num - len(self.ids) // 2)]
pos_ids = al[-(confident_num - len(self.ids) // 2):]
# 变成向量
pos_ids = np.array(pos_ids)
pos_label = self.T[pos_ids] # 获得neg_ids的真实标签
pcorrect = np.sum(pos_label == 1) # 抽取N的时候真实标签为-1
neg_ids = np.array(neg_ids)
neg_label = self.T[neg_ids] # 获得neg_ids的真实标签
ncorrect = np.sum(neg_label < 1)
self.P[pos_ids] = 1 # 将他们标注为1
print("P Correct: {}/{}".format(pcorrect, len(pos_ids))) # 打印
print("N Correct: {}/{}".format(ncorrect, len(neg_ids)))
self.pick_accuracy.append((pcorrect + ncorrect) * 1.0 / (len(pos_ids) * 2))
if self.replacement:
#self.ids = np.concatenate([self.pos_ids, pos_ids, neg_ids]) # 如果置换的话,在ids的基础上加上neg_ids
self.ids = np.concatenate([pos_ids, neg_ids])
else:
#if len(self.ids) == 0: self.ids = np.concatenate([self.ids, self.pos_ids]) # 如果为空的话则首先加上pos_ids
#self.ids = np.concatenate([self.ids, pos_ids, neg_ids])
self.ids = np.concatenate([self.ids, pos_ids, neg_ids])
self.ids = self.ids.astype(int) # 为了做差集
if self.pickout:
out = np.setdiff1d(self.oids, self.ids) # 计算剩下的ids的数量并返回
else:
out = self.oids
if noisy_num > 0:
noisy_select = out[np.random.permutation(len(out))][:noisy_num]
self.P[np.intersect1d(results >= 0.5, noisy_select)] = 1
self.ids = np.concatenate([self.ids, noisy_select], 0)
if self.pickout:
out = np.setdiff1d(self.oids, self.ids)
if self.pickout:
assert len(np.intersect1d(self.ids, out)) == 0 # 要求两者不能有重合
return out
elif self.mode == 'T':
self.reset_labels()
al = np.setdiff1d(np.argsort(results), self.pos_ids, assume_unique=True)
print(lt)
print(ht)
negative_confident_num = int(lt * len(al))
positive_confident_num = int((1-ht) * len(al))
neg_ids = al[:negative_confident_num]
pos_ids = al[len(al)-positive_confident_num:]
# 变成向量
pos_ids = np.array(pos_ids)
pos_label = self.T[pos_ids] # 获得neg_ids的真实标签
pcorrect = np.sum(pos_label == 1) # 抽取N的时候真实标签为-1
neg_ids = np.array(neg_ids)
neg_label = self.T[neg_ids] # 获得neg_ids的真实标签
ncorrect = np.sum(neg_label < 1)
self.P[pos_ids] = 1 # 将他们标注为1
print("P Correct: {}/{}".format(pcorrect, len(pos_ids))) # 打印
print("N Correct: {}/{}".format(ncorrect, len(neg_ids)))
self.pick_accuracy.append((pcorrect + ncorrect) * 1.0 / (len(pos_ids) * 2))
if self.replacement:
#self.ids = np.concatenate([self.pos_ids, pos_ids, neg_ids]) # 如果置换的话,在ids的基础上加上neg_ids
self.ids = np.concatenate([pos_ids, neg_ids])
else:
#if len(self.ids) == 0: self.ids = np.concatenate([self.ids, self.pos_ids]) # 如果为空的话则首先加上pos_ids
#self.ids = np.concatenate([self.ids, pos_ids, neg_ids])
self.ids = np.concatenate([self.ids, pos_ids, neg_ids])
self.ids = self.ids.astype(int) # 为了做差集
if self.pickout:
out = np.setdiff1d(self.oids, self.ids) # 计算剩下的ids的数量并返回
else:
out = self.oids
if self.pickout:
assert len(np.intersect1d(self.ids, out)) == 0 # 要求两者不能有重合
return out
class MNIST_Dataset_FixSample(Dataset):
def __init__(self, n_labeled, n_unlabeled, trainX, trainY, testX, testY, type="noisy", split="train", mode="A", ids=None, pn=False, increasing=False, replacement=True, top = 0.5, flex = 0, pickout=True, seed = None):
self.X, self.Y, self.T, self.oids, self.prior = make_dataset(((trainX, trainY), (testX, testY)), n_labeled, n_unlabeled, mode=split, pn=pn, seed = seed)
assert np.all(self.oids == np.linspace(0, len(self.X) - 1, len(self.X)))
self.clean_ids = []
#self.Y_origin = self.Y
self.P = self.Y.copy()
self.type = type
if (ids is None):
self.ids = self.oids
else:
self.ids = np.array(ids)
self.split = split
self.mode = mode
self.pos_ids = self.oids[self.Y == 1]
self.pid = self.pos_ids
if len(self.ids) != 0:
self.uid = np.intersect1d(self.ids[self.Y[self.ids] == -1], self.ids)
else:
self.uid = []
print(len(self.uid))
print(len(self.pid))
self.sample_ratio = len(self.uid) // len(self.pid) + 1
print(self.sample_ratio)
print("origin:", len(self.pos_ids), len(self.ids))
self.increasing = increasing
self.replacement = replacement
self.top = top
self.flex = flex
self.pickout = pickout
self.pick_accuracy = []
self.result = -np.ones(len(self))
self.random_count = 0
def copy(self, dataset):
''' Copy random sequence
'''
self.X, self.Y, self.T, self.oids = dataset.X.copy(), dataset.Y.copy(), dataset.T.copy(), dataset.oids.copy()
self.P = self.Y.copy()
def __len__(self):
if self.type == 'noisy':
#return len(self.uid) * 2
return len(self.pid) * self.sample_ratio
else:
return len(self.ids)
def set_type(self, type):
self.type = type
def update_prob(self, result):
rank = np.empty_like(result)
rank[np.argsort(result)] = np.linspace(0, 1, len(result))
#print(rank)
if (len(self.pos_ids) > 0):
rank[self.pos_ids] = -1
self.result = rank
def shuffle(self):
perm = np.random.permutation(len(self.uid))
self.uid = self.uid[perm]
perm = np.random.permutation(len(self.pid))
self.pid = self.pid[perm]
def __getitem__(self, idx):
#print(idx)
# self.ids[idx]是真实的行索引
# 始终使用真实的行索引去获得数据
# 1901 保持比例
if self.type == 'noisy':
'''
if (idx % 2 == 0):
index = self.pid[idx % 1000]
else:
index = self.uid[idx - (idx // 2 + 1)]
'''
if (idx % self.sample_ratio == 0):
index = self.pid[idx // self.sample_ratio]
id = 0
else:
index = self.uid[idx - (idx // self.sample_ratio + 1)]
return self.X[index], self.Y[index], self.P[index], self.T[index], index, 0
else:
return self.X[self.ids[idx]], self.Y[self.ids[idx]], self.P[self.ids[idx]], self.T[self.ids[idx]], self.ids[idx], 0
def reset_ids(self):
''' Using all origin ids
'''
self.ids = self.oids.copy()
def set_ids(self, ids):
''' Set specific ids
'''
self.ids = np.array(ids).copy()
if len(ids) > 0:
self.uid = np.intersect1d(self.ids[self.Y[self.ids] == -1], self.ids)
self.pid = np.intersect1d(self.ids[self.Y[self.ids] == 1], self.ids)
if len(self.pid) == 0:
self.sample_ratio = 10000000000
else:
self.sample_ratio = int(len(self.uid) / len(self.pid)) + 1
def reset_labels(self):
''' Reset Y labels
'''
self.P = self.Y.copy()
def update_ids(self, results, epoch, ratio=None, lt = 0, ht = 0):
if not self.replacement or self.increasing:
percent = min(epoch / 100, 1) # 决定抽取数据的比例
else:
percent = 1
if ratio == None:
ratio = self.prior
self.reset_labels()
n_all = int((len(self.oids) - len(self.pos_ids)) * (1 - ratio) * percent * self.top) # 决定抽取的数量
confident_num = int(n_all * (1 - self.flex))
noisy_num = int(n_all * self.flex)
if self.replacement:
# 如果替换的话,抽取n_pos个
#print(np.argsort(results))
#print(np.setdiff1d(np.argsort(results), self.pos_ids, assume_unique=True))
al = np.setdiff1d(np.argsort(results), self.pos_ids, assume_unique=True)
neg_ids = al[:confident_num]
pos_ids = al[-confident_num:]
else:
# 否则抽取n_pos - #ids
al = np.setdiff1d(np.argsort(results), self.ids, assume_unique=True)
neg_ids = al[:(confident_num - len(self.ids) // 2)]
pos_ids = al[-(confident_num - len(self.ids) // 2):]
# 变成向量
pos_ids = np.array(pos_ids)
pos_label = self.T[pos_ids] # 获得neg_ids的真实标签
pcorrect = np.sum(pos_label == 1) # 抽取N的时候真实标签为-1
neg_ids = np.array(neg_ids)
neg_label = self.T[neg_ids] # 获得neg_ids的真实标签
ncorrect = np.sum(neg_label < 1)
self.P[pos_ids] = 1 # 将他们标注为1
print("P Correct: {}/{}".format(pcorrect, len(pos_ids))) # 打印
print("N Correct: {}/{}".format(ncorrect, len(neg_ids)))
self.pick_accuracy.append((pcorrect + ncorrect) * 1.0 / (len(pos_ids) * 2))
if self.replacement:
#self.ids = np.concatenate([self.pos_ids, pos_ids, neg_ids]) # 如果置换的话,在ids的基础上加上neg_ids
self.ids = np.concatenate([pos_ids, neg_ids])
else:
#if len(self.ids) == 0: self.ids = np.concatenate([self.ids, self.pos_ids]) # 如果为空的话则首先加上pos_ids
#self.ids = np.concatenate([self.ids, pos_ids, neg_ids])
self.ids = np.concatenate([self.ids, pos_ids, neg_ids])
self.ids = self.ids.astype(int) # 为了做差集
if self.pickout:
out = np.setdiff1d(self.oids, self.ids) # 计算剩下的ids的数量并返回
else:
out = self.oids
if noisy_num > 0:
noisy_select = out[np.random.permutation(len(out))][:noisy_num]
self.P[np.intersect1d(results >= 0.5, noisy_select)] = 1
self.ids = np.concatenate([self.ids, noisy_select], 0)
if self.pickout:
out = np.setdiff1d(self.oids, self.ids)
if self.pickout:
assert len(np.intersect1d(self.ids, out)) == 0 # 要求两者不能有重合
return out