-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.py
166 lines (122 loc) · 5.69 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import sys
import numpy as np
import matplotlib.pyplot as plt
sys.path.append("..")
from utils import *
from linear_regression import *
from svm import *
from softmax import *
from features import *
from kernel import *
# 1. Load MNIST data:
train_x, train_y, test_x, test_y = get_MNIST_data()
# Plot the first 20 images of the training set.
plot_images(train_x[0:20, :])
# 2. Linear Regression with Closed Form Solution
def run_linear_regression_on_MNIST(lambda_factor=1):
"""
Trains linear regression, classifies test data, computes test error on test set
Returns:
Final test error
"""
train_x, train_y, test_x, test_y = get_MNIST_data()
train_x_bias = np.hstack([np.ones([train_x.shape[0], 1]), train_x])
test_x_bias = np.hstack([np.ones([test_x.shape[0], 1]), test_x])
theta = closed_form(train_x_bias, train_y, lambda_factor)
test_error = compute_test_error_linear(test_x_bias, test_y, theta)
return test_error
# Print test error using linear_regression: Closed Form Solution
print('Linear Regression test_error =', run_linear_regression_on_MNIST(lambda_factor=1))
# 3. Support Vector Machine (One vs. Rest and Multiclass)
def run_svm_one_vs_rest_on_MNIST():
"""
Trains svm, classifies test data, computes test error on test set
Returns:
Test error for the binary svm
"""
train_x, train_y, test_x, test_y = get_MNIST_data()
train_y[train_y != 0] = 1
test_y[test_y != 0] = 1
pred_test_y = one_vs_rest_svm(train_x, train_y, test_x)
test_error = compute_test_error_svm(test_y, pred_test_y)
return test_error
print('SVM one vs. rest test_error:', run_svm_one_vs_rest_on_MNIST())
def run_multiclass_svm_on_MNIST():
"""
Trains svm, classifies test data, computes test error on test set
Returns:
Test error for the binary svm
"""
train_x, train_y, test_x, test_y = get_MNIST_data()
pred_test_y = multi_class_svm(train_x, train_y, test_x)
test_error = compute_test_error_svm(test_y, pred_test_y)
return test_error
print('Multiclass SVM test_error:', run_multiclass_svm_on_MNIST())
# 4. Multinomial (Softmax) Regression and Gradient Descent
def run_softmax_on_MNIST(temp_parameter=1):
"""
Trains softmax, classifies test data, computes test error, and plots cost function
Runs softmax_regression on the MNIST training set and computes the test error using
the test set. It uses the following values for parameters:
alpha = 0.3
lambda = 1e-4
num_iterations = 150
Saves the final theta to ./theta.pkl.gz
Returns:
Final test error
"""
train_x, train_y, test_x, test_y = get_MNIST_data()
theta, cost_function_history = softmax_regression(train_x, train_y, temp_parameter, alpha= 0.3, lambda_factor = 1.0e-4, k = 10, num_iterations = 150)
plot_cost_function_over_time(cost_function_history)
test_error = compute_test_error(test_x, test_y, theta, temp_parameter)
# Save the model parameters theta obtained from calling softmax_regression to disk.
write_pickle_data(theta, "./theta.pkl.gz")
# TODO: add your code here for the "Using the Current Model" question in tab 4.
# and print the test_error_mod3
return test_error
print('softmax test_error=', run_softmax_on_MNIST(temp_parameter=1))
# TODO: Find the error rate for temp_parameter = [.5, 1.0, 2.0]
# Remember to return the tempParameter to 1, and re-run run_softmax_on_MNIST
# 6. Changing Labels
def run_softmax_on_MNIST_mod3(temp_parameter=1):
"""
Trains Softmax regression on digit (mod 3) classifications.
See run_softmax_on_MNIST for more info.
"""
#YOUR CODE HERE
raise NotImplementedError
# TODO: Run run_softmax_on_MNIST_mod3(), report the error rate
# 7. Classification Using Manually Crafted Features
## Dimensionality reduction via PCA ##
# TODO: First fill out the PCA functions in features.py as the below code depends on them.
n_components = 18
pcs = principal_components(train_x)
train_pca = project_onto_PC(train_x, pcs, n_components)
test_pca = project_onto_PC(test_x, pcs, n_components)
# train_pca (and test_pca) is a representation of our training (and test) data
# after projecting each example onto the first 18 principal components.
# TODO: Train your softmax regression model using (train_pca, train_y)
# and evaluate its accuracy on (test_pca, test_y).
# TODO: Use the plot_PC function in features.py to produce scatterplot
# of the first 100 MNIST images, as represented in the space spanned by the
# first 2 principal components found above.
plot_PC(train_x[range(100),], pcs, train_y[range(100)])
# TODO: Use the reconstruct_PC function in features.py to show
# the first and second MNIST images as reconstructed solely from
# their 18-dimensional principal component representation.
# Compare the reconstructed images with the originals.
firstimage_reconstructed = reconstruct_PC(train_pca[0, ], pcs, n_components, train_x)
plot_images(firstimage_reconstructed)
plot_images(train_x[0,])
secondimage_reconstructed = reconstruct_PC(train_pca[1, ], pcs, n_components, train_x)
plot_images(secondimage_reconstructed)
plot_images(train_x[1,])
## Cubic Kernel ##
# TODO: Find the 10-dimensional PCA representation of the training and test set
# TODO: First fill out cubicFeatures() function in features.py as the below code requires it.
train_cube = cubic_features(train_pca10)
test_cube = cubic_features(test_pca10)
# train_cube (and test_cube) is a representation of our training (and test) data
# after applying the cubic kernel feature mapping to the 10-dimensional PCA representations.
# TODO: Train your softmax regression model using (train_cube, train_y)
# and evaluate its accuracy on (test_cube, test_y).