コード例 #1
0
def main():

    np.random.seed(args.seed)
    cur_acc = 0
    max_acc = 0
    num_param = 12
    cur_param = np.zeros(num_param)
    max_pt = np.zeros(num_param)
    for iii in range(args.n_iter):
        for jjj in range(args.n_samples):
            for kkk in range(num_param):
                cur_param[kkk] = np.random.beta(1, 1)
            cur_param[2] *= 0.5
            cur_param[4] *= 0.5
            cur_param[9] *= 0.5
            cur_param[5] /= 0.5
            cur_param[6] *= 0.5
            cur_param[11] /= 0.5
            cur_param[10] *= 0.5
            cur_acc = black_box_function(cur_param)
            if max_acc < cur_acc:
                max_acc = cur_acc
                max_pt = cur_param.copy()

    hyp_param = np.zeros(6)
    hyp_param[0] = max_pt[0]
    hyp_param[1] = 1 - max_pt[0]
    hyp_param[2] = max_pt[1]
    hyp_param[3] = max_pt[2]
    hyp_param[4] = max_pt[3]
    hyp_param[5] = max_pt[4]

    rate_schedule = hyp_param[0] * (1 - np.exp(-hyp_param[3] * np.power(
        np.arange(args.n_epoch, dtype=float),
        hyp_param[2]))) + hyp_param[1] * (1 - 1 / np.power(
            (hyp_param[5] * np.arange(args.n_epoch, dtype=float) + 1),
            hyp_param[4]))
    print('Schedule:', rate_schedule, hyp_param)
    '''
    rate_schedule=np.ones(args.n_epoch)*forget_rate
    rate_schedule[:10]=np.arange(10,dtype=float)/10*forget_rate
    # rate_schedule[10:]=np.arange(args.n_epoch-10,dtype=float)/(args.n_epoch-10)*forget_rate+forget_rate
    rate_schedule=np.zeros(args.n_epoch)
    print(rate_schedule)
    '''
    epoch = 0
    mean_pure_ratio1 = 0
    mean_pure_ratio2 = 0

    print('building model...')
    cnn1 = CNN_large(n_outputs=num_classes)
    cnn1.cuda()
    print(cnn1.parameters)
    optimizer1 = torch.optim.Adam(cnn1.parameters(), lr=learning_rate)

    cnn2 = CNN_large(n_outputs=num_classes)
    cnn2.cuda()
    print(cnn2.parameters)
    optimizer2 = torch.optim.Adam(cnn2.parameters(), lr=learning_rate)

    train_acc1 = 0
    train_acc2 = 0
    # evaluate models with random weights
    test_acc1, test_acc2 = evaluate(test_loader, cnn1, cnn2)
    print(
        'Epoch [%d/%d] Test Accuracy on the %s test images: Model1 %.4f %% Model2 %.4f %% Pure Ratio1 %.4f %% Pure Ratio2 %.4f %%'
        % (epoch + 1, args.n_epoch, len(test_dataset), test_acc1, test_acc2,
           mean_pure_ratio1, mean_pure_ratio2))
    # save results
    with open(txtfile, "a") as myfile:
        myfile.write(
            str(int(epoch)) + ' ' + str(train_acc1) + ' ' + str(train_acc2) +
            ' ' + str(test_acc1) + " " + str(test_acc2) + ' ' +
            str(mean_pure_ratio1) + ' ' + str(mean_pure_ratio2) + ' ' +
            str(rate_schedule[epoch]) + "\n")

    # training
    for epoch in range(1, args.n_epoch):
        # train models
        cnn1.train()
        adjust_learning_rate(optimizer1, epoch)
        cnn2.train()
        adjust_learning_rate(optimizer2, epoch)
        train_acc1, train_acc2, pure_ratio_1_list, pure_ratio_2_list = train(
            train_loader, epoch, cnn1, optimizer1, cnn2, optimizer2,
            rate_schedule)
        # evaluate models
        test_acc1, test_acc2 = evaluate(test_loader, cnn1, cnn2)
        # save results
        mean_pure_ratio1 = sum(pure_ratio_1_list) / len(pure_ratio_1_list)
        mean_pure_ratio2 = sum(pure_ratio_2_list) / len(pure_ratio_2_list)
        print(
            'Epoch [%d/%d] Test Accuracy on the %s test images: Model1 %.4f %% Model2 %.4f %%, Pure Ratio 1 %.4f %%, Pure Ratio 2 %.4f %%'
            % (epoch + 1, args.n_epoch, len(test_dataset), test_acc1,
               test_acc2, mean_pure_ratio1, mean_pure_ratio2))
        with open(txtfile, "a") as myfile:
            myfile.write(
                str(int(epoch)) + ' ' + str(train_acc1) + ' ' +
                str(train_acc2) + ' ' + str(test_acc1) + " " + str(test_acc2) +
                ' ' + str(mean_pure_ratio1) + ' ' + str(mean_pure_ratio2) +
                ' ' + str(rate_schedule[epoch]) + "\n")
