Пример #1
0
def main():
    print(args)
    device = torch.device('cuda')
    maml = Meta(args).to(device)
    tmp = filter(lambda x: x.requires_grad, maml.parameters())
    num = sum(map(lambda x: np.prod(x.shape), tmp))
    print(maml)
    print('Total trainable tensors:', num)
    trainset = Gen(args.task_num, args.k_spt, args.k_qry)
    testset = Gen(args.task_num, args.k_spt, args.k_qry * 10)

    for epoch in range(args.epoch):
        ind = [i for i in range(trainset.xs.shape[0])]
        np.random.shuffle(ind)
        xs, ys = torch.Tensor(trainset.xs[ind]).to(device), torch.Tensor(
            trainset.ys[ind]).to(device)
        xq, yq = torch.Tensor(trainset.xq[ind]).to(device), torch.Tensor(
            trainset.yq[ind]).to(device)
        maml.train()
        loss = maml(xs, ys, xq, yq, epoch)
        print('Epoch: {} Initial loss: {} Train loss: {}'.format(
            epoch, loss[0] / args.task_num, loss[-1] / args.task_num))
        if (epoch + 1) % 50 == 0:
            print("Evaling the model...")
            torch.save(maml.state_dict(), 'save.pt')
            # del(maml)
            # maml = Meta(args).to(device)
            # maml.load_state_dict(torch.load('save.pt'))
            maml.eval()
            i = random.randint(0, testset.xs.shape[0] - 1)
            xs, ys = torch.Tensor(testset.xs[i]).to(device), torch.Tensor(
                testset.ys[i]).to(device)
            xq, yq = torch.Tensor(testset.xq[i]).to(device), torch.Tensor(
                testset.yq[i]).to(device)
            losses, losses_q, logits_q, _ = maml.finetunning(xs, ys, xq, yq)
            print('Epoch: {} Initial loss: {} Test loss: {}'.format(
                epoch, losses_q[0], losses_q[-1]))
