def forward(self, x, label=None): seg1 = x[:, 1, :] seg2 = x[:, 0, :] K = seg1.size()[0] cos_sim_matrix = F.cosine_similarity( seg1.unsqueeze(-1), seg2.unsqueeze(-1).transpose(0, 2)) torch.clamp(self.w, 1e-6) cos_sim_matrix1 = cos_sim_matrix * self.w + self.b cos_sim_matrix2 = cos_sim_matrix.transpose(0, 1) * self.w + self.b label = torch.from_numpy(numpy.asarray(range(0, K))).cuda() nloss1 = self.criterion(cos_sim_matrix1, label) nloss2 = self.criterion(cos_sim_matrix2, label) nloss = (nloss1 + nloss2) / 2 prec1, _ = accuracy(cos_sim_matrix1.detach().cpu(), label.detach().cpu(), topk=(1, 5)) prec2, _ = accuracy(cos_sim_matrix2.detach().cpu(), label.detach().cpu(), topk=(1, 5)) prec = (prec1 + prec2) / 2 return nloss, prec
def main(): # Reads .csv files from input data folders. # Generated [FOLDER]_cleaned_ALL_Pilot_[DATE].csv and [FOLDER]_master_list.csv cleanAll() # Reads from [FOLDER]_cleaned_ALL_Pilot_[DATE].csv # Generates [FOLDER]_CALL_Accuracy_[DATE].csv accuracy()
def eval_accuracies(self): print('Predicting training set...') ptrain = self.m.predict(self.ds.xtrain, 1, False) print('Predicting test set...') ptest = self.m.predict(self.ds.xtest, 1, False) print('Watching train heat maps...') acctrain = accuracy.accuracy(self.ds.ytrain, ptrain) print('Watching test heat maps...') acctest = accuracy.accuracy(self.ds.ytest, ptest) return { 'acctrain': acctrain, 'acctest': acctest, }
def forward(self, x, label=None): assert x.size()[0] == label.size()[0] assert x.size()[1] == self.in_feats # cos(theta) cosine = F.linear(F.normalize(x), F.normalize(self.weight)) # cos(theta + m) sine = torch.sqrt((1.0 - torch.pow(cosine, 2)).clamp(0, 1)) phi = cosine * self.cos_m - sine * self.sin_m if self.easy_margin: phi = torch.where(cosine > 0, phi, cosine) else: phi = torch.where((cosine - self.th) > 0, phi, cosine - self.mm) #one_hot = torch.zeros(cosine.size(), device='cuda' if torch.cuda.is_available() else 'cpu') one_hot = torch.zeros_like(cosine) one_hot.scatter_(1, label.view(-1, 1), 1) output = (one_hot * phi) + ((1.0 - one_hot) * cosine) output = output * self.s loss = self.ce(output, label) prec1, _ = accuracy(output.detach().cpu(), label.detach().cpu(), topk=(1, 5)) return loss, prec1
def test(model, path): triplets = load_resnet(path) #print(triplets[0:3]) new_triplets = triplets[:25] good_pic_batch = np.array([val[1] for val in new_triplets]) bad_pic_batch = np.array([val[2] for val in new_triplets]) caption_batch = np.array([val[0] for val in new_triplets]) print(good_pic_batch.shape, caption_batch.shape) good_pic_pred = model(good_pic_batch) bad_pic_pred = model(bad_pic_batch) good_pic_pred = good_pic_pred / mg.sqrt(mg.sum(mg.power(good_pic_pred, 2))) bad_pic_pred = bad_pic_pred / mg.sqrt((mg.sum(mg.power(bad_pic_pred, 2)))) print(good_pic_pred.shape) # good_pic_pred = good_pic_pred.reshape(1600, 1, 1) # bad_pic_pred = bad_pic_pred.reshape(1600, 1, 1) # caption_batch = caption_batch.reshape(1600, 1, 1) Sgood = (good_pic_pred * caption_batch).sum(axis=-1) Sbad = (bad_pic_pred * caption_batch).sum(axis=-1) print(Sgood.shape, Sbad.shape) # Sgood = Sgood.reshape(32, 50) # Sbad = Sbad.reshape(32, 50) #loss = margin_ranking_loss(Sgood, Sbad, 1, 0.1) acc = accuracy(Sgood.flatten(), Sbad.flatten()) print(acc)
def forward(self, x, label=None): x = self.fc(x) nloss = self.criterion(x, label) prec1, _ = accuracy(x.detach().cpu(), label.detach().cpu(), topk=(1, 5)) return nloss, prec1
def sync_loss(self, out_v, out_a, criterion): batch_size = out_a.size()[0] time_size = out_a.size()[2] label = torch.arange(time_size).cuda() nloss = 0 prec1 = 0 for ii in range(0, batch_size): ft_v = out_v[[ii], :, :].transpose(2, 0) ft_a = out_a[[ii], :, :].transpose(2, 0) output = F.cosine_similarity( ft_v.expand(-1, -1, time_size), ft_a.expand(-1, -1, time_size).transpose( 0, 2)) * self.__L__.wC + self.__L__.bC p1, p5 = accuracy(output.detach().cpu(), label.detach().cpu(), topk=(1, 5)) nloss += criterion(output, label) prec1 += p1 nloss = nloss / batch_size prec1 = prec1 / batch_size return nloss, prec1
def forward(self, input, label=None): # normalize features x = F.normalize(input) # normalize weights W = F.normalize(self.W) # dot product logits = F.linear(x, W) if label is None: return logits # feature re-scale theta = torch.acos(torch.clamp(logits, -1.0 + 1e-7, 1.0 - 1e-7)) one_hot = torch.zeros_like(logits) one_hot.scatter_(1, label.view(-1, 1).long(), 1) with torch.no_grad(): B_avg = torch.where(one_hot < 1, torch.exp(self.s * logits), torch.zeros_like(logits)) B_avg = torch.sum(B_avg) / input.size(0) # print(B_avg) theta_med = torch.median(theta[one_hot == 1]) self.s = torch.log(B_avg) / torch.cos( torch.min(math.pi / 4 * torch.ones_like(theta_med), theta_med)) output = self.s * logits loss = self.ce(output, label) prec1, _ = accuracy(output.detach().cpu(), label.detach().cpu(), topk=(1, 5)) return loss, prec1
def main(): # ******************************** Part I using k-fold cross validation on the data set ************************** # ******************************** Read data from files *********************************************************** # Get the required data from the file. data_set, features, num_of_features = read_data.read_from_train_file('dataset.txt') # ******************************** K cross validation ************************************************************* # ***************************************************************************************************************** # ***************************************************************************************************************** # K cross validation of the data. # The data is shuffled and split into k chunks. # One chunk is set to be the test set and the rest are mixed to be the training set. train, test = k_cross_validation.data_cross_validation(5, data_set) # Initialize the features. # Cross validation - send the training set. utility.create_feature_dictionaries(features, train) # ******************************** Decision Tree ****************************************************************** # Create the model tree_model = decision_tree.DecisionTree(num_of_features, utility.all_feature_types) # Create the root. # Cross validation - send the training set. tree_root = tree_model.create_tree_root(train, list(utility.all_feature_types.keys()), tree_model.majority_classification(train), 0) # Create the tree. tree = decision_tree.Tree(tree_root) # Run the algorithm on the data set. # Cross validation - send testing set. tree_results = tree_model.classify(test, tree) # Create the tree string. tree_string = tree.create_tree_string(tree_root) # Write it to a file. with open("tree.txt", 'w') as f: f.write(tree_string) # ******************************** KNN **************************************************************************** # Create the model. knn_model = k_nearest_neightbors.KNearestNeighbors(5, num_of_features) # Run the algorithm and get the results. # Cross validation - send training and test set. knn_results = knn_model.classify(train, test) # ******************************** Naives Bayes ****************************************************************** # Create the model. bayes_model = naive_bayes.NaiveBayes(num_of_features) # Run the algorithm and get the results. # Cross validation - send training and test set. bayes_results = bayes_model.classify(train, test) # ******************************** Accuracy ********************************************************************** # Call the accuracy function, send the test set and algorithm results to compare and write results to a file. accuracy.accuracy(test, tree_results, knn_results, bayes_results)
def evaluate(dataset, model, num_batch, batch_size): total_acc = 0 for (inputs, label) in dataset: pred = model(inputs) pred_id = tf.argmax(pred, axis=1) acc = accuracy(pred_id, label) total_acc += acc print("Accuracy On Validation Set: {}".format(total_acc / num_batch))
def main(): # Test case 1 print("\nTest case 1\n") perceptron = construct_perceptron([-1, 3], 2) inputs = [[1, -1], [2, 1], [3, 1], [-1, -1]] targets = [0, 1, 1, 0] print(accuracy(perceptron, inputs, targets))
def analytic_update(seq, snaps, num_time_steps, hops, alpha, method): if method == 'PageRank': input_rank = np.array(seq[snaps].pr.T) elif method == 'GammaPageRank': input_rank = np.array(seq[snaps].gpr) err_magnitude = [] err_degrees = [] predictions = [] for snapshot in range(snaps, num_time_steps): ####################### # update distribution local on the perturbation if method == 'PageRank': update_dist = (1 - alpha) * (seq[snapshot + 1].P.T - seq[snapshot].P.T).dot(input_rank) elif method == 'GammaPageRank': mu = alpha / (1 - alpha) psi = -10 / (2 * mu + 10) rho = (2 * mu) / (2 * mu + 10) update_dist = psi * (seq[snapshot + 1].Op_shift - seq[snapshot].Op_shift).dot(input_rank) ####################### # diffuse the initial update distribution tmp_dif = np.array(update_dist) man_up = np.array(update_dist) if method == 'PageRank': for k in range(hops - 1): tmp_dif = (1 - alpha) * (seq[snapshot + 1].P.T).dot(tmp_dif) man_up += tmp_dif elif method == 'GammaPageRank': for k in range(hops - 1): tmp_dif = psi * (seq[snapshot + 1].Op_shift).dot(tmp_dif) man_up += tmp_dif ####################### # update the rankings updated_rankings = np.array(input_rank + man_up) ####################### # compute the error err_mag, err_deg = accuracy(updated_rankings, seq, snapshot, method) err_magnitude.append(err_mag) err_degrees.append(err_deg) ####################### # set the prediction as the input of the new graph input_rank = np.array(updated_rankings) ####################### # store the prediction predictions.append(updated_rankings) return err_magnitude, err_degrees, predictions
def fit_predict(x_train, y_train, alpha, EPS, max_iter, x_val, y_val, x_others, y_others): ''' Fit a linear regression model and classify the training and the validation set. ''' # Train the model theta0 = np.ones(1025) * 0.01 theta, cost, iters = grad_descent.grad_descent(f, df, x_train.T, y_train.T, theta0, alpha, EPS, max_iter) # Performance of the model on the training set y_pred = classify(x_train.T, theta.T) acc_train = accuracy.accuracy(y_train.T, y_pred.T) # Performance of the model on the validation set y_pred = classify(x_val.T, theta.T) acc_val = accuracy.accuracy(y_val.T, y_pred.T) # Performance of the model on the other 6 actors y_pred = classify(x_others.T, theta.T) acc_others = accuracy.accuracy(y_others.T, y_pred.T) return acc_train, acc_val, acc_others
def main(): training_set, test_set = data.load_dataset('formatted_file.data') predictions = [] k = 3 for x in range(len(test_set)): neighbors = ns.get_neighbors(training_set, test_set[x], k) result = rs.get_response(neighbors) predictions.append(result[0][0]) print('> predicted = ' + repr(result) + ', actual = ' + repr(test_set[x][-1])) accuracy = acc.accuracy(test_set, predictions) print('Accuracy: ' + repr(accuracy) + '%')
def forward(self, out_anchor, out_positive, label=None): stepsize = out_anchor.size()[0] n_q = out_positive.size(0) // stepsize output = torch.matmul(out_positive, F.normalize(out_anchor, dim=-1).T) #output = -1 * (F.pairwise_distance(out_positive.unsqueeze(-1).expand(-1,-1,stepsize*n_q),out_anchor.unsqueeze(-1).expand(-1,-1,stepsize*n_q).transpose(0,2))**2) label = torch.from_numpy(numpy.asarray(range( 0, stepsize))).repeat(n_q).cuda() nloss = self.criterion(output, label) prec1, _ = accuracy(output.detach().cpu(), label.detach().cpu(), topk=(1, 5)) return nloss, prec1
def forward(self, x, label=None): assert x.size()[1] >= 2 out_anchor = torch.mean(x[:,1:,:],1) out_positive = x[:,0,:] stepsize = out_anchor.size()[0] output = -1 * (F.pairwise_distance(out_positive.unsqueeze(-1),out_anchor.unsqueeze(-1).transpose(0,2))**2) label = torch.from_numpy(numpy.asarray(range(0,stepsize))).cuda() nloss = self.criterion(output, label) prec1, _ = accuracy(output.detach().cpu(), label.detach().cpu(), topk=(1, 5)) return nloss, prec1
def no_update(seq, snaps, num_time_steps, method): err_magnitude = [] err_degrees = [] predictions = [] for snapshot in range(snaps, num_time_steps): if method == 'PageRank': updated_rankings = np.array(seq[snaps].pr.T) elif method == 'GammaPageRank': updated_rankings = np.array(seq[snaps].gpr) err_mag, err_deg = accuracy(seq[snaps].pr.T, seq, snapshot, method) err_magnitude.append(err_mag) err_degrees.append(err_deg) return err_magnitude, err_degrees
def sync_loss(self, out_v, out_a, criterion, L, dis2=False): batch_size = out_a.size()[0] feature_size = out_a.size()[1] time_size = out_a.size()[2] merge_size = (time_size - 1) // 3 + 1 label = torch.arange(merge_size).cuda() nloss = 0 prec1 = 0 for ii in range(0, batch_size): ft_v = out_v[[ii], :, :].transpose(2, 0) # ft_v = (15, 1024, 1) ft_a = out_a[[ii], :, :].transpose(2, 0) # ft_a = (15, 1024, 1) ft_v_merge = torch.zeros( ((time_size - 1) // 3 + 1, feature_size, 1), dtype=torch.float32).cuda() ft_a_merge = torch.zeros( ((time_size - 1) // 3 + 1, feature_size, 1), dtype=torch.float32).cuda() for itime in range(0, time_size): ft_v_merge[(itime - 1) // 3] += ft_v[itime] ft_a_merge[(itime - 1) // 3] += ft_a[itime] # output = F.cosine_similarity(ft_v.expand(-1, -1, time_size), # ft_a.expand(-1, -1, time_size).transpose(0, 2)) \ # *self.__L__.wC+self.__L__.bC output = F.cosine_similarity(ft_v_merge.expand(-1, -1, merge_size), ft_a_merge.expand(-1, -1, merge_size).transpose(0, 2)) \ *L.wC+L.bC if dis2: dumb_score = output.std() return dumb_score # output = (15, 15) p1 = accuracy(output.detach().cpu(), label.detach().cpu(), topk=(1, ))[0] nloss += criterion(output, label) prec1 += p1 nloss = nloss / batch_size prec1 = prec1 / batch_size return nloss, prec1
def forward(self, x, label=None): assert x.size()[1] >= 2 out_anchor = torch.mean(x[:,1:,:],1) out_positive = x[:,0,:] stepsize = out_anchor.size()[0] cos_sim_matrix = F.cosine_similarity(out_positive.unsqueeze(-1),out_anchor.unsqueeze(-1).transpose(0,2)) torch.clamp(self.w, 1e-6) cos_sim_matrix = cos_sim_matrix * self.w + self.b label = torch.from_numpy(numpy.asarray(range(0,stepsize))).cuda() nloss = self.criterion(cos_sim_matrix, label) prec1, _ = accuracy(cos_sim_matrix.detach().cpu(), label.detach().cpu(), topk=(1, 5)) return nloss, prec1
def evaluate(val_ldr, metric): net.eval() total_iou = 0 total_acc = 0 with torch.no_grad(): for image, mask, target in val_ldr: bs = len(image) logit, _, _, _, _, _, logit_image = net(image) logit = logit[:, :, 14:-13, 14:-13] total_acc += accuracy(logit_image, target).data total_iou += metric(logit, mask).data return ( total_acc / val_size, total_iou / val_size )
def forward(self, x, label=None): assert x.size()[0] == label.size()[0] assert x.size()[1] == self.in_feats x_norm = torch.norm(x, p=2, dim=1, keepdim=True).clamp(min=1e-12) x_norm = torch.div(x, x_norm) w_norm = torch.norm(self.W, p=2, dim=0, keepdim=True).clamp(min=1e-12) w_norm = torch.div(self.W, w_norm) costh = torch.mm(x_norm, w_norm) label_view = label.view(-1, 1) if label_view.is_cuda: label_view = label_view.cpu() delt_costh = torch.zeros(costh.size()).scatter_(1, label_view, self.m) if x.is_cuda: delt_costh = delt_costh.cuda() costh_m = costh - delt_costh costh_m_s = self.s * costh_m loss = self.ce(costh_m_s, label) prec1, _ = accuracy(costh_m_s.detach().cpu(), label.detach().cpu(), topk=(1, 5)) return loss, prec1
def train(dataset_train, dataset_val, num_batch, epochs, model, optimizer, loss_function, batch_size): for epoch in range(epochs): total_loss = 0 total_acc = 0 for batch, (inputs, label) in enumerate(dataset_train): with tf.GradientTape() as tape: pred = model(inputs) loss = loss_function(label, pred) total_loss += loss pred_id = tf.argmax(pred, axis=1) acc = accuracy(pred_id, label) total_acc += acc variables = model.variables grad = tape.gradient(loss, variables) optimizer.apply_gradients(zip(grad, variables)) print("Epoch: {}/{}, Loss: {}, Accuracy: {}".format( epoch, epochs, total_loss / num_batch, total_acc / num_batch)) evaluate(dataset_val, model, num_batch, batch_size)
def model_testing(model_eval, seq, snaps, num_time_steps, hops, method): err_magnitude = [] err_degrees = [] predictions = [] if method == 'PageRank': input_rank = np.array(seq[snaps].pr.T) elif method == 'GammaPageRank': input_rank = np.array(seq[snaps].gpr) for snapshot in range(snaps, num_time_steps): ####################### # compute feature vector feat_test_data, idx_to_update = prepare_data_test( input_rank, seq, snapshot, method, hops) ####################### # predict new rank model_eval.eval() predict = model_eval(feat_test_data) ####################### # update rankings updated_rankings = np.array(input_rank) updated_rankings[idx_to_update] = predict.detach().numpy() ####################### # evaluate accuracy of prediction err_mag, err_deg = accuracy(updated_rankings, seq, snapshot, method) err_magnitude.append(err_mag) err_degrees.append(err_deg) ####################### # set the prediction as the input of the new graph input_rank = np.array(updated_rankings) ####################### # store the prediction predictions.append(updated_rankings) return err_magnitude, err_degrees, predictions
def main(args): 'Tests accuracy of logistic regression in predicting if candies are chocolate' alpha, regParam, iterations = _getParams(args) #Regression on full data set fullDesign, fullLabels = processData('data/candy-data.csv') fullTheta, fullAccuracy = _runLogRegression(fullDesign, fullLabels, alpha, regParam, iterations) #Regression on first half of data set halfDesign, halfLabels = processData('data/firstHalf-candy-data.csv') halfTheta, halfAccuracy = _runLogRegression(halfDesign, halfLabels, alpha, regParam, iterations) #Test accuracy of params trained on first half on second half secondHalfDesign, secondHalfLabels = processData( 'data/secondHalf-candy-data.csv') secondHalfAccuracy = accuracy(logHypo(halfTheta, secondHalfDesign), secondHalfLabels) _displayResults(fullAccuracy, halfAccuracy, secondHalfAccuracy)
def sync_loss(self, out_v, out_a, criterion): batch_size = out_a.size()[0] time_size = out_a.size()[2] label = torch.arange(time_size, device='cuda').repeat_interleave(batch_size) # BxCxT => # TxCxB ft_a = out_a.transpose(2, 0) ft_v = out_v.transpose(2, 0) ft_a = ft_a.unsqueeze(-1).expand(-1, -1, -1, time_size) ft_v = ft_v.unsqueeze(-1).expand(-1, -1, -1, time_size).transpose(0, 3) # output has TxBxT output = F.cosine_similarity(ft_a, ft_v) output = output.reshape(time_size * batch_size, time_size) p1, p5 = accuracy(output, label, topk=(1, 5)) nloss = criterion(output, label) return nloss, p1
def main3(): f = h5py.File('MNIST6000.h5','r') X = f.get('/train/data')[:,:] / 255. V = f.get('/test/data')[:,:] / 255. lg = f.get('/test/target')[:] f.close() acc = 0.0 t = 0.0 accl = list() for i in xrange(40): s = '{},{},{},{},{},{}\n' ok = OKMF(2000,10,100,2,0.8,0.1,0.3,'rbf',gamma=(2.0**-9-0)) t0 = clock() ok.fit(X) t1 = clock() #print 'OKMF',i lp = np.argmax(ok.predictH(V).T,axis=1) aux = accuracy(lp,lg,10) accl.append(aux) print 'OKMF',i,aux # print aux acc += aux # print t1-t0 t += t1-t0 f = open('ResultsCRBF.csv','w') f.write('Budget,Gamma,Lambda,Alpha,Acc,time\n') s = '{},{},{},{},{},{}\n' print acc/40.0 val = (1000,0.01,0.01,0.8) + (acc/40.0,t/40.0) f.write(s.format(*val)) f.close() f = open('series2000K.txt','w') f.write(str(accl)+'\n') f.close() acc = 0.0 accl = list() """
def calcAccuracy( model, word_vocab : Vocabulary, char_vocab : Vocabulary, tag_vocab : Vocabulary, dev_words : List[List[int]], dev_tags : List[List[int]], ): _device = model._device model._device = "cpu" model = model.to(torch.device(model._device)) hyp = test( model, word_vocab, char_vocab, tag_vocab, dev_words, to_stdout=False, ) for h in hyp[:5]: print(" ".join(h)) """ ref = [ tag_vocab.toTagTokens(t) for t in dev_tags ] """ p, r, f1 = accuracy(dev_tags, hyp) print() model._device = _device modle = model.to(torch.device(model._device)) return p, r, f1
def evaluate(args): model = Fairnas[args.model](s_r=args.se_ratio) device = torch.device(args.device) if args.device == 'cuda': model.cuda() state = torch.load(f'{args.model_path}', map_location=device) model.load_state_dict(state) _input = torch.randn(1, 3, 224, 224).to(device) flops, params = profile(model, inputs=(_input,), verbose=False) print('Model: {}, params: {}M, flops: {}M'.format(args.model, params / 1e6, flops / 1e6)) model.eval() val_dataloader = get_imagenet_dataset(batch_size=args.batch_size, dataset_root=args.val_dataset_root, dataset_tpye="valid") print("Start to evaluate ...") total_top1 = 0.0 total_top5 = 0.0 total_counter = 0.0 for image, label in val_dataloader: image, label = image.to(device), label.to(device) result = model(image) top1, top5 = accuracy(result, label, topk=(1, 5)) if device.type == 'cuda': total_counter += image.cpu().data.shape[0] total_top1 += top1.cpu().data.numpy() total_top5 += top5.cpu().data.numpy() else: total_counter += image.data.shape[0] total_top1 += top1.data.numpy() total_top5 += top5.data.numpy() mean_top1 = total_top1 / total_counter mean_top5 = total_top5 / total_counter print('Evaluate Result: Total: %d\tmTop1: %.4f\tmTop5: %.6f' % (total_counter, mean_top1, mean_top5))
def forward(self, x, label=None): gsize = x.size()[1] centroids = torch.mean(x, 1) stepsize = x.size()[0] cos_sim_matrix = [] for ii in range(0, gsize): idx = [*range(0, gsize)] idx.remove(ii) exc_centroids = torch.mean(x[:, idx, :], 1) cos_sim_diag = F.cosine_similarity(x[:, ii, :], exc_centroids) cos_sim = F.cosine_similarity( x[:, ii, :].unsqueeze(-1).expand(-1, -1, stepsize), centroids.unsqueeze(-1).expand(-1, -1, stepsize).transpose(0, 2)) cos_sim[range(0, stepsize), range(0, stepsize)] = cos_sim_diag cos_sim_matrix.append(torch.clamp(cos_sim, 1e-6)) cos_sim_matrix = torch.stack(cos_sim_matrix, dim=1) torch.clamp(self.w, 1e-6) cos_sim_matrix = cos_sim_matrix * self.w + self.b label = torch.from_numpy(numpy.asarray(range(0, stepsize))).cuda() nloss = self.criterion( cos_sim_matrix.view(-1, stepsize), torch.repeat_interleave(label, repeats=gsize, dim=0).cuda()) prec1, _ = accuracy(cos_sim_matrix.view(-1, stepsize).detach().cpu(), torch.repeat_interleave(label, repeats=gsize, dim=0).detach().cpu(), topk=(1, 5)) return nloss, prec1
def train_network(self, loader=None, evalmode=None, alpC=1.0, alpI=1.0): print('Content loss %f Identity loss %f' % (alpC, alpI)) if evalmode: self.eval() else: self.train() # ==================== ==================== counter = 0 index = 0 loss = 0 eer = 0 top1_sy = 0 top1_id = 0 criterion = torch.nn.CrossEntropyLoss() stepsize = loader.batch_size label_id = torch.arange(stepsize).cuda() for data in loader: tstart = time.time() self.zero_grad() data_v, data_a = data # ==================== FORWARD PASS ==================== if evalmode: with torch.no_grad(): out_a, out_A = self.__S__.forward_aud(data_a.cuda()) out_v, out_V = self.__S__.forward_vid(data_v.cuda()) else: out_a, out_A = self.__S__.forward_aud(data_a.cuda()) out_v, out_V = self.__S__.forward_vid(data_v.cuda()) time_size = out_V.size()[2] ri = random.randint(0, time_size - 1) out_AA = torch.mean(out_A, 2, keepdim=True) out_VA = out_V[:, :, [ri]] # sync loss and accuracy nloss_sy, p1s = self.sync_loss(out_v, out_a, criterion) # identity loss and accuracy idoutput = F.cosine_similarity( out_VA.expand(-1, -1, stepsize), out_AA.expand(-1, -1, stepsize).transpose( 0, 2)) * self.__L__.wI + self.__L__.bI nloss_id = criterion(idoutput, label_id) p1i, p5i = accuracy(idoutput.detach().cpu(), label_id.detach().cpu(), topk=(1, 2)) # ==================== Divergence Loss ==================== nloss = alpC * nloss_sy + alpI * nloss_id if not evalmode: nloss.backward() self.__optimizer__.step() loss += nloss.detach().cpu() top1_sy += p1s[0] top1_id += p1i[0] counter += 1 telapsed = time.time() - tstart sys.stdout.write("\rProc (%d/%d): %.3fHz " % (index, loader.nFiles, stepsize / telapsed)) sys.stdout.write("Ls %.3f SYT1 %2.3f%% " % (loss / counter, top1_sy / counter)) sys.stdout.write("IDT1 %2.3f%% " % (top1_id / counter)) # ==================== CALCULATE LOSSES ==================== sys.stdout.write("Q:(%d/%d)" % (loader.qsize(), loader.maxQueueSize)) sys.stdout.flush() index += stepsize sys.stdout.write("\n") return (loss / counter, top1_id / counter)
def RBFTuning(dataset,k,budgets,Gammas,Lambdas,Alphas,Sigmas,runs,target='acc',l1=True,K=False): """ Performs a parameter tunning for OKMF using rbf kernels and random budgets. Parameters ---------- dataset : H5 File A file containing the dataset and labels for the clustering task. k : int The number of clusters to be found. budgets : Array like An array containing the size of budgets to be tuned. Gammas : array like An array containing the learning rates to be tuned. Lambdas : array like An array containing the W regularization parameters to be tuned. Alphas : array like An array containing the H regularization parameters to be tuned. Sigmas : array like An array containing the RBF's sigma parameters to be tuned. runs : int The number of runs to tune each parameter. target : string A string to select the measure to use in the parameter tuning The posible measures are: 'acc' or 'obj' l1 : boolean True if L1 Normalization is going to be used. Default True K : boolean True if KMeans budget is to be used. Default False. """ print 'RBF tuning' + dataset.filename # Dataset loading X = dataset.get('/data')[:,:] if l1: X = L1Normalization(X) LG = dataset.get('/labels')[:] n = len(budgets) # Budget size tuning averageAcc = np.zeros((n,)) averageObj = np.zeros((n,)) for i in xrange(n): print 'Tuning Budget %i of %i'%(i+1,n) budget = budgets[i] if K: KM = MiniBatchKMeans(n_clusters = budget) KM.fit(X) Budget = KM.cluster_centers_ accs = np.zeros((runs,)) objs = np.zeros((runs,)) for j in xrange(runs): ok = OKMF(budget,k,30,10,0.8,0.1,0.2,'rbf') try: if K: ok.fit(X,Budget=Budget) else: ok.fit(X) LF = np.argmax(ok.H,axis=0) if target == 'acc': accs[j] = accuracy(LF,LG) elif target == 'obj': objs[j] = ok.Error(X) except np.linalg.LinAlgError: print "LinAlgError found" if target == 'acc': accs[j] = float('-inf') elif target == 'obj': objs[j] = float('inf') del ok averageAcc[i] = np.average(accs) averageObj[i] = np.average(objs) # Tuned budget if target == 'acc': budget = budgets[np.argmax(averageAcc)] elif target == 'obj': budget = budgets[np.argmin(averageObj)] del averageAcc,averageObj Budget = None if K: KM = MiniBatchKMeans(n_clusters = budget) KM.fit(X) Budget = KM.cluster_centers_ # Gamma tuning n = len(Gammas) averageAcc = np.zeros((n,)) averageObj = np.zeros((n,)) for i in xrange(n): print 'Tuning Gamma %i of %i'%(i+1,n) Gamma = Gammas[i] accs = np.zeros((runs,)) objs = np.zeros((runs,)) for j in xrange(runs): ok = OKMF(budget,k,30,10,Gamma,0.1,0.2,'rbf') try: if K: ok.fit(X,Budget=Budget) else: ok.fit(X) LF = np.argmax(ok.H,axis=0) if target == 'acc': accs[j] = accuracy(LF,LG) elif target == 'obj': objs[j] = ok.Error(X) except np.linalg.LinAlgError: print "LinAlgError found" if target == 'acc': accs[j] = float('-inf') elif target == 'obj': objs[j] = float('inf') del ok averageAcc[i] = np.average(accs) averageObj[i] = np.average(objs) # Tuned Gamma if target == 'acc': Gamma = Gammas[np.argmax(averageAcc)] elif target == 'obj': Gamma = Gammas[np.argmin(averageObj)] del averageAcc,averageObj # Lambda tuning n = len(Lambdas) averageAcc = np.zeros((n,)) averageObj = np.zeros((n,)) for i in xrange(n): print 'Tuning Lambda %i of %i'%(i+1,n) Lambda = Lambdas[i] accs = np.zeros((runs,)) objs = np.zeros((runs,)) for j in xrange(runs): ok = OKMF(budget,k,30,10,Gamma,Lambda,0.2,'rbf') try: if K: ok.fit(X,Budget=Budget) else: ok.fit(X) LF = np.argmax(ok.H,axis=0) if target == 'acc': accs[j] = accuracy(LF,LG) elif target == 'obj': objs[j] = ok.Error(X) except np.linalg.LinAlgError: print "LinAlgError found" if target == 'acc': accs[j] = float('-inf') elif target == 'obj': objs[j] = float('inf') del ok averageAcc[i] = np.average(accs) averageObj[i] = np.average(objs) # Tuned Lamnda if target == 'acc': Lambda = Lambdas[np.argmax(averageAcc)] elif target == 'obj': Lambda = Lambdas[np.argmin(averageObj)] del averageAcc,averageObj # Alpha tuning n = len(Alphas) averageAcc= np.zeros((n,)) averageObj = np.zeros((n,)) for i in xrange(n): print 'Tuning Alpha %i of %i'%(i+1,n) Alpha = Alphas[i] accs = np.zeros((runs,)) objs = np.zeros((runs,)) for j in xrange(runs): ok = OKMF(budget,k,30,10,Gamma,Lambda,Alpha,'rbf') try: if K: ok.fit(X,Budget=Budget) else: ok.fit(X) LF = np.argmax(ok.H,axis=0) if target == 'acc': accs[j] = accuracy(LF,LG) elif target == 'obj': objs[j] = ok.Error(X) except np.linalg.LinAlgError: print "LinAlgError found" if target == 'acc': accs[j] = float('-inf') elif target == 'obj': objs[j] = float('inf') del ok averageAcc[i] = np.average(accs) averageObj[i] = np.average(objs) # Tuned Alpha if target == 'acc': Alpha = Alphas[np.argmax(averageAcc)] elif target == 'obj': Alpha = Alphas[np.argmin(averageObj)] del averageAcc,averageObj # Sigma tuning n = len(Sigmas) averageAcc= np.zeros((n,)) averageObj = np.zeros((n,)) for i in xrange(n): print 'Tuning Sigma %i of %i'%(i+1,n) Sigma = Sigmas[i] gam = 1.0 / (2.0 * (2.0**Sigma)**2.0) accs = np.zeros((runs,)) objs = np.zeros((runs,)) for j in xrange(runs): ok = OKMF(budget,k,30,10,Gamma,Lambda,Alpha,'rbf',gamma=gam) try: if K: ok.fit(X,Budget=Budget) else: ok.fit(X) LF = np.argmax(ok.H,axis=0) if target == 'acc': accs[j] = accuracy(LF,LG) elif target == 'obj': objs[j] = ok.Error(X) except np.linalg.LinAlgError: print "LinAlgError found" if target == 'acc': accs[j] = float('-inf') elif target == 'obj': objs[j] = float('inf') del ok averageAcc[i] = np.average(accs) averageObj[i] = np.average(objs) # Tuned Sigma if target == 'acc': Sigma = Sigmas[np.argmax(averageAcc)] elif target == 'obj': Sigma = Sigmas[np.argmin(averageObj)] del averageAcc,averageObj result = dict() result['budget'] = budget result['Gamma'] = Gamma result['Lambda'] = Lambda result['Alpha'] = Alpha result['Sigma'] = Sigma return result
args.hidden_size, len(tag_vocab.t2i.keys()), dropout=0.0, ) model.load_state_dict(torch.load(args.model_path, map_location=lambda x, loc: x)) model._device = "cpu" model.eval() sentences = [ [ args.delimiter.join(chunk.split(args.delimiter)[:-1]) for chunk in sentence.split() ] for sentence in open(args.test_path).readlines() ] hyp = test( model, word_vocab, char_vocab, tag_vocab, sentences, to_stdout=False, ) ref = [ [ chunk.split(args.delimiter)[-1] for chunk in sentence.split() ] for sentence in open(args.test_path).readlines() ] accuracy(ref, hyp)
def test_init(self): r = accuracy.accuracy() for model_name in r._models: self.assertIsInstance(r._models[model_name], SklearnClassifier) self.assertIsInstance(r._text_analysis, stylometry.Text_Analysis)
import ui import stylometry import model import accuracy print("Welcome to Who Wrote Me v0.1.0") text_analysis = stylometry.Text_Analysis() models = model.Models(text_analysis.text_features_library) accuracy = accuracy.accuracy() u = ui.Ui(models, text_analysis, accuracy)
def RBFExperiment(dataset,k,budget,Gamma,Lambda,Alpha,Sigma,runs,target='acc',l1=True,K=False): """ Performs an experiment using OKMF and returns the list of objectives or accuracies of each experiment. Parameters ---------- dataset : H5 dataset The dataset to use in the experiment k : int The number of latent topics of the KMF budget : int The size of the budget Gamma : double The Gamma constant to be used Lambda : double The Lambda constant to be used Alpha : double The Alpha constant to be used Sigma : duble The Sigma to be used runs : int The number of runs target : string A string to select the measure to use in The experiment. The posible measures are: 'acc' or 'obj' l1 : boolean True if L1 Normalization is going to be used. Default True K : boolean True if KMeans budget is to be used. Default False. Returns ------- result : ndarray An array representing the performance mesure for each run """ X = dataset.get('/data')[:,:] if l1: X = L1Normalization(X) LG = dataset.get('/labels')[:] measures = np.zeros((runs,)) gam = 1.0 / (2.0 * (2.0**Sigma)**2.0) ok = OKMF(budget,k,30,10,Gamma,Lambda,Alpha,'rbf',gamma=gam) Budget = None if K: KM = MiniBatchKMeans(n_clusters = budget) KM.fit(X) Budget = KM.cluster_centers_ for i in xrange(runs): try: if K: ok.fit(X,Budget=Budget) else: ok.fit(X) if target=='acc': LF = np.argmax(ok.H,axis=0) measures[i]=accuracy(LF,LG) elif target=='obj': measures[i]=ok.Error(X) except np.linalg.LinAlgError : if target=='acc': LF = np.argmax(ok.H,axis=0) measures[i]=0.0 elif target=='obj': measures[i]=float('inf') return measures
def experimentsAbalone(): budgets = [3,10,50,100,200,500,1000,1500,2000,3000,4000,4177] Sigmas = range(-10,5,1) Gammas = [1.0,0.9,0.8,0.7] Lambdas = [0.0,0.01,0.1,0.2,0.3,0.4] Alphas = [0.4,0.5,0.6,0.7] runs = 10 acc = np.zeros((runs,),dtype=np.float64) exRuns = 30 results = dict() f = File('../Datasets/abalone.h5','r') X = f.get('data')[:,:] lg = f.get('labels')[:] f.close() k = 3 # Without K-means budget for budget in budgets: print "Tuning with budget size = {}".format(budget) finalAcc = np.zeros((exRuns,),dtype=np.float64) finalTime = np.zeros((exRuns,),dtype=np.float64) accT = np.zeros((len(Gammas),),dtype=np.float64) for i in xrange(len(Gammas)): print "Tuning Gamma {} of {}".format(i+1,len(Gammas)) Gamma = Gammas[i] for j in xrange(runs): ok = OKMF(budget,k,50,4,Gamma,0.1,0.2,'rbf') try: ok.fit(X) lf = np.argmax(ok.H,axis=0) acc[j] = accuracy(lf,lg) except np.linalg.LinAlgError as er: print er.message acc[j] = 0.0 accT[i] = np.average(acc) Gamma = Gammas[np.argmax(accT)] accT = np.zeros((len(Lambdas),),dtype=np.float64) for i in xrange(len(Lambdas)): print "Tuning Lambda {} of {}".format(i+1,len(Lambdas)) Lambda = Lambdas[i] for j in xrange(runs): ok = OKMF(budget,k,50,4,Gamma,Lambda,0.2,'rbf') try: ok.fit(X) lf = np.argmax(ok.H,axis=0) acc[j] = accuracy(lf,lg) except np.linalg.LinAlgError as er: print er.message acc[j] = 0.0 accT[i] = np.average(acc) Lambda = Lambdas[np.argmax(accT)] accT = np.zeros((len(Alphas),),dtype=np.float64) for i in xrange(len(Alphas)): print "Tuning Alpha {} of {}".format(i+1,len(Alphas)) Alpha = Alphas[i] for j in xrange(runs): ok = OKMF(budget,k,50,4,Gamma,Lambda,Alpha,'rbf') try: ok.fit(X) lf = np.argmax(ok.H,axis=0) acc[j] = accuracy(lf,lg) except np.linalg.LinAlgError as er: print er.message acc[j] = 0.0 accT[i] = np.average(acc) Alpha = Alphas[np.argmax(accT)] accT = np.zeros((len(Sigmas),),dtype=np.float64) for i in xrange(len(Sigmas)): print "Tuning Sigma {} of {}".format(i+1,len(Sigmas)) Sigma = Sigmas[i] sigmaR = 1.0 / ((2.0 ** Sigma) ** 2.0) for j in xrange(runs): ok = OKMF(budget,k,50,4,Gamma,Lambda,Alpha,'rbf',gamma=sigmaR) try: ok.fit(X) lf = np.argmax(ok.H,axis=0) acc[j] = accuracy(lf,lg) except np.linalg.LinAlgError as er: print er.message acc[j] = 0.0 accT[i] = np.average(acc) Sigma = Sigmas[np.argmax(accT)] sigmaR = 1.0 / ((2.0 ** Sigma) ** 2.0) for i in xrange(exRuns): ok = OKMF(budget,k,50,4,Gamma,Lambda,Alpha,'rbf',gamma=sigmaR) try: t0 = clock() ok.fit(X) lf = np.argmax(ok.H,axis=0) t1 = clock() finalAcc[i] = accuracy(lf,lg) finalTime[i] = t1 - t0 except np.linalg.LinAlgError as er: print er.message finalAcc[i] = 0 finalTime[i] = float('inf') results[str(budget)]=finalAcc,finalTime return results