コード例 #2
0
def black_box_function(opt_param):
    mean_pure_ratio1 = 0
    mean_pure_ratio2 = 0

    print('building model...')
    cnn1 = CNN_large(n_outputs=num_classes)
    cnn1.cuda()
    print(cnn1.parameters)
    optimizer1 = torch.optim.Adam(cnn1.parameters(), lr=learning_rate)

    cnn2 = CNN_large(n_outputs=num_classes)
    cnn2.cuda()
    print(cnn2.parameters)
    optimizer2 = torch.optim.Adam(cnn2.parameters(), lr=learning_rate)

    # rate_schedule=opt_param[0]*(1-np.exp(-opt_param[2]*np.power(np.arange(args.n_epoch,dtype=float),opt_param[1])))+(1-opt_param[0])*(1-1/np.power((opt_param[4]*np.arange(args.n_epoch,dtype=float)+1),opt_param[3]))-np.power(np.arange(args.n_epoch,dtype=float)/args.n_epoch,opt_param[5])*opt_param[6]
    rate_schedule=opt_param[0]*(1-np.exp(-opt_param[2]*np.power(np.arange(args.n_epoch,dtype=float),opt_param[1])))\
+(1-opt_param[0])*opt_param[7]*(1-1/np.power((opt_param[4]*np.arange(args.n_epoch,dtype=float)+1),opt_param[3]))\
+(1-opt_param[0])*(1-opt_param[7])*(1-np.log(1+opt_param[8])/np.log(1+opt_param[8]+opt_param[9]*np.arange(args.n_epoch,dtype=float)))\
-np.power(np.arange(args.n_epoch,dtype=float)/args.n_epoch,opt_param[5])*opt_param[6]\
-np.log(1+np.power(np.arange(args.n_epoch,dtype=float),opt_param[11]))/np.log(1+np.power(args.n_epoch,opt_param[11]))*opt_param[10]
    print('Schedule:', rate_schedule, opt_param)

    epoch = 0
    train_acc1 = 0
    train_acc2 = 0
    # evaluate models with random weights
    val_acc1, val_acc2 = evaluate(val_loader, cnn1, cnn2)
    test_acc1, test_acc2 = evaluate(test_loader, cnn1, cnn2)
    print(
        'Epoch [%d/%d] Test Accuracy on the %s test images: Model1 %.4f %% Model2 %.4f %% Pure Ratio1 %.4f %% Pure Ratio2 %.4f %%'
        % (epoch + 1, args.n_epoch, len(test_dataset), test_acc1, test_acc2,
           mean_pure_ratio1, mean_pure_ratio2))
    # save results
    with open(txtfile, "a") as myfile:
        myfile.write(
            str(int(epoch)) + ' ' + str(train_acc1) + ' ' + str(train_acc2) +
            ' ' + str(val_acc1) + ' ' + str(val_acc2) + ' ' + str(test_acc1) +
            " " + str(test_acc2) + ' ' + str(mean_pure_ratio1) + ' ' +
            str(mean_pure_ratio2) + ' ' + str(rate_schedule[epoch]) + "\n")

    # training
    for epoch in range(1, args.n_epoch):
        # train models
        cnn1.train()
        adjust_learning_rate(optimizer1, epoch)
        cnn2.train()
        adjust_learning_rate(optimizer2, epoch)
        train_acc1, train_acc2, pure_ratio_1_list, pure_ratio_2_list = train(
            train_loader, epoch, cnn1, optimizer1, cnn2, optimizer2,
            rate_schedule)
        # evaluate models
        val_acc1, val_acc2 = evaluate(val_loader, cnn1, cnn2)
        test_acc1, test_acc2 = evaluate(test_loader, cnn1, cnn2)
        # save results
        mean_pure_ratio1 = sum(pure_ratio_1_list) / len(pure_ratio_1_list)
        mean_pure_ratio2 = sum(pure_ratio_2_list) / len(pure_ratio_2_list)
        print(
            'Epoch [%d/%d] Test Accuracy on the %s test images: Model1 %.4f %% Model2 %.4f %%, Pure Ratio 1 %.4f %%, Pure Ratio 2 %.4f %%'
            % (epoch + 1, args.n_epoch, len(test_dataset), test_acc1,
               test_acc2, mean_pure_ratio1, mean_pure_ratio2))
        with open(txtfile, "a") as myfile:
            myfile.write(
                str(int(epoch)) + ' ' + str(train_acc1) + ' ' +
                str(train_acc2) + ' ' + str(val_acc1) + ' ' + str(val_acc2) +
                ' ' + str(test_acc1) + " " + str(test_acc2) + ' ' +
                str(mean_pure_ratio1) + ' ' + str(mean_pure_ratio2) + ' ' +
                str(rate_schedule[epoch]) + "\n")

    return (test_acc1 + test_acc2) / 200
