示例#1
0
    X_tr, y_tr, X_val, y_val = val.split_data(X_train, y_train, frac=frac, seed=seed)

    # Filter y values to 0, 1 labels
    y_tr_true = mu.mnist_filter(y_tr, filterby=2)
    y_val_true = mu.mnist_filter(y_val, filterby=2)

    # Define arrays
    eta_arr = np.logspace(-6,0,num)
    err_val = np.zeros((num,num))
    err_train = np.zeros((num,num))

    for ii in range(num):
        print("Eta:",eta_arr[ii])
        err_val[ii,:], err_train[ii,:], lams = \
        val.logistic_reg_path(X_tr, y_tr_true, X_val, y_val_true, grad=cu.bin_logistic_grad,
        lammax=lammax,scale=scale, num=num, error_func=val.loss_01, thresh=best_thresh, best_w=False,
        eta=eta_arr[ii], adaptive=True, llfn=val.logloss_bin, savell=False, eps=1.0e-2)

    # Find minimum threshold, lambda from minimum validation error
    # Mask infs
    err_val[np.isinf(err_val)] = np.nan
    ind_e,ind_l = np.unravel_index(np.nanargmin(err_val), err_val.shape)
    best_lambda = lams[ind_l]
    best_eta = eta_arr[ind_e]
    print("Best lambda:",best_lambda)
    print("Best eta:",best_eta)

