Exemple #1
0
def get_acc(source_outputs,
            target_outputs,
            test_dict=None,
            alphas=None,
            just_S=False):
    Sf = np.zeros((len(source_outputs[0]), len(target_outputs[0])))
    list_S_numpy = []
    accs = ""
    for i in range(0, len(source_outputs)):
        S = torch.matmul(F.normalize(source_outputs[i]),
                         F.normalize(target_outputs[i]).t())
        S_numpy = S.detach().cpu().numpy()
        if test_dict is not None:
            if not just_S:
                acc = get_statistics(S_numpy, test_dict)
                accs += "Acc layer {} is: {:.4f}, ".format(i, acc)
        if alphas is not None:
            Sf += alphas[i] * S_numpy
        else:
            Sf += S_numpy
    if test_dict is not None:
        if not just_S:
            acc = get_statistics(Sf, test_dict)
            accs += "Final acc is: {:.4f}".format(acc)
    return accs, Sf
Exemple #2
0
def get_acc(source_outputs, target_outputs, alphas=None, att_simi=None, value_simi=None, alpha_att_val=[1, 0.5, 0.5], test_dict=None):
    Sf = np.zeros((len(source_outputs[0]), len(target_outputs[0])))
    alphas = np.array(alphas)
    alphas = alphas / alphas.sum()
    accs = ""
    for i in range(0, len(source_outputs)):
        S = torch.matmul(F.normalize(source_outputs[i]), F.normalize(target_outputs[i]).t())
        S_numpy = S.detach().cpu().numpy()
        if test_dict is not None:
            acc = get_statistics(S_numpy, test_dict)
            accs += "Acc layer {} is: {:.4f}, ".format(i, acc)
        if alphas is not None:
            Sf += alphas[i] * S_numpy
        else:
            Sf += S_numpy
    Sori = Sf + 0
    if att_simi is not None:
        Sf = alpha_att_val[0] * Sf + alpha_att_val[1] * att_simi + alpha_att_val[2] * value_simi
    if test_dict is not None:
        acc = get_statistics(Sf, test_dict)
        accs += "Final acc is: {:.4f}".format(acc)
    return accs, Sf, Sori
                      gammastruc = args.gammastruc, gammaattr = args.gammaattr, normalize=True, num_top=args.num_top)
    elif algorithm == "BigAlign":
        model = BigAlign(source_dataset, target_dataset, lamb=args.lamb)
    elif algorithm == "IONE":
        model = IONE(source_dataset, target_dataset, gt_train=args.train_dict, epochs=args.epochs, dim=args.dim, seed=args.seed)
    elif algorithm == "PALE":
        model = PALE(source_dataset, target_dataset, args)
    elif algorithm == "DeepLink":
        model = DeepLink(source_dataset, target_dataset, args)
    else:
        raise Exception("Unsupported algorithm")

    random.seed(args.seed)
    np.random.seed(args.seed)
    torch.manual_seed(7)

    start_time = time()

    S = model.align()
    get_statistics(S, groundtruth_matrix)
    print("Full_time: ", time() - start_time)









Exemple #4
0
    def nawal_mapping(self):
        #print("Start nawal mapping")
        self.mapping = Mapping(self.args.embedding_dim)
        self.discriminator = Discriminator(self.args.embedding_dim,
                                           self.args.dis_layers,
                                           self.args.dis_hid_dim,
                                           self.args.dis_dropout,
                                           self.args.dis_input_dropout)

        optim_fn, optim_params = get_optimizer(self.args.map_optimizer)
        self.map_optimizer = optim_fn(self.mapping.parameters(),
                                      **optim_params)
        optim_fn, optim_params = get_optimizer(self.args.dis_optimizer)
        self.dis_optimizer = optim_fn(self.discriminator.parameters(),
                                      **optim_params)

        if self.args.cuda:
            self.mapping = self.mapping.cuda()
            self.discriminator = self.discriminator.cuda()
        nawal_map_epoch_times = []
        for n_epoch in range(self.args.nawal_mapping_epochs):
            print('Starting adversarial training epoch %i...' % n_epoch)
            tic = time.time()
            n_nodes_proc = 0
            stats = {'DIS_COSTS': []}

            for n_iter in range(0, self.args.nawal_mapping_epoch_size,
                                self.args.nawal_mapping_batch_size):
                # discriminator training
                for _ in range(self.args.dis_steps):
                    self.dis_step(stats)

                # mapping training (discriminator fooling)
                n_nodes_proc += self.mapping_step()

                # log stats
                if n_iter % 500 == 0:
                    stats_str = [('DIS_COSTS', 'Discriminator loss')]
                    stats_log = [
                        '%s: %.4f' % (v, np.mean(stats[k]))
                        for k, v in stats_str if len(stats[k]) > 0
                    ]
                    stats_log.append('%i samples/s' % int(n_nodes_proc /
                                                          (time.time() - tic)))
                    print(('%06i - ' % n_iter) + ' - '.join(stats_log))

                    # reset
                    tic = time.time()
                    n_nodes_proc = 0
                    for k, _ in stats_str:
                        del stats[k][:]
            # embeddings / discriminator evaluation
            self.dist_mean_cosine()

            # JSON log / save best model / end of epoch
            self.save_best()

            print('End of epoch %i.\n\n' % n_epoch)
            nawal_map_epoch_times.append(time.time() - tic)
            # update the learning rate (stop if too small)
            self.update_lr()

        self.reload_best()
        self.S = self.calculate_simi_matrix(self.mapping.eval())
        # print("NAWAL before refining")
        groundtruth_matrix = load_gt(self.args.test_dict,
                                     self.source_dataset.id2idx,
                                     self.target_dataset.id2idx)
        groundtruth_dict = load_gt(self.args.test_dict,
                                   self.source_dataset.id2idx,
                                   self.target_dataset.id2idx, 'dict')
        self.nawal_before_refine_acc = get_statistics(self.S, groundtruth_dict,
                                                      groundtruth_matrix)
        # print("Accuracy: {}".format(acc))

        self.mapping.train()
        nawal_refine_epoch_times = []
        # training loop
        for n_iter in range(self.n_refinement):
            tic = time.time()
            # build a dictionary from aligned embeddings
            src_emb = self.mapping(self.source_embedding).data
            tgt_emb = self.target_embedding
            dico = build_dictionary(src_emb, tgt_emb, p_keep=0.45)
            # apply the Procrustes solution
            self.procrustes(dico)
            self.dist_mean_cosine()
            self.save_best()
            nawal_refine_epoch_times.append(time.time() - tic)
        self.reload_best()

        S = self.calculate_simi_matrix(self.mapping.eval(), save=True)
        # print("Nawal after refining")
        groundtruth_matrix = load_gt(self.args.test_dict,
                                     self.source_dataset.id2idx,
                                     self.target_dataset.id2idx)
        groundtruth_dict = load_gt(self.args.test_dict,
                                   self.source_dataset.id2idx,
                                   self.target_dataset.id2idx, 'dict')
        self.nawal_after_refine_acc = get_statistics(S, groundtruth_dict,
                                                     groundtruth_matrix)
        self.log()
        print("NAWAL average map epoch time: {:.4f}".format(
            np.mean(nawal_map_epoch_times)))
        print("NAWAL average refine epoch time: {:.4f}".format(
            np.mean(nawal_refine_epoch_times)))
        print("NAWAL average emb epoch time: {:.4f}".format(
            np.mean(self.epoch_times)))
        return S
