-
Notifications
You must be signed in to change notification settings - Fork 4
/
train_PU.py
290 lines (202 loc) · 10.6 KB
/
train_PU.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
import os
import argparse
import time
import random
import numpy as np
import torch
import torch.nn as nn
import torch.optim as optim
import torch.nn.functional as F
import torch.backends.cudnn as cudnn
import torchvision
import torchvision.transforms as transforms
from transformers import AdamW
from algorithm import *
from model_helper import *
from helper import *
from estimator import *
from baselines import *
np.set_printoptions(suppress=True, precision=1)
parser = argparse.ArgumentParser(description='PU Learning Training')
parser.add_argument('--lr', default=0.1, type=float, help='learning rate')
parser.add_argument('--wd', default=5e-4, type=float, help='Weight decay')
parser.add_argument('--momentum', default=0.9, type=float, help='SGD momentum')
parser.add_argument('--batch-size', type=int, default=200, help='input batch size')
parser.add_argument('--data-type', type=str, help='mnist | cifar')
parser.add_argument('--train-method', type=str, help='training algorithm to use')
parser.add_argument('--net-type', type=str, help='linear | FCN | ResNet')
parser.add_argument('--sigmoid-loss', default=True, action='store_false', help='Sigmoid loss for nnPU training')
parser.add_argument('--estimate-alpha', default=True, action='store_false', help='Estimate alpha')
parser.add_argument('--warm-start', action='store_true', default=False, help='Start domain discrimination training')
parser.add_argument('--warm-start-epochs', type=int, default=0, help='Epochs for domain discrimination training')
parser.add_argument('--epochs', type=int, default=5000, help='Epochs for the specified training algorithm')
parser.add_argument('--seed', type=int, default=42, help='Seed')
parser.add_argument('--alpha', type=float, default=0.5, help='Mixture proportion in unlabeled')
parser.add_argument('--beta', type=float, default=0.5, help='Proportion of labeled in total data ')
parser.add_argument('--log-dir', type=str, default='logging_accuracy', help='Dir for logging accuracies')
parser.add_argument('--data-dir', type=str, default='data', help='Data directory')
parser.add_argument('--optimizer', type=str, default='SGD', help='Optimizer used')
args = parser.parse_args()
torch.manual_seed(args.seed)
torch.cuda.manual_seed(args.seed)
np.random.seed(args.seed)
random.seed(args.seed)
print(args)
net_type = args.net_type
device = 'cuda' if torch.cuda.is_available() else 'cpu'
train_method = args.train_method
data_type = args.data_type
## Train set for positive and unlabeled
alpha = args.alpha
beta = args.beta
warm_start = args.warm_start
warm_start_epochs = args.warm_start_epochs
batch_size=args.batch_size
epochs=args.epochs
log_dir=args.log_dir + "/" + data_type +"/"
optimizer_str=args.optimizer
alpha_estimate=0.0
show_bar = False
use_alpha = False
data_dir = args.data_dir
estimate_alpha = args.estimate_alpha
if train_method == "TEDn":
use_alpha=True
#################
if not os.path.exists(log_dir):
os.makedirs(log_dir)
timestr = time.strftime("%Y%m%d-%H%M%S")
file_name = log_dir + "{}_{}_{}_{}_{}_{}_{}_{}_{}".format(train_method, net_type, args.seed, epochs, warm_start_epochs, args.lr, args.wd, alpha, beta) + "_" + timestr
outfile= open(file_name, 'w')
## Obtain dataset
if train_method=='PN':
u_trainloader, u_validloader, net= get_PN_dataset(data_dir, data_type,net_type, device, alpha, beta, batch_size)
else:
p_trainloader, u_trainloader, p_validloader, u_validloader, net, X, Y, p_validdata, u_validdata, u_traindata = \
get_dataset(data_dir, data_type,net_type, device, alpha, beta, batch_size)
train_pos_size= len(X)
train_unlabeled_size= len(Y)
valid_pos_size= len(p_validdata)
valid_unlabeled_size= len(u_validdata)
if device.startswith('cuda'):
net = torch.nn.DataParallel(net)
cudnn.benchmark = True
criterion = nn.CrossEntropyLoss()
if optimizer_str=="SGD":
optimizer = optim.SGD(net.parameters(), lr=args.lr, momentum=args.momentum, weight_decay=args.wd)
elif optimizer_str=="Adam":
optimizer = optim.Adam(net.parameters(), lr=args.lr,weight_decay=args.wd)
elif optimizer_str=="AdamW":
optimizer = AdamW(net.parameters(), lr=args.lr)
## Train in the begining for warm start
if warm_start and train_method=="TEDn":
outfile.write("Warm_start: \n")
for epoch in range(warm_start_epochs):
train_acc = train(epoch, net, p_trainloader, u_trainloader, \
optimizer=optimizer, criterion=criterion, device=device, show_bar=show_bar)
valid_acc = validate(epoch, net, u_validloader, \
criterion=criterion, device=device, threshold=0.5*beta/(beta + (1-beta)*alpha),show_bar=show_bar)
if estimate_alpha:
pos_probs = p_probs(net, device, p_validloader)
unlabeled_probs, unlabeled_targets = u_probs(net, device, u_validloader)
our_mpe_estimate, _, _ = BBE_estimator(pos_probs, unlabeled_probs, unlabeled_targets)
dedpul_estimate, dedpul_probs = dedpul(pos_probs, unlabeled_probs,unlabeled_targets)
EN_estimate= estimator_CM_EN(pos_probs, unlabeled_probs[:,0])
scott_mpe_estimator = scott_estimator(pos_probs, unlabeled_probs)
dedpul_accuracy = dedpul_acc(dedpul_probs,unlabeled_targets )*100.0
alpha_estimate =our_mpe_estimate
if estimate_alpha:
outfile.write("{}, {}, {}, {}, {}, {}, {}, {}\n".format(epoch, train_acc, valid_acc, dedpul_accuracy,\
alpha_estimate, dedpul_estimate, EN_estimate, scott_mpe_estimator) )
outfile.flush()
else:
outfile.write("{}, {}, {}\n".format(epoch, train_acc, valid_acc))
outfile.flush()
outfile.write("Algo_training: \n")
if train_method=='PvU':
for epoch in range(epochs):
if use_alpha:
alpha_used = alpha_estimate
else:
alpha_used = alpha
train_acc = train(epoch, net, p_trainloader, u_trainloader, \
optimizer=optimizer, criterion=criterion, device=device,show_bar=show_bar)
valid_acc = validate(epoch, net, u_validloader, \
criterion=criterion, device=device, threshold=0.5*beta/(beta + (1-beta)*alpha_used),show_bar=show_bar)
if estimate_alpha:
pos_probs = p_probs(net, device, p_validloader)
unlabeled_probs, unlabeled_targets = u_probs(net, device, u_validloader)
scott_mpe_estimator = scott_estimator(pos_probs, unlabeled_probs)
our_mpe_estimate, _, _ = BBE_estimator(pos_probs, unlabeled_probs, unlabeled_targets)
dedpul_estimate, dedpul_probs = dedpul(pos_probs, unlabeled_probs,unlabeled_targets)
EN_estimate= estimator_CM_EN(pos_probs, unlabeled_probs[:,0])
dedpul_accuracy = dedpul_acc(dedpul_probs,unlabeled_targets )*100.0
alpha_estimate =our_mpe_estimate
outfile.write("{}, {}, {}, {}, {}, {}, {}, {}\n".format(epoch, train_acc, valid_acc, dedpul_accuracy,\
alpha_estimate, dedpul_estimate, EN_estimate, scott_mpe_estimator) )
outfile.flush()
else:
outfile.write("{}, {}, {}\n".format(epoch, train_acc, valid_acc))
outfile.flush()
elif train_method=='CVIR' or train_method=="TEDn":
alpha_used = alpha_estimate
for epoch in range(epochs):
if use_alpha:
alpha_used = alpha_estimate
else:
alpha_used = alpha
keep_samples, neg_reject = rank_inputs(epoch, net, u_trainloader, device,\
alpha_used, u_size=train_unlabeled_size)
train_acc = train_PU_discard(epoch, net, p_trainloader, u_trainloader,\
optimizer, criterion, device, keep_sample=keep_samples,show_bar=show_bar)
valid_acc = validate(epoch, net, u_validloader, \
criterion=criterion, device=device, threshold=0.5,show_bar=show_bar)
if estimate_alpha:
pos_probs = p_probs(net, device, p_validloader)
unlabeled_probs, unlabeled_targets = u_probs(net, device, u_validloader)
our_mpe_estimate, _, _ = BBE_estimator(pos_probs, unlabeled_probs, unlabeled_targets)
dedpul_estimate, dedpul_probs = dedpul(pos_probs, unlabeled_probs,unlabeled_targets)
EN_estimate= estimator_CM_EN(pos_probs, unlabeled_probs[:,0])
dedpul_accuracy = dedpul_acc(dedpul_probs,unlabeled_targets )*100.0
alpha_estimate =our_mpe_estimate
outfile.write("{}, {}, {}, {}, {}, {}, {}, {}\n".format(epoch, train_acc, valid_acc, dedpul_accuracy,\
alpha_estimate, dedpul_estimate, EN_estimate, scott_mpe_estimator) )
outfile.flush()
else:
outfile.write("{}, {}, {}\n".format(epoch, train_acc, valid_acc))
outfile.flush()
elif train_method=='uPU':
for epoch in range(epochs):
train_acc = train_PU_unbiased(epoch, net, p_trainloader, u_trainloader,\
optimizer, criterion, device, alpha, logistic=(not args.sigmoid_loss), show_bar=show_bar)
valid_acc = validate(epoch, net, u_validloader, \
criterion=criterion, device=device, threshold=0.5, logistic=(not args.sigmoid_loss), show_bar=show_bar)
outfile.write("{}, {}, {}\n".format(epoch, train_acc, valid_acc))
outfile.flush()
elif train_method=='nnPU':
for epoch in range(epochs):
train_acc = train_PU_nn_unbiased(epoch, net, p_trainloader, u_trainloader,\
optimizer, criterion, device, alpha, logistic=(not args.sigmoid_loss),show_bar=show_bar)
valid_acc = validate(epoch, net, u_validloader, \
criterion=criterion, device=device, threshold=0.5,logistic=(not args.sigmoid_loss), show_bar=show_bar)
outfile.write("{}, {}, {}\n".format(epoch, train_acc, valid_acc))
outfile.flush()
elif train_method=="PN":
for epoch in range(epochs):
train_acc = train_PN(epoch, net, u_trainloader, \
optimizer=optimizer, criterion=criterion, device=device, show_bar=False)
valid_acc = validate(epoch, net, u_validloader, \
criterion=criterion, device=device, threshold=0.5, show_bar=False)
outfile.write("{}, {}, {}\n".format(epoch, train_acc, valid_acc))
outfile.flush()
elif train_method=="TiCE" or train_method=="KM":
print("here")
Y_train = u_validdata.data.reshape(len(u_validdata.data), -1)
X_train = p_validdata.data.reshape(len(p_validdata.data), -1)
X = np.concatenate((X,X_train), axis=0)
Y = np.concatenate((Y,Y_train), axis=0)
if train_method=="KM":
print(KM_estimate(X,Y,data_type))
else:
print(TiCE_estimate(X,Y,data_type))
outfile.close()