コード例 #3
0
def black_box_function(opt_param):
    mean_pure_ratio1 = 0
    mean_pure_ratio2 = 0

    print('building model...')
    cnn1 = CNN_large(n_outputs=num_classes)
    cnn1.cuda()
    print(cnn1.parameters)
    optimizer1 = torch.optim.Adam(cnn1.parameters(), lr=learning_rate)

    cnn2 = CNN_large(n_outputs=num_classes)
    cnn2.cuda()
    print(cnn2.parameters)
    optimizer2 = torch.optim.Adam(cnn2.parameters(), lr=learning_rate)

    rate_schedule = opt_param.copy()
    print('Schedule:', rate_schedule)

    epoch = 0
    train_acc1 = 0
    train_acc2 = 0
    # evaluate models with random weights
    test_acc1, test_acc2 = evaluate(test_loader, cnn1, cnn2)
    print(
        'Epoch [%d/%d] Test Accuracy on the %s test images: Model1 %.4f %% Model2 %.4f %% Pure Ratio1 %.4f %% Pure Ratio2 %.4f %%'
        % (epoch + 1, args.n_epoch, len(test_dataset), test_acc1, test_acc2,
           mean_pure_ratio1, mean_pure_ratio2))
    # save results
    with open(txtfile, "a") as myfile:
        myfile.write(
            str(int(epoch)) + ' ' + str(train_acc1) + ' ' + str(train_acc2) +
            ' ' + str(test_acc1) + " " + str(test_acc2) + ' ' +
            str(mean_pure_ratio1) + ' ' + str(mean_pure_ratio2) + ' ' +
            str(rate_schedule[epoch]) + "\n")

    # training
    for epoch in range(1, args.n_epoch):
        # train models
        cnn1.train()
        adjust_learning_rate(optimizer1, epoch)
        cnn2.train()
        adjust_learning_rate(optimizer2, epoch)
        train_acc1, train_acc2, pure_ratio_1_list, pure_ratio_2_list = train(
            train_loader, epoch, cnn1, optimizer1, cnn2, optimizer2,
            rate_schedule)
        # evaluate models
        test_acc1, test_acc2 = evaluate(test_loader, cnn1, cnn2)
        # save results
        mean_pure_ratio1 = sum(pure_ratio_1_list) / len(pure_ratio_1_list)
        mean_pure_ratio2 = sum(pure_ratio_2_list) / len(pure_ratio_2_list)
        print(
            'Epoch [%d/%d] Test Accuracy on the %s test images: Model1 %.4f %% Model2 %.4f %%, Pure Ratio 1 %.4f %%, Pure Ratio 2 %.4f %%'
            % (epoch + 1, args.n_epoch, len(test_dataset), test_acc1,
               test_acc2, mean_pure_ratio1, mean_pure_ratio2))
        with open(txtfile, "a") as myfile:
            myfile.write(
                str(int(epoch)) + ' ' + str(train_acc1) + ' ' +
                str(train_acc2) + ' ' + str(test_acc1) + " " + str(test_acc2) +
                ' ' + str(mean_pure_ratio1) + ' ' + str(mean_pure_ratio2) +
                ' ' + str(rate_schedule[epoch]) + "\n")

    return (test_acc1 + test_acc2) / 200