def train_and_save_scores(epoch): net.train() train_loss = 0 correct = 0 total = 0 abstain = 0 # for saving purpose global training_loss global train_abstained global train_acc global train_acc_without_abstained global train_f1score global train_f1score_without_abstained global train_predicted train_softmax_scores = [] total = 0 abstained = 0 loss_acc = np.array([]) batch_abstained = np.array([]) batch_true = np.array([]) batch_true_without_abstained = np.array([]) batch_f1score = np.array([]) batch_f1score_without_abstained = np.array([]) if args.dataset == 'mnist': if int(epoch/args.epdl) > 5 and int(epoch/args.epdl) <= 20: args.lr = 0.01 if int(epoch/args.epdl) > 20 and int(epoch/args.epdl) <=50: args.lr = 0.001 #optimizer = optim.SGD(net.parameters(), lr=cf.learning_rate(args.lr, epoch), momentum=0.9, optimizer = optim.SGD(net.parameters(), lr=args.lr, momentum=0.9, nesterov=args.nesterov, weight_decay=5e-4) print('\n=> Training Epoch #%d, LR=%.4f' %(epoch, args.lr)) else: #cifar 10/100/stl-10/tin200/fashion optimizer = optim.SGD(net.parameters(), lr=cf.learning_rate(args.lr, int(epoch/args.epdl)), momentum=0.9, weight_decay=5e-4,nesterov=args.nesterov) # optimizer = optim.Adam(net.parameters(), lr=cf.learning_rate(args.lr, int(epoch/args.epdl))) print('\n=> Training Epoch #%d, LR=%.4f' %(epoch, cf.learning_rate(args.lr, int(epoch/args.epdl)))) print('\n => Training Epoch #%d, LR=%.4f' %(epoch, cf.learning_rate(args.lr, epoch))) sys.stdout.flush() #pdb.set_trace() for batch_idx, (inputs, targets) in enumerate(trainloader): #print(dir(inputs.cuda)) #quit() optimizer.zero_grad() inputs, targets = Variable(inputs.to(device)), Variable(targets.to(device)) if (args.net_type == 'tsc-lstm'): # update input to match lstm inputs = inputs.view(-1, series_length, 1) # input dimensions for time series data set are 1 # print(inputs.shape) outputs = net(inputs) # Forward Propagation #pdb.set_trace() if args.loss_fn is None: loss = criterion(outputs, targets) else: loss = criterion(outputs, targets, epoch) # Loss loss.backward() # Backward Propagation optimizer.step() # Optimizer update train_loss += loss.data.item() _, predicted = torch.max(outputs.data, 1) this_batch_size =targets.size(0) total += this_batch_size correct += predicted.eq(targets.data).cpu().sum().data.item() abstained_now = predicted.eq(abstain_class_id).sum().data.item() abstain += abstained_now if total-abstain != 0: #pdb.set_trace() abst_acc = 100.*correct/(float(total-abstain)) else: abst_acc = 1. p_out = F.softmax(outputs,dim=1) #pdb.set_trace() total += p_out.size(0) _,predicted = torch.max(p_out.data,1) abstained += predicted.eq(abstain_class_id).sum().data.item() train_softmax_scores.append(p_out.data) loss_acc = np.append(loss_acc, loss.data.item()) f1score = f1_score(targets.cpu().numpy(), predicted.cpu().numpy(), average='weighted') # TODO: check if it works (get accuracy without abstension) batch_total = p_out.size(0) batch_abstained = np.append(batch_abstained, predicted.eq(abstain_class_id).sum().data.item() / batch_total) batch_true = np.append(batch_true, predicted.eq(targets.data).cpu().sum().data.item() / batch_total) batch_true_without_abstained = np.append(batch_true_without_abstained, abst_acc) batch_f1score = np.append(batch_f1score, f1score) non_abstained_index = np.invert(predicted.eq(abstain_class_id).cpu().detach().numpy()).nonzero()[0] f1score_without_abstained = f1_score(targets.cpu().numpy()[non_abstained_index], predicted.cpu().numpy()[non_abstained_index], average='weighted') batch_f1score_without_abstained = np.append(batch_f1score_without_abstained, f1score_without_abstained) #train_predicted = np.append(train_predicted, predicted.cpu()) #print('\r') #print('| Epoch [%3d/%3d] Iter[%3d/%3d]\t\tAbstained %d Abstention rate %.4f Cumulative Abstention Rate: %.4f Loss: %.4f Acc@1: %.3f%% Acc@2: %.3f%%' #%(epoch, num_epochs, batch_idx+1, # (len(trainset)//batch_size)+1, abstain, float(abstained_now)/this_batch_size, float(abstain)/total, loss.data.item(), 100.*correct/float(total), abst_acc)) #sys.stdout.flush() print('\r') print('| Epoch [%3d/%3d] Iter[%3d/%3d]\t\tAbstained %d Abstention rate %.4f Cumulative Abstention Rate: %.4f Loss: %.4f Acc@1: %.3f%% Acc@2: %.3f%%' %(epoch, num_epochs, batch_idx+1, (len(trainset)//batch_size)+1, abstain, float(abstained_now)/this_batch_size, float(abstain)/total, loss.data.item(), 100.*correct/float(total), abst_acc)) sys.stdout.flush() training_loss = np.append(training_loss, np.mean(loss_acc)) train_abstained = np.append(train_abstained, np.mean(batch_abstained)) train_acc = np.append(train_acc, np.mean(batch_true)) train_acc_without_abstained = np.append(train_acc_without_abstained, np.mean(batch_true_without_abstained)) train_scores = torch.cat(train_softmax_scores).cpu().numpy() train_f1score = np.append(train_f1score, np.mean(batch_f1score)) train_f1score_without_abstained = np.append(train_f1score_without_abstained, np.mean(batch_f1score_without_abstained)) if args.save_train_scores: print('Saving train softmax scores at Epoch %d' %(epoch)) fn = args.expt_name if args.expt_name else 'test' np.save(args.output_path+fn+"/"+ fn +".train_scores.epoch_"+str(epoch), train_scores) print("\n##### Epoch %d Train Abstention Rate at end of epoch %.4f" %(epoch, float(abstained)/total))
def train(epoch): net.train() train_loss = 0 correct = 0 total = 0 abstain = 0 if args.dataset == 'mnist': if int(epoch/args.epdl) > 5 and int(epoch/args.epdl) <= 20: args.lr = 0.01 if int(epoch/args.epdl) > 20 and int(epoch/args.epdl) <=50: args.lr = 0.001 #optimizer = optim.SGD(net.parameters(), lr=cf.learning_rate(args.lr, epoch), momentum=0.9, optimizer = optim.SGD(net.parameters(), lr=args.lr, momentum=0.9, nesterov=args.nesterov, weight_decay=5e-4) print('\n=> Training Epoch #%d, LR=%.4f' %(epoch, args.lr)) else: #cifar 10/100/stl-10/tin200/fashion optimizer = optim.SGD(net.parameters(), lr=cf.learning_rate(args.lr, int(epoch/args.epdl)), momentum=0.9, weight_decay=5e-4,nesterov=args.nesterov) print('\n=> Training Epoch #%d, LR=%.4f' %(epoch, cf.learning_rate(args.lr, int(epoch/args.epdl)))) #print('\n=> Training Epoch #%d, LR=%.4f' %(epoch, cf.learning_rate(args.lr, epoch))) #pdb.set_trace() for batch_idx, (inputs, targets) in enumerate(trainloader): #print(dir(inputs.cuda)) #quit() optimizer.zero_grad() inputs, targets = Variable(inputs), Variable(targets) if (args.net_type == 'tsc-lstm'): # update input to match lstm inputs = inputs.view(-1, series_length, 1) # input dimensions for time series data set are 1 outputs = net(inputs) # Forward Propagation #pdb.set_trace() if args.loss_fn is None: loss = criterion(outputs, targets) else: loss = criterion(outputs, targets, epoch) # Loss loss.backward() # Backward Propagation optimizer.step() # Optimizer update train_loss += loss.data.item() _, predicted = torch.max(outputs.data, 1) this_batch_size =targets.size(0) total += this_batch_size correct += predicted.eq(targets.data).cpu().sum().data.item() abstained_now = predicted.eq(abstain_class_id).sum().data.item() abstain += abstained_now if total-abstain != 0: #pdb.set_trace() abst_acc = 100.*correct/(float(total-abstain)) else: abst_acc = 1. sys.stdout.write('\r') sys.stdout.write('| Epoch [%3d/%3d] Iter[%3d/%3d]\t\tAbstained %d Abstention rate %.4f Cumulative Abstention Rate: %.4f Loss: %.4f Acc@1: %.3f%% Acc@2: %.3f%%' %(epoch, num_epochs, batch_idx+1, (len(trainset)//batch_size)+1, abstain, float(abstained_now)/this_batch_size, float(abstain)/total, loss.data.item(), 100.*correct/float(total), abst_acc)) sys.stdout.flush()
def train(epoch): net.train() train_loss = 0 correct = 0 total = 0 abstain = 0 if args.dataset == 'mnist': if int(epoch / args.epdl) > 5 and int(epoch / args.epdl) < 20: args.lr = 0.01 if int(epoch / args.epdl) >= 20: args.lr = 0.001 #optimizer = optim.SGD(net.parameters(), lr=cf.learning_rate(args.lr, epoch), momentum=0.9, optimizer = optim.SGD(net.parameters(), lr=args.lr, momentum=0.9, nesterov=args.nesterov, weight_decay=5e-4) print('\n=> Training Epoch #%d, LR=%.4f' % (epoch, args.lr)) else: #cifar 10/100/stl-10/tin200/fashion optimizer = optim.SGD(net.parameters(), lr=cf.learning_rate(args.lr, int(epoch / args.epdl)), momentum=0.9, weight_decay=5e-4, nesterov=args.nesterov) print('\n=> Training Epoch #%d, LR=%.4f' % (epoch, cf.learning_rate(args.lr, int(epoch / args.epdl)))) #print('\n=> Training Epoch #%d, LR=%.4f' %(epoch, cf.learning_rate(args.lr, epoch))) for batch_idx, (inputs, targets) in enumerate(trainloader): #print(type(inputs)) #print(dir(inputs.cuda)) #quit() if use_cuda: #pdb.set_trace() inputs, targets = inputs.cuda(cuda_device), targets.cuda( cuda_device) # GPU settings #inputs, targets = inputs.cuda(), targets.cuda() # GPU settings optimizer.zero_grad() inputs, targets = Variable(inputs), Variable(targets) outputs = net(inputs) # Forward Propagation if args.loss_fn is None: loss = criterion(outputs, targets) else: loss = criterion(outputs, targets, epoch) # Loss loss.backward() # Backward Propagation optimizer.step() # Optimizer update train_loss += loss.data.item() _, predicted = torch.max(outputs.data, 1) total += targets.size(0) correct += predicted.eq(targets.data).cpu().sum().data.item() abstain += predicted.eq(abstain_class_id).sum().data.item() if total - abstain != 0: #pdb.set_trace() abst_acc = 100. * correct / (float(total - abstain)) else: abst_acc = 1. sys.stdout.write('\r') sys.stdout.write( '| Epoch [%3d/%3d] Iter[%3d/%3d]\t\tAbstained %d Loss: %.4f Acc@1: %.3f%% Acc@2: %.3f%%' % (epoch, num_epochs, batch_idx + 1, (len(trainset) // batch_size) + 1, abstain, loss.data.item(), 100. * correct / float(total), abst_acc)) sys.stdout.flush()