Exemplo n.º 1
0
Arquivo: main.py Projeto: yshen4/pymal
def problem8():
    data = (train_bow_features, train_labels, val_bow_features, val_labels)

    # values of T and lambda to try
    Ts = [1, 5, 10, 15, 25, 50]
    Ls = [0.001, 0.01, 0.1, 1, 10]

    pct_tune_results = utils.tune_perceptron(Ts, *data)
    print('perceptron valid:', list(zip(Ts, pct_tune_results[1])))
    print('best = {:.4f}, T={:.4f}'.format(np.max(pct_tune_results[1]), Ts[np.argmax(pct_tune_results[1])]))

    avg_pct_tune_results = utils.tune_avg_perceptron(Ts, *data)
    print('avg perceptron valid:', list(zip(Ts, avg_pct_tune_results[1])))
    print('best = {:.4f}, T={:.4f}'.format(np.max(avg_pct_tune_results[1]), Ts[np.argmax(avg_pct_tune_results[1])]))

    # fix values for L and T while tuning Pegasos T and L, respective
    fix_L = 0.01
    peg_tune_results_T = utils.tune_pegasos_T(fix_L, Ts, *data)
    print('Pegasos valid: tune T', list(zip(Ts, peg_tune_results_T[1])))
    print('best = {:.4f}, T={:.4f}'.format(np.max(peg_tune_results_T[1]), Ts[np.argmax(peg_tune_results_T[1])]))

    fix_T = Ts[np.argmax(peg_tune_results_T[1])]
    peg_tune_results_L = utils.tune_pegasos_L(fix_T, Ls, *data)
    print('Pegasos valid: tune L', list(zip(Ls, peg_tune_results_L[1])))
    print('best = {:.4f}, L={:.4f}'.format(np.max(peg_tune_results_L[1]), Ls[np.argmax(peg_tune_results_L[1])]))

    utils.plot_tune_results('Perceptron', 'T', Ts, *pct_tune_results)
    utils.plot_tune_results('Avg Perceptron', 'T', Ts, *avg_pct_tune_results)
    utils.plot_tune_results('Pegasos', 'T', Ts, *peg_tune_results_T)
    utils.plot_tune_results('Pegasos', 'L', Ls, *peg_tune_results_L)
                                       Ts[np.argmax(pct_tune_results[1])]))

avg_pct_tune_results = utils.tune_avg_perceptron(Ts, *data)
print('avg perceptron valid:', list(zip(Ts, avg_pct_tune_results[1])))
print('best = {:.4f}, T={:.4f}'.format(np.max(avg_pct_tune_results[1]),
                                       Ts[np.argmax(avg_pct_tune_results[1])]))

# fix values for L and T while tuning Pegasos T and L, respective
fix_L = 0.01
peg_tune_results_T = utils.tune_pegasos_T(fix_L, Ts, *data)
print('Pegasos valid: tune T', list(zip(Ts, peg_tune_results_T[1])))
print('best = {:.4f}, T={:.4f}'.format(np.max(peg_tune_results_T[1]),
                                       Ts[np.argmax(peg_tune_results_T[1])]))

fix_T = Ts[np.argmax(peg_tune_results_T[1])]
peg_tune_results_L = utils.tune_pegasos_L(fix_T, Ls, *data)
print('Pegasos valid: tune L', list(zip(Ls, peg_tune_results_L[1])))
print('best = {:.4f}, L={:.4f}'.format(np.max(peg_tune_results_L[1]),
                                       Ls[np.argmax(peg_tune_results_L[1])]))

utils.plot_tune_results('Perceptron', 'T', Ts, *pct_tune_results)
utils.plot_tune_results('Avg Perceptron', 'T', Ts, *avg_pct_tune_results)
utils.plot_tune_results('Pegasos', 'T', Ts, *peg_tune_results_T)
utils.plot_tune_results('Pegasos', 'L', Ls, *peg_tune_results_L)

#-------------------------------------------------------------------------------
# Use the best method (perceptron, average perceptron or Pegasos) along with
# the optimal hyperparameters according to validation accuracies to test
# against the test dataset. The test data has been provided as
# test_bow_features and test_labels.
#-------------------------------------------------------------------------------
print('avg perceptron valid:', list(zip(Ts, avg_pct_tune_results[1])))
print('best = {:.4f}, T={:.4f}'.format(avg_pct_best_acc, avg_pct_best_T))
avg_pct_best_results = (avg_pct_best_acc, (avg_pct_best_T, ))
print("Average Perceptron Best Result:", avg_pct_best_results)

# fix values for L and T while tuning Pegasos T and L, respective

fix_L = 0.01

peg_tune_results_T = utils.tune_pegasos_T(fix_L, Ts, *data)
print('Pegasos valid: tune T', list(zip(Ts, peg_tune_results_T[1])))
peg_best_T_acc, peg_best_T = np.max(peg_tune_results_T[1]), Ts[np.argmax(
    peg_tune_results_T[1])]
print('best = {:.4f}, T={:.4f}'.format(peg_best_T_acc, peg_best_T))

peg_tune_results_L = utils.tune_pegasos_L(peg_best_T, Ls, *data)
print('Pegasos valid: tune L', list(zip(Ls, peg_tune_results_L[1])))
peg_best_L, peg_best_acc = Ls[np.argmax(peg_tune_results_L[1])], np.max(
    peg_tune_results_L[1])
print('best = {:.4f}, L={:.4f}'.format(peg_best_acc, peg_best_L))

peg_best_results = (peg_best_acc, (peg_best_T, peg_best_L))
print("Pegasos Best Result:", peg_best_results)

# Consoloidate Results
methods = [("Perceptron", p1.perceptron),
           ("Average Perceptron", p1.average_perceptron),
           ("Pegasos", p1.pegasos)]

training_results = [pct_best_results, avg_pct_best_results, peg_best_results]