# With a best fit lambda, threshold, refit
eps = 2.5e-3 # Use tighter eps for better convergence
w0, w, ll_train, ll_test, train_01, test_01, iter_train = \
gd.gradient_descent(cu.bin_logistic_grad, X_train, y_train_true,
示例#2
0
    X_tr, y_tr, X_val, y_val = val.split_data(X_train, y_train, frac=frac, seed=seed)

    # Filter y values to 0, 1 labels
    y_tr_true = np.asarray(y_tr[:, None] == np.arange(max(y_tr)+1),dtype=int).squeeze()
    y_val_true = np.asarray(y_val[:, None] == np.arange(max(y_val)+1),dtype=int).squeeze()

    # Define arrays
    eta_arr = np.logspace(-7,-3,num)
    err_val = np.zeros((num,num))
    err_train = np.zeros((num,num))

    for ii in range(num):
        print("Eta:",eta_arr[ii])
        err_val[ii,:], err_train[ii,:], lams = \
        val.logistic_reg_path(X_tr, y_tr_true, X_val, y_val_true, grad=cu.multi_logistic_grad,
        y_train_label=y_tr, y_val_label=y_val, classfn=cu.multi_logistic_classifier,
        lammax=lammax,scale=scale, num=num, error_func=val.loss_01, thresh=best_thresh, best_w=False,
        eta=eta_arr[ii], adaptive=True, llfn=val.logloss_multi, savell=False, eps=1.0e-2)

    # Find minimum threshold, lambda from minimum validation error
    # Mask infs
    print(err_val)
    err_val[np.isinf(err_val)] = np.nan
    ind_e,ind_l = np.unravel_index(np.nanargmin(err_val), err_val.shape)
    best_lambda = lams[ind_l]
    best_eta = eta_arr[ind_e]
    print("Best lambda:",best_lambda)
    print("Best eta:",best_eta)

    if show_plots:
        fig, ax = plt.subplots()
示例#3
0
    eps = 5.0e-3
    frac = 0.1 # Fraction of training data to split into validation set
    kwargs = {}

    # Split training data into subtraining set, validation set
    X_tr, y_tr, X_val, y_val = val.split_data(X_train, y_train, frac=frac, seed=seed)

    # Filter y values to 0, 1 labels
    y_tr_true = np.asarray(y_tr[:, None] == np.arange(max(y_tr)+1),dtype=int).squeeze()
    y_val_true = np.asarray(y_val[:, None] == np.arange(max(y_val)+1),dtype=int).squeeze()

    # Run reg path!
    err_val, err_train, lams = val.logistic_reg_path(X_tr, y_tr_true, \
                         X_val, y_val_true,
                         y_train_label = y_tr, y_val_label = y_val,
                         grad=cu.bin_logistic_grad, lammax=lammax,
                         scale=scale, num=num, error_func=val.loss_01, thresh=0.5, best_w=False,
                         eta = eta, sparse=False, eps=eps, max_iter=1000,
                         adaptive=True, llfn=val.logloss_multi, savell=False, batchsize=100,
                         classfn=cu.multi_logistic_classifier, **kwargs)

    # Find minimum lambda from minimum validation error
    # Mask infs
    print(err_val)
    err_val[np.isinf(err_val)] = np.nan
    ind_l = np.nanargmin(err_val)
    best_lambda = lams[ind_l]
    print("Best lambda:",best_lambda)

    if show_plots:
        fig, ax = plt.subplots()
示例#4
0
    # Filter y values to 0, 1 labels
    y_tr_true = np.asarray(y_tr[:, None] == np.arange(max(y_tr) + 1),
                           dtype=int).squeeze()
    y_val_true = np.asarray(y_val[:, None] == np.arange(max(y_val) + 1),
                            dtype=int).squeeze()

    # Define arrays
    eta_arr = np.logspace(-7, -3, num)
    err_val = np.zeros((num, num))
    err_train = np.zeros((num, num))

    for ii in range(num):
        print("Eta:", eta_arr[ii])
        err_val[ii,:], err_train[ii,:], lams = \
        val.logistic_reg_path(X_tr, y_tr_true, X_val, y_val_true, grad=cu.multi_logistic_grad,
        y_train_label=y_tr, y_val_label=y_val, classfn=cu.multi_logistic_classifier,
        lammax=lammax,scale=scale, num=num, error_func=val.loss_01, thresh=best_thresh, best_w=False,
        eta=eta_arr[ii], adaptive=True, llfn=val.logloss_multi, savell=False, eps=1.0e-2)

    # Find minimum threshold, lambda from minimum validation error
    # Mask infs
    print(err_val)
    err_val[np.isinf(err_val)] = np.nan
    ind_e, ind_l = np.unravel_index(np.nanargmin(err_val), err_val.shape)
    best_lambda = lams[ind_l]
    best_eta = eta_arr[ind_e]
    print("Best lambda:", best_lambda)
    print("Best eta:", best_eta)

    if show_plots:
        fig, ax = plt.subplots()
示例#5
0
                                              seed=seed)

    # Filter y values to 0, 1 labels
    y_tr_true = mu.mnist_filter(y_tr, filterby=2)
    y_val_true = mu.mnist_filter(y_val, filterby=2)

    # Define arrays
    eta_arr = np.logspace(-6, 0, num)
    err_val = np.zeros((num, num))
    err_train = np.zeros((num, num))

    for ii in range(num):
        print("Eta:", eta_arr[ii])
        err_val[ii,:], err_train[ii,:], lams = \
        val.logistic_reg_path(X_tr, y_tr_true, X_val, y_val_true, grad=cu.bin_logistic_grad,
        lammax=lammax,scale=scale, num=num, error_func=val.loss_01, thresh=best_thresh, best_w=False,
        eta=eta_arr[ii], adaptive=True, llfn=val.logloss_bin, savell=False, eps=1.0e-2)

    # Find minimum threshold, lambda from minimum validation error
    # Mask infs
    err_val[np.isinf(err_val)] = np.nan
    ind_e, ind_l = np.unravel_index(np.nanargmin(err_val), err_val.shape)
    best_lambda = lams[ind_l]
    best_eta = eta_arr[ind_e]
    print("Best lambda:", best_lambda)
    print("Best eta:", best_eta)

# With a best fit lambda, threshold, refit
eps = 2.5e-3  # Use tighter eps for better convergence
w0, w, ll_train, ll_test, train_01, test_01, iter_train = \
gd.gradient_descent(cu.bin_logistic_grad, X_train, y_train_true,