Ejemplo n.º 1
0
Archivo: main.py Proyecto: 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)
Ejemplo n.º 2
0
Ls = [1, 5, 10, 20, 50, 100]

pct_tune_results = utils.tune_perceptron(Ts, *data)
avg_pct_tune_results = utils.tune_avg_perceptron(Ts, *data)

#print pct_tune_results
#print avg_pct_tune_results
#
# # fix values for L and T while tuning passive-aggressive T and L, respective
best_L = 20
best_T = 5
#
avg_pa_tune_results_T = utils.tune_passive_aggressive_T(best_L, Ts, *data)
avg_pa_tune_results_L = utils.tune_passive_aggressive_L(best_T, Ls, *data)
#
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('Avg Passive-Aggressive', 'T', Ts,
                        *avg_pa_tune_results_T)
utils.plot_tune_results('Avg Passive-Aggressive', 'L', Ls,
                        *avg_pa_tune_results_L)
#-------------------------------------------------------------------------------
#
#-------------------------------------------------------------------------------
# Section 2.11a
#
# Call one of the accuracy functions that you wrote in part 2.9.a and report
# the hyperparameter and accuracy of your best classifier on the test data.
# The test data has been provided as test_bow_features and test_labels.
#-------------------------------------------------------------------------------
print "test accuracy is :",\
                                       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.
#-------------------------------------------------------------------------------

# Your code here

#-------------------------------------------------------------------------------
# Assign to best_theta, the weights (and not the bias!) learned by your most
Ejemplo n.º 4
0
print("Validation accuracy: {:.4f}".format(val_accuracy))  # 0.8943

#-------------------------------------------------------------------------------
# Part 3 - Improving the Model
#-------------------------------------------------------------------------------

#-------------------------------------------------------------------------------
# Part 3.1 - Tuning the Hyperparameters
#-------------------------------------------------------------------------------

Ts = [1, 5, 10, 15, 20]

train_accs, val_accs = lab2.tune(Ts, train_bow_features, train_labels,
                                 val_bow_features, val_labels)

utils.plot_tune_results(Ts, train_accs, val_accs)

#-------------------------------------------------------------------------------
# Best T value
#-------------------------------------------------------------------------------

T_best = 10

#-------------------------------------------------------------------------------
# Part 3.2 - Understanding the Model
#-------------------------------------------------------------------------------

theta, theta_0 = lab2.perceptron(train_bow_features, train_labels, T_best)

word_list = sorted(dictionary.keys(), key=lambda word: dictionary[word])
sorted_words = utils.most_explanatory_words(theta, word_list)
Ejemplo n.º 5
0
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.
#-------------------------------------------------------------------------------

# Your code here
theta,theta_0 = p1.perceptron(test_bow_features, test_labels,50)
print(p1.classify(test_bow_features,theta,theta_0))
Ejemplo n.º 6
0
Ts = [1,2,3,4,5,6,7,10,12,15,17,20,25]
Ls = [1,10,15,20,25,27,30,32,34,50]

#pct_tune_results = utils.tune_perceptron(Ts, *data)
#avg_pct_tune_results = utils.tune_avg_perceptron(Ts, *data)

# fix values for L and T while tuning passive-aggressive T and L, respective
best_L = 27
best_T = 4

avg_pa_tune_results_T = utils.tune_passive_aggressive_T(best_L, Ts, *data)
avg_pa_tune_results_L = utils.tune_passive_aggressive_L(best_T, Ls, *data)

#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('Avg Passive-Aggressive', 'T', Ts, *avg_pa_tune_results_T)
utils.plot_tune_results('Avg Passive-Aggressive', 'L', Ls, *avg_pa_tune_results_L)


#-------------------------------------------------------------------------------
#
#-------------------------------------------------------------------------------
#
#
# Accuracy Computation
#-------------------------------------------------------------------------------

print "(train accuracy, test accuracy) before modification"
print p1.average_passive_aggressive_accuracy(train_bow_features,test_bow_features,train_labels,test_labels,T,L)

#-------------------------------------------------------------------------------
Ejemplo n.º 7
0
#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.
#-------------------------------------------------------------------------------

# Your code
best_theta, theta_0 = p1.pegasos(train_bow_features, train_labels, 25, 0.01)
# test_predict_labels = p1.classify(test_bow_features, best_theta, theta_0)
# test_accuracy = p1.accuracy(test_predict_labels, test_labels)
# print(test_accuracy)