Ejemplo n.º 1
0
def main(args):
    fpath = "./dataset/{0}.dat".format(args.dname)
    X, y = load_dat(fpath, minmax=(0, 1), normalize=False, bias_term=True)
    N, dim = X.shape

    nrep = args.rep
    delta = args.delta
    obj_clip = args.obj_clip
    grad_clip = args.grad_clip

    epsilon = [0.05, 0.1, 0.2, 0.4, 0.8, 1.6]
    neps = len(epsilon)

    K = 5  # 5-folds cross-validation
    cv_rep = 2
    k = 0
    acc = np.zeros((neps, nrep, K * cv_rep))
    obj = np.zeros((neps, nrep, K * cv_rep))

    rkf = RepeatedKFold(n_splits=K, n_repeats=cv_rep)

    for train, test in rkf.split(X):
        train_X, train_y = X[train, :], y[train]
        test_X, test_y = X[test, :], y[test]

        n_train = train_X.shape[0]

        for i, eps in enumerate(epsilon):
            ipdb.set_trace()
            rho = dp_to_zcdp(eps, delta)

            for j in range(nrep):
                sol = agd(train_X,
                          train_y,
                          rho,
                          eps,
                          delta,
                          logistic_grad,
                          logistic_loss,
                          logistic_test,
                          obj_clip,
                          grad_clip,
                          reg_coeff=args.reg_coeff,
                          batch_size=args.batch_size,
                          exp_dec=args.exp_dec,
                          gamma=args.gamma,
                          verbose=True)

                obj[i, j, k] = logistic_loss(sol, train_X, train_y) / n_train
                acc[i, j, k] = logistic_test(sol, test_X, test_y) * 100.0
                # print "acc[{},{},{}]={}".format(i, j, k, acc[i, j, k])

        k += 1

    avg_acc = np.vstack([np.mean(acc, axis=(1, 2)), np.std(acc, axis=(1, 2))])
    avg_obj = np.vstack([np.mean(obj, axis=(1, 2)), np.std(obj, axis=(1, 2))])

    filename = "agd_logres_{0}".format(args.dname)
    np.savetxt("{0}_acc.out".format(filename), avg_acc, fmt='%.5f')
    np.savetxt("{0}_obj.out".format(filename), avg_obj, fmt='%.5f')
Ejemplo n.º 2
0
def main(args):
    # load the dataset
    fpath = "./dataset/{0}.dat".format(args.dname)
    X, y = load_dat(fpath, minmax=(0, 1), normalize=False, bias_term=True)
    N = X.shape[0]

    delta = args.delta
    obj_clip = args.obj_clip
    grad_clip = args.grad_clip

    # variables to change
    epsilon = [0.1, 0.5, 1.0]
    splits = [20, 40, 60, 80, 100, 120]
    n_eps = len(epsilon)
    n_rep = 10
    n_splits = len(splits)

    loss = np.zeros((n_eps, n_splits, n_rep))
    acc = np.zeros((n_eps, n_splits, n_rep))

    for k, eps in enumerate(epsilon):
        rho = dp_to_zcdp(eps, delta)
        print "rho = {:.5f}".format(rho)

        for i, split in enumerate(splits):
            for j in range(n_rep):
                w = agd(X,
                        y,
                        rho,
                        eps,
                        delta,
                        logistic_grad,
                        logistic_loss,
                        logistic_test,
                        obj_clip,
                        grad_clip,
                        reg_coeff=0.0,
                        splits=split)
                loss[k, i, j] = logistic_loss(w, X, y) / N
                acc[k, i, j] = logistic_test(w, X, y)

    avg_loss = np.mean(loss, axis=2)
    avg_acc = np.mean(acc, axis=2)

    np.savetxt('varying_splits_{0}_acc.out'.format(args.dname),
               avg_acc,
               fmt='%.5f')
    np.savetxt('varying_splits_{0}_obj.out'.format(args.dname),
               avg_loss,
               fmt='%.5f')
Ejemplo n.º 3
0
def main(args):
    # load the dataset
    fpath = "./dataset/{0}.dat".format(args.dname)
    X, y = load_dat(fpath, minmax=(0, 1), normalize=False, bias_term=True)
    N = X.shape[0]

    delta = args.delta
    obj_clip = args.obj_clip
    grad_clip = args.grad_clip

    # variables to change
    epsilon = [0.1, 0.2, 0.4, 0.8, 1.6]
    gammas = [0.1, 0.2, 0.3, 0.4, 0.5]
    n_eps = len(epsilon)
    n_rep = 10
    n_gammas = len(gammas)

    loss = np.zeros((n_gammas, n_eps, n_rep))
    acc = np.zeros_like(loss)

    for i, gamma in enumerate(gammas):
        for j, eps in enumerate(epsilon):
            rho = dp_to_zcdp(eps, delta)
            print "rho = {:.5f}".format(rho)

            for k in range(n_rep):
                w = agd(X,
                        y,
                        rho,
                        eps,
                        delta,
                        logistic_grad,
                        logistic_loss,
                        logistic_test,
                        obj_clip,
                        grad_clip,
                        reg_coeff=0.0,
                        gamma=gamma)
                loss[i, j, k] = logistic_loss(w, X, y) / N
                acc[i, j, k] = logistic_test(w, X, y)

    avg_loss = np.mean(loss, axis=2)
    avg_acc = np.mean(acc, axis=2)

    np.savetxt('varying_gamma_{0}_acc.out'.format(args.dname),
               avg_acc,
               fmt='%.5f')
    np.savetxt('varying_gamma_{0}_obj.out'.format(args.dname),
               avg_loss,
               fmt='%.5f')