MATLAB Report Assignment #11
Due: April 16, 2020
1 submission per pair of students
SVM classification of handwritten digits
In this assignment, you will re-use the part of the MNIST dataset seen in the PCA assignment. Download the MNIST data-file from D2L, and load it into MATLAB using
load mnist;
This data set contains 12,665 images of handwritten digits 0 and 1. If you can keep a secret: The first 5,923 images seem to be 0s, the rest is 1s. We will first use a small portion of the images to train a hard-margin SVM model, and see how it performs on the entire dataset. Then we will mess the data up a little (trying to make it non-separable), and see whether a soft-margin SVM would still do the job.
Document all your findings in a nice pdf-report and submit to D2L.
Hard SVM
1. Use the first N images in X as training examples with label -1; use the last N images in X as training examples with label +1.
2. Based on this subset, train a hard margin SVM classifier.
3. Apply the trained classifier to the entire data set. NO FOR LOOPS ALLOWED. You get a vector in R12,665; translate this vector into labels ±1. Plot the classifier values, the resulting labels, and compare against the true labels given in the secret, above. Repeat for different N, e.g. 10, 100, 1000. Discuss what happens in terms of training time (tic toc FTW), and error rate.
4. The trained w is a template that is compared against each of the images; visualize this template using imagesc(reshape(w, dims)), and discuss what you see: What kind of image will produce a strongly positive inner product, versus a strongly negative inner product?
Soft SVM
Now, let's mess with the training data: reload the MNIST data set, and create a copy, which we swap a few digits in (i.e., the first 100 zeros will actually look like ones, and the last 100 ones will actually look like zeros...):
load mnist; X2 = X; mess = 100;
X2(:, [1:mess (end-mess+1:end)]) = X2(:, [(end-mess+1:end) 1:mess]); % swap some 0/ 1s
1. Pick N = 4096 and train a hard-margin SVM, but now based on partly mislabeled data. What happens?
2. We can still try learning a soft-margin SVM, instead. λ ≈ 0.001 seems to work, but try different values, too.
3. Apply the trained classifier to the entire data set. Compare against the true labels given in the secret, above.
4. Visualize the template w using imagesc(reshape(w, dims)), and discuss what you see.