Пример #2
0
def main():

    torch.manual_seed(222)
    torch.cuda.manual_seed_all(222)
    np.random.seed(222)

    ## Task Learner Setup
    task_config = [('conv2d', [32, 3, 3, 3, 1, 0]), ('relu', [True]),
                   ('bn', [32]), ('max_pool2d', [2, 2, 0]),
                   ('conv2d', [32, 32, 3, 3, 1, 0]), ('relu', [True]),
                   ('bn', [32]), ('max_pool2d', [2, 2, 0]),
                   ('conv2d', [32, 32, 3, 3, 1, 0]), ('relu', [True]),
                   ('bn', [32]), ('max_pool2d', [2, 2, 0]),
                   ('conv2d', [32, 32, 3, 3, 1, 0]), ('relu', [True]),
                   ('bn', [32]), ('max_pool2d', [2, 1, 0]), ('flatten', []),
                   ('linear', [args.n_way, 32 * 5 * 5])]
    last_epoch = 0
    suffix = "_v0"
    save_path = os.getcwd() + '/data/model_batchsz' + str(
        args.k_model) + '_stepsz' + str(
            args.update_lr) + '_epoch' + str(last_epoch) + suffix + '.pt'
    while os.path.isfile(save_path):
        valid_epoch = last_epoch
        last_epoch += 500
        save_path = os.getcwd() + '/data/model_batchsz' + str(
            args.k_model) + '_stepsz' + str(
                args.update_lr) + '_epoch' + str(last_epoch) + suffix + '.pt'
    save_path = os.getcwd() + '/data/model_batchsz' + str(
        args.k_model) + '_stepsz' + str(
            args.update_lr) + '_epoch' + str(valid_epoch) + suffix + '.pt'

    device = torch.device('cuda')
    task_mod = Meta(args, task_config).to(device)
    task_mod.load_state_dict(torch.load(save_path))
    task_mod.eval()

    ## AL Learner Setup
    print(args)

    al_config = [('linear', [1, 32 * 5 * 5])]

    device = torch.device('cuda')
    maml = AL_Learner(args, al_config, task_mod).to(device)
    tmp = filter(lambda x: x.requires_grad, maml.parameters())
    num = sum(map(lambda x: np.prod(x.shape), tmp))
    print(maml)
    print('Total trainable tensors:', num)

    # batchsz here means total episode number
    mini = MiniImagenet('/home/tesca/data/miniimagenet/',
                        mode='train',
                        n_way=args.n_way,
                        k_shot=1,
                        k_query=args.k_qry,
                        batchsz=10000,
                        resize=args.imgsz)
    mini_test = MiniImagenet('/home/tesca/data/miniimagenet/',
                             mode='test',
                             n_way=args.n_way,
                             k_shot=1,
                             k_query=args.k_qry,
                             batchsz=100,
                             resize=args.imgsz)
    save_path = os.getcwd() + '/data/model_batchsz' + str(
        args.k_model) + '_stepsz' + str(args.update_lr) + '_epoch'

    for epoch in range(args.epoch // 10000):
        # fetch meta_batchsz num of episode each time
        db = DataLoader(mini,
                        args.task_num,
                        shuffle=True,
                        num_workers=1,
                        pin_memory=True)

        for step, (x_spt, y_spt, x_qry, y_qry) in enumerate(db):

            x_spt, y_spt, x_qry, y_qry = x_spt.to(device), y_spt.to(
                device), x_qry.to(device), y_qry.to(device)

            al_accs = maml(x_spt, y_spt, x_qry, y_qry)

            if step % 30 == 0:
                print('step:', step, '\tAL acc:', al_accs)

            if step % 500 == 0:  # evaluation
                torch.save(maml.state_dict(),
                           save_path + str(step) + "_al_net.pt")
                '''db_test = DataLoader(mini_test, 1, shuffle=True, num_workers=1, pin_memory=True)
Пример #3
0
def main():

    torch.manual_seed(666)
    torch.cuda.manual_seed_all(666)
    np.random.seed(666)

    # data_name = 'BlogCatalog'

    print(args)
    nb_epochs = 50
    nb_runs = 16
    nb_try = 16
    nb_batch_maml = 10
    nb_batch = 32
    lr_1 = 0.03
    lr_s = lr_1 * args.task_num
    tr = 0.6
    # features, labels, idx_train, y_train, idx_val, y_val, idx_labeled, train_unlabeled = SGC_process(data_name, degree=2, l_ratio=0.08, tr_ratio=0.8)
    # print(args.task_num)

    aucfile = 'results/auc_' + datetime.now().strftime("%m_%d_%H_%M") + '_yelp.txt'
    with open(aucfile, 'a') as f:
        f.write("settings: {labeled ratio: %f, training ratio: %f, epochs: %d, update_step: %d}\n" % (lr_1, tr, nb_epochs, args.update_step))
        for t in range(nb_try):
            taskData = task(nb_task=args.task_num, degree=2, l_ratio=lr_1, t_ratio=tr, name='yelp')
            taskData.loadNProcess()
            f.write("target data name:" + taskData.f_name[-1] + "\n")
            f.write("%d-th try: \n" % t)
            for i in range(nb_runs):
                # training maml
                print("maml training...")
                print("In %d-th run..." % (i + 1))
                f.write("%d-th run\n" % i)
                feature_list, label, l_list, ul_list, idx_test = taskData.sampleAnomaly()
                config = modelArch(feature_list[0].shape[1], args.n_way)
                device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
                maml = Meta(args, config).to(device)
                # stats of parameters to be updated
                tmp = filter(lambda x: x.requires_grad, maml.parameters())
                num = sum(map(lambda x: np.prod(x.shape), tmp))
                # print(maml)
                print("Total #trainable tensors: ", num)
                batch_gen = DataLoaderN(feature_list, l_list, ul_list, b_size=8, b_size_qry=6, nb_task=args.task_num, device=device)
                maml.train()
                for e in range(1, nb_epochs + 1):
                    print("Running %d-th epoch" % e)
                    epoch_loss = 0
                    epoch_acc = 0
                    for b in range(nb_batch_maml):
                        x_train, y_train, x_qry, y_qry = batch_gen.getBatch(qry=False)
                        y_pred, loss = maml(x_train, y_train, x_qry, y_qry)
                        epoch_loss += loss
                    print("Epoch loss: %f" % epoch_loss)
                print("End of training.")
                # testing
                print("Evaluating the maml model")
                maml.eval()
                x_test, y_test = feature_list[args.task_num-1][idx_test].to(device), label[idx_test].to(device)
                auc_roc, auc_pr, ap = maml.evaluating(x_test, y_test)
                print("End of evaluating.")
                f.write("MAML auc_roc: %.5f, auc_pr: %.5f, ap: %.5f\n" % (auc_roc, auc_pr, ap))

                # g-dev training
                print('G-dev training...')
                features, labels, idx_labeled, idx_unlabeled, idx_test = SGC_process(taskData.target, degree=2, l_ratio=lr_s, tr_ratio=tr)
                # print("finish loading data...")
                attr_dim = features.shape[1]
                # print("%d-th run:" % i)
                # model = FCNet(attr_dim, 1).to(device)
                # model = SGC_original(attr_dim, 1).to(device)
                model = SGC(attr_dim, 1).to(device)
                # print(model)
                optim = torch.optim.Adam(model.parameters(), lr=0.002, weight_decay=0)
                # loss = deviation_loss()
                data_sampler = DataLoader(features, idx_labeled, idx_unlabeled, b_size=8)
                model.float()
                model.train()
                for e in range(1, nb_epochs + 1):
                    # print('Epoch: %d' % e)
                    epoch_loss = 0
                    epoch_acc = 0
                    for b in range(nb_batch):
                        x_b, y_b = data_sampler.getBatch()
                        x_b, y_b = x_b.to(device), y_b.to(device)
                        y_pred = model(x_b)
                        loss = deviation_loss(y_b, y_pred)
                        optim.zero_grad()
                        loss.backward()
                        optim.step()
                        epoch_loss += loss.item()
                    print("epoch loss %f" % epoch_loss)
                # validation
                model.eval()
                # print(idx_val.shape)
                x_val = features[idx_test].to(device)
                # print(x_val.shape)
                y_pred = model(x_val).detach().cpu().numpy()
                y_val = labels[idx_test].detach().cpu().numpy()
                # fpr, tpr, roc_auc = dict(), dict(), dict()
                # for i in range(2):
                #     fpr[i], tpr[i], _ = roc_curve(y_val, y_pred, pos_label=1)
                #     roc_auc[i] = auc(fpr[i], tpr[i])
                auc_roc, _, auc_pr = aucPerformance(y_val, y_pred)
                print("G-dev auc_roc: %.5f, auc_pr: %.5f" % (auc_roc, auc_pr))
                f.write("G-Dev auc_roc: %.5f, auc_pr: %.5f\n" % (auc_roc, auc_pr))

    f.close()
Пример #4
0
def main(args):
    step = args.step
    set_seed(args.seed)

    adj, features, labels = load_citation(args.dataset, args.normalization)

    features = sgc_precompute(features, adj, args.degree)

    if args.dataset == 'citeseer':
        node_num = 3327
        class_label = [0, 1, 2, 3, 4, 5]
        combination = list(combinations(class_label, 2))
    elif args.dataset == 'cora':
        node_num = 2708
        class_label = [0, 1, 2, 3, 4, 5, 6]
        combination = list(combinations(class_label, 2))

    config = [('linear', [args.hidden, features.size(1)]),
              ('linear', [args.n_way, args.hidden])]

    device = torch.device('cuda')

    for i in range(len(combination)):
        print("Cross Validation: {}".format((i + 1)))

        maml = Meta(args, config).to(device)

        test_label = list(combination[i])
        train_label = [n for n in class_label if n not in test_label]
        print('Cross Validation {} Train_Label_List: {} '.format(
            i + 1, train_label))
        print('Cross Validation {} Test_Label_List: {} '.format(
            i + 1, test_label))

        for j in range(args.epoch):
            x_spt, y_spt, x_qry, y_qry = sgc_data_generator(
                features, labels, node_num, train_label, args.task_num,
                args.n_way, args.k_spt, args.k_qry)
            accs = maml.forward(x_spt, y_spt, x_qry, y_qry)
            print('Step:', j, '\tMeta_Training_Accuracy:', accs)
            if j % 100 == 0:
                torch.save(maml.state_dict(), 'maml.pkl')
                meta_test_acc = []
                for k in range(step):
                    model_meta_trained = Meta(args, config).to(device)
                    model_meta_trained.load_state_dict(torch.load('maml.pkl'))
                    model_meta_trained.eval()
                    x_spt, y_spt, x_qry, y_qry = sgc_data_generator(
                        features, labels, node_num, test_label, args.task_num,
                        args.n_way, args.k_spt, args.k_qry)
                    accs = model_meta_trained.forward(x_spt, y_spt, x_qry,
                                                      y_qry)
                    meta_test_acc.append(accs)
                if args.dataset == 'citeseer':
                    with open('citeseer.txt', 'a') as f:
                        f.write(
                            'Cross Validation:{}, Step: {}, Meta-Test_Accuracy: {}'
                            .format(
                                i + 1, j,
                                np.array(meta_test_acc).mean(axis=0).astype(
                                    np.float16)))
                        f.write('\n')
                elif args.dataset == 'cora':
                    with open('cora.txt', 'a') as f:
                        f.write(
                            'Cross Validation:{}, Step: {}, Meta-Test_Accuracy: {}'
                            .format(
                                i + 1, j,
                                np.array(meta_test_acc).mean(axis=0).astype(
                                    np.float16)))
                        f.write('\n')