Exemple #5
0
    np.random.seed(args.seed)
    torch.manual_seed(args.seed)
    source_dataset = Dataset(args.source_dataset, args.dataset_name)
    target_dataset = Dataset(args.target_dataset)
    groundtruth = graph_utils.load_gt(args.groundtruth, source_dataset.id2idx,
                                      target_dataset.id2idx, 'dict')

    algorithm = args.algorithm

    if algorithm == "EMGCN":
        model = EMGCN(source_dataset, target_dataset, args)
    else:
        raise Exception("Unsupported algorithm")

    S = model.align()

    for i in range(2):
        if i == 1:
            print("right to left...")
        else:
            print("left to right...")
        accs, MAP, AUC = get_statistics(S, groundtruth, get_all_metric=True)
        for key, value in accs.items():
            print("Hit_{}: {:.4f}".format(key, value))
        print("MAP: {:.4f}".format(MAP))
        print("AUC: {:.4f}".format(AUC))
        print("Full_time: {:.4f}".format(time() - start_time))

        S = S.T
        groundtruth = {v: k for k, v in groundtruth.items()}
    algorithm = args.algorithm

    if algorithm == "FINAL":
        train_dict = None
        if args.train_dict != "":
            train_dict = graph_utils.load_gt(args.train_dict, source_dataset.id2idx, target_dataset.id2idx, 'dict')
        model = FINAL(source_dataset, target_dataset, H=args.H, alpha=args.alpha, maxiter=args.max_iter, tol=args.tol, train_dict=train_dict)
    elif algorithm == "REGAL":
        model = REGAL(source_dataset, target_dataset, max_layer=args.max_layer, alpha=args.alpha, k=args.k, num_buckets=args.buckets,
                      gammastruc = args.gammastruc, gammaattr = args.gammaattr, normalize=True, num_top=args.num_top)
    elif algorithm == "ANAGCN":
        model = ANAGCN(source_dataset, target_dataset, args)
    else:
        raise Exception("Unsupported algorithm")

    S = model.align()

    
    acc, MAP, Hit, AUC, top5, top10, top20, top30, top50, top100 = get_statistics(S, groundtruth, get_all_metric=True)
    print("MAP: {:.4f}".format(MAP))
    print("AUC: {:.4f}".format(AUC))
    print("Top_1: {:.4f}".format(acc))
    print("Top_5: {:.4f}".format(top5))
    print("Top_10: {:.4f}".format(top10))
    print("Top_20: {:.4f}".format(top20))
    print("Top_30: {:.4f}".format(top30))
    print("Top_50: {:.4f}".format(top50))
    print("Top_100: {:.4f}".format(top100))
    print("Full_time: {:.4f}".format(time() - start_time))

                     gt_train=args.train_dict,
                     epochs=args.epochs,
                     dim=args.dim,
                     seed=args.seed,
                     learning_rate=args.lr)
    elif algorithm == "DeepLink":
        model = DeepLink(source_dataset, target_dataset, args)
    elif algorithm == "GAlign":
        model = GAlign(source_dataset, target_dataset, args)
    elif algorithm == "PALE":
        model = PALE(source_dataset, target_dataset, args)
    elif algorithm == "CENALP":
        model = CENALP(source_dataset, target_dataset, args)
    elif algorithm == "NAWAL":
        model = NAWAL(source_dataset, target_dataset, args)
    else:
        raise Exception("Unsupported algorithm")

    S = model.align()
    print("-" * 100)
    acc, MAP, top5, top10 = get_statistics(S,
                                           groundtruth,
                                           use_greedy_match=True,
                                           get_all_metric=True)
    print("Accuracy: {:.4f}".format(acc))
    print("MAP: {:.4f}".format(MAP))
    print("Precision_5: {:.4f}".format(top5))
    print("Precision_10: {:.4f}".format(top10))
    print("-" * 100)
    print('Running time: {}'.format(time() - start_time))