Exercise:Self-Taught Learning

From Ufldl

Jump to: navigation, search
 
Line 1: Line 1:
===Overview===
===Overview===
-
In this exercise, we will use the self-taught learning paradigm with the sparse autoencoder to build a classifier for handwritten digits.
+
In this exercise, we will use the self-taught learning paradigm with the sparse autoencoder and softmax classifier to build a classifier for handwritten digits.
-
In this context, the self-taught learning paradigm can be used as follows. First, the sparse autoencoder is trained on a unlabelled training dataset of images of handwritten digits. This produces feature detectors that correspond to the activation pattern for each hidden unit. The features learned are expected to be pen strokes to resemble segments of digits.  
+
You will be building upon your code from the earlier exercises. First, you will train your sparse autoencoder on an "unlabeled" training dataset of handwritten digits. This produces feature that are penstroke-like. We then extract these learned features from a labeled dataset of handwritten digits. These features will then be used as inputs to the softmax classifier that you wrote in the previous exercise.  
-
The sparse autoencoder is then used to augment the labelled training dataset, which is then used train the logistic regression classifier. For each example in the the labelled training dataset, <math>\textstyle (a_l^{(k)}</math>, forward propogation is used to is used to obtain the activation of the hidden units <math>\textstyle y^{(k)}</math>. The concatenated data point <math>\textstyle (a_l^{(k)}, y^{(k)})</math> is then used to train the logistic regression model. Finally, the test dataset is also augmented with hidden unit activations in the same manner, and run through the model to test the model's performance.
+
Concretely, for each example in the the labeled training dataset <math>\textstyle x_l</math>, we forward propagate the example to obtain the activation of the hidden units <math>\textstyle a^{(2)}</math>. We now represent this example using <math>\textstyle a^{(2)}</math> (the "replacement" representation), and use this to as the new feature representation with which to train the softmax classifier.  
-
To demonstrate the effectiveness of this method, we will train the sparse autoencoder on an unlabelled data set comprised out of only the digits 0 to 4, and then test them on the digits 5 to 9. The purpose of this is to demonstrate that self-taught learning can be surprisingly effective even if the unlabelled training data set does not contain items from our classification task. In practice, there is no good reason to discard data from our training dataset, so we would choose to use all images available to use for training the sparse autoencoder.
+
Finally, we also extract the same features from the test data to obtain predictions.
-
===Step One: Generate the input and test data sets===
+
In this exercise, our goal is to distinguish between the digits from 0 to 4.  We will use the digits 5 to 9 as our
 +
"unlabeled" dataset which which to learn the features; we will then use a labeled dataset with the digits 0 to 4 with
 +
which to train the softmax classifier.
-
Download and decompress <tt>self_taught_assign.zip</tt>, which contains starter code for this exercise. Additionally, you will need to download the datasets from the MNIST Handwritten Digit Database for this project. Download and decompress the files <tt>getLabelledData.m</tt> and <tt>getUnlabelledData.m</tt> to the <tt>mnist/</tt> path of the project folder. The functions to read data from the raw files have already been provided. You will need to write functions to separate the cases into separate sets containing the digits 0 to 4, and 5 to 9. Fill in your code in the function files  <tt>getLabelledData.m</tt> and <tt>getUnlabelledData.m</tt>. The results of your function will be visualized as three rows, corresponding to the unlabelled training set, the labelled training set, and the test set. Ensure that the first row only contains digits 0 to 4, and the second and third only contains digits 5 to 9.
+
In the starter code, we have provided a file '''<tt>stlExercise.m</tt>''' that will help walk you through the steps in this exercise.
-
[[File:selfTaughtInput.png]]
+
=== Dependencies ===
-
===Step Two: Train the sparse autoencoder===
+
The following additional files are required for this exercise:
 +
* [http://yann.lecun.com/exdb/mnist/ MNIST Dataset]
 +
* [[Using the MNIST Dataset | Support functions for loading MNIST in Matlab ]]
 +
* [http://ufldl.stanford.edu/wiki/resources/stl_exercise.zip Starter Code (stl_exercise.zip)]
-
Next we will train the input data set on the sparse autoencoder. Copy the <tt>sparseAutoencoderCost.m</tt> function from the previous assignment and run the training step. Use the frameworks from previous assignments to ensure that your code is working and vectorized, as no testing facilities are provided in this assignment. After doing so, running the training step should take about half an hour (on a reasonably fast computer). When it is completed, a visualization of pen strokes should be displayed.
+
You will also need your code from the following exercises:
 +
* [[Exercise:Sparse Autoencoder]]
 +
* [[Exercise:Vectorization]]
 +
* [[Exercise:Softmax Regression]]
-
Hint: This step takes a very long time, so you might want to avoid running it on subsequent trials! To do so, after running this step, run <tt>save('theta.mat', 'theta');</tt> from the command line. Then modify one line near the top of the <tt>trainSelfTaught.m</tt> script to set <tt>loadTheta = true;</tt>. This skips the autoencoder training step and loads the saved weights from disk.
+
''If you have not completed the exercises listed above, we strongly suggest you complete them first.''
 +
 
 +
===Step 1: Generate the input and test data sets===
 +
 
 +
Download and decompress <tt>[http://ufldl.stanford.edu/wiki/resources/stl_exercise.zip stl_exercise.zip]</tt>, which contains starter code for this exercise. Additionally, you will need to download the datasets from the MNIST Handwritten Digit Database for this project.
 +
 
 +
===Step 2: Train the sparse autoencoder===
 +
 
 +
Next, use the unlabeled data (the digits from 5 to 9) to train a sparse autoencoder, using the same <tt>sparseAutoencoderCost.m</tt> function as you had written in  the previous exercise. (From the earlier exercise, you should have a working and vectorized implementation of the sparse autoencoder.) For us, the training step took less than 25 minutes on a fast desktop. When training is complete, you should get a visualization of pen strokes like the image shown below:
[[File:selfTaughtFeatures.png]]
[[File:selfTaughtFeatures.png]]
-
===Step Three: Training the logistic regression model===
+
Informally, the features learned by the sparse autoencoder should correspond to penstrokes.
 +
 
 +
===Step 3: Extracting features===
 +
 
 +
After the sparse autoencoder is trained, you will use it to extract features from the handwritten digit images.
 +
 
 +
Complete <tt>feedForwardAutoencoder.m</tt> to produce a matrix whose columns correspond to activations of the hidden layer for each example, i.e., the vector <math>a^{(2)}</math> corresponding to activation of layer 2.  (Recall that we treat the inputs as layer 1).
 +
 
 +
After completing this step, calling <tt>feedForwardAutoencoder.m</tt> should convert the raw image data to hidden unit activations <math>a^{(2)}</math>.
 +
 
 +
===Step 4: Training and testing the logistic regression model===
 +
 
 +
Use your code from the softmax exercise (<tt>softmaxTrain.m</tt>) to train a softmax classifier using the training set features (<tt>trainFeatures</tt>) and labels (<tt>trainLabels</tt>).
 +
 
 +
===Step 5: Classifying on the test set===
 +
 
 +
Finally, complete the code to make predictions on the test set (<tt>testFeatures</tt>) and see how your learned features perform! If you've done all the steps correctly, you should get an accuracy of about '''98%''' percent.
-
After the sparse autoencoder is trained, we can use it to detect pen strokes in images. Fill in <tt>featureActivation.m</tt> to use forward propagation to determine the activation of the hidden units for a given input. This is the same as performing forward computation without the output layer. Your function should compute the vector:
+
As a comparison, when ''raw pixels'' are used (instead of the learned features), we obtained a test accuracy of only around 96% (for the same train and test sets).
-
:<math>\begin{align}
+
[[Category:Exercises]]
-
z &= W^{(1)} a^{(1)} + b^{(1)}
+
-
\end{align}</math>
+
-
===Step Four: Training and testing the logistic regression model===
 
-
After completing these steps, running the entire script in <tt>trainSelfTaught.m</tt> will use your sparse autoencoder to train the logistic model, then measure how well this system performs on the test set. Statistics about the model will be displayed afterwards. If you've done all the steps correctly, you should get an accuracy of about X percent.
+
{{STL}}

Latest revision as of 11:02, 26 May 2011

Personal tools