Feature Extraction Workflow
This example shows a complete workflow for feature extraction from image data.
Obtain Data
This example uses the MNIST image data [1], which consists of images of handwritten digits. The images are 28-by-28 pixels in gray scale. Each image has an associated label from 0 through 9, which is the digit that the image represents.
Begin by obtaining image and label data from
https://yann.lecun.com/exdb/mnist/
Unzip the files. For better performance on this long example, use the test data as training data and the training data as test data.
imageFileName = 't10k-images.idx3-ubyte'; labelFileName = 't10k-labels.idx1-ubyte';
Process the files to load them in the workspace. The code for this processing function appears at the end of this example.
[Xtrain,LabelTrain] = processMNISTdata(imageFileName,labelFileName);
Read MNIST image data... Number of images in the dataset: 10000 ... Each image is of 28 by 28 pixels... The image data is read to a matrix of dimensions: 10000 by 784... End of reading image data. Read MNIST label data... Number of labels in the dataset: 10000 ... The label data is read to a matrix of dimensions: 10000 by 1... End of reading label data.
View a few of the images.
rng('default') % For reproducibility numrows = size(Xtrain,1); ims = randi(numrows,4,1); imgs = Xtrain(ims,:); for i = 1:4 pp{i} = reshape(imgs(i,:),28,28); end ppf = [pp{1},pp{2};pp{3},pp{4}]; imshow(ppf);
Choose New Feature Dimensions
There are several considerations in choosing the number of features to extract:
More features use more memory and computational time.
Fewer features can produce a poor classifier.
For this example, choose 100 features.
q = 100;
Extract Features
There are two feature extraction functions, sparsefilt
and rica
. Begin with the sparsefilt
function. Set the number of iterations to 10 so that the extraction does not take too long.
Typically, you get good results by running the sparsefilt
algorithm for a few iterations to a few hundred iterations. Running the algorithm for too many iterations can lead to decreased classification accuracy, a type of overfitting problem.
Use sparsefilt
to obtain the sparse filtering model while using 10 iterations.
Mdl = sparsefilt(Xtrain,q,'IterationLimit',10);
Warning: Solver LBFGS was not able to converge to a solution.
sparsefilt
warns that the internal LBFGS optimizer did not converge. The optimizer did not converge because you set the iteration limit to 10. Nevertheless, you can use the result to train a classifier.
Create Classifier
Transform the original data into the new feature representation.
NewX = transform(Mdl,Xtrain);
Train a linear classifier based on the transformed data and the correct classification labels in LabelTrain
. The accuracy of the learned model is sensitive to the fitcecoc
regularization parameter Lambda
. Try to find the best value for Lambda
by using the OptimizeHyperparameters
name-value pair. Be aware that this optimization takes time. If you have a Parallel Computing Toolbox™ license, use parallel computing for faster execution. If you don't have a parallel license, remove the UseParallel
calls before running this script.
t = templateLinear('Solver','lbfgs'); options = struct('UseParallel',true); Cmdl = fitcecoc(NewX,LabelTrain,'Learners',t, ... 'OptimizeHyperparameters',{'Lambda'}, ... 'HyperparameterOptimizationOptions',options);
Starting parallel pool (parpool) using the 'Processes' profile ... Connected to parallel pool with 6 workers. Copying objective function to workers... Done copying objective function to workers. |================================================================================================| | Iter | Active | Eval | Objective | Objective | BestSoFar | BestSoFar | Lambda | | | workers | result | | runtime | (observed) | (estim.) | | |================================================================================================| | 1 | 6 | Best | 0.5777 | 8.6141 | 0.5777 | 0.5777 | 0.20606 | | 2 | 5 | Accept | 0.8865 | 9.3095 | 0.2041 | 0.27206 | 8.8234 | | 3 | 5 | Best | 0.2041 | 10.856 | 0.2041 | 0.27206 | 0.026804 | | 4 | 6 | Best | 0.1087 | 22.758 | 0.1087 | 0.10873 | 1.7309e-09 | | 5 | 6 | Accept | 0.2 | 8.2175 | 0.1087 | 0.10873 | 0.024862 | | 6 | 5 | Best | 0.0962 | 22.518 | 0.0962 | 0.096222 | 0.0002442 | | 7 | 5 | Accept | 0.2073 | 8.0071 | 0.0962 | 0.096222 | 0.029034 | | 8 | 6 | Accept | 0.1072 | 19.941 | 0.0962 | 0.096222 | 2.037e-08 | | 9 | 6 | Accept | 0.1254 | 12.116 | 0.0962 | 0.096211 | 0.0030655 | | 10 | 6 | Accept | 0.0978 | 35.453 | 0.0962 | 0.096199 | 8.0495e-06 | | 11 | 6 | Accept | 0.1092 | 18.766 | 0.0962 | 0.096239 | 1.0018e-09 | | 12 | 6 | Accept | 0.103 | 25.864 | 0.0962 | 0.096298 | 3.1423e-07 | | 13 | 6 | Best | 0.0918 | 26.211 | 0.0918 | 0.091716 | 7.1191e-05 | | 14 | 6 | Accept | 0.1075 | 15.686 | 0.0918 | 0.091849 | 0.0007934 | | 15 | 6 | Accept | 0.1091 | 19.43 | 0.0918 | 0.091847 | 6.6474e-09 | | 16 | 6 | Accept | 0.1009 | 29.873 | 0.0918 | 0.091865 | 1.8056e-06 | | 17 | 6 | Accept | 0.106 | 24.035 | 0.0918 | 0.091861 | 7.7786e-08 | | 18 | 6 | Accept | 0.0926 | 24.591 | 0.0918 | 0.092543 | 0.00010947 | | 19 | 6 | Accept | 0.0943 | 29.077 | 0.0918 | 0.092666 | 5.1308e-05 | | 20 | 6 | Accept | 0.0932 | 27.617 | 0.0918 | 0.092714 | 6.3777e-05 | |================================================================================================| | Iter | Active | Eval | Objective | Objective | BestSoFar | BestSoFar | Lambda | | | workers | result | | runtime | (observed) | (estim.) | | |================================================================================================| | 21 | 6 | Accept | 0.0948 | 32.018 | 0.0918 | 0.092744 | 2.4161e-05 | | 22 | 6 | Accept | 0.8865 | 6.2645 | 0.0918 | 0.092743 | 1.6582 | | 23 | 5 | Accept | 0.0929 | 26.604 | 0.0918 | 0.092804 | 9.9377e-05 | | 24 | 5 | Accept | 0.1483 | 10.882 | 0.0918 | 0.092804 | 0.0071672 | | 25 | 6 | Accept | 0.0937 | 30.281 | 0.0918 | 0.092808 | 4.9079e-05 | | 26 | 6 | Accept | 0.0932 | 25.531 | 0.0918 | 0.092698 | 0.00011247 | | 27 | 6 | Accept | 0.0924 | 25.188 | 0.0918 | 0.092627 | 0.00010419 | | 28 | 6 | Accept | 0.115 | 14.017 | 0.0918 | 0.092633 | 0.0015315 | | 29 | 6 | Accept | 0.1017 | 29.549 | 0.0918 | 0.092634 | 7.3706e-07 | | 30 | 6 | Accept | 0.1054 | 22.393 | 0.0918 | 0.092634 | 3.9819e-08 |
__________________________________________________________ Optimization completed. MaxObjectiveEvaluations of 30 reached. Total function evaluations: 30 Total elapsed time: 120.2394 seconds Total objective function evaluation time: 621.6674 Best observed feasible point: Lambda __________ 7.1191e-05 Observed objective function value = 0.0918 Estimated objective function value = 0.092818 Function evaluation time = 26.211 Best estimated feasible point (according to models): Lambda __________ 9.9377e-05 Estimated objective function value = 0.092634 Estimated function evaluation time = 25.8187
Evaluate Classifier
Check the error of the classifier when applied to test data. First, load the test data.
imageFileName = 'train-images.idx3-ubyte'; labelFileName = 'train-labels.idx1-ubyte'; [Xtest,LabelTest] = processMNISTdata(imageFileName,labelFileName);
Read MNIST image data... Number of images in the dataset: 60000 ... Each image is of 28 by 28 pixels... The image data is read to a matrix of dimensions: 60000 by 784... End of reading image data. Read MNIST label data... Number of labels in the dataset: 60000 ... The label data is read to a matrix of dimensions: 60000 by 1... End of reading label data.
Calculate the classification loss when applying the classifier to the test data.
TestX = transform(Mdl,Xtest); Loss = loss(Cmdl,TestX,LabelTest)
Loss = 0.1009
Did this transformation result in a better classifier than one trained on the original data? Create a classifier based on the original training data and evaluate its loss.
Omdl = fitcecoc(Xtrain,LabelTrain,'Learners',t, ... 'OptimizeHyperparameters',{'Lambda'}, ... 'HyperparameterOptimizationOptions',options);
Copying objective function to workers...
Warning: Files that have already been attached are being ignored. To see which files are attached see the 'AttachedFiles' property of the parallel pool.
Done copying objective function to workers. |================================================================================================| | Iter | Active | Eval | Objective | Objective | BestSoFar | BestSoFar | Lambda | | | workers | result | | runtime | (observed) | (estim.) | | |================================================================================================| | 1 | 6 | Best | 0.0792 | 38.388 | 0.0792 | 0.0792 | 1.3269e-06 | | 2 | 6 | Accept | 0.0792 | 38.794 | 0.0792 | 0.0792 | 3.8643e-09 | | 3 | 6 | Accept | 0.0797 | 106.79 | 0.0792 | 0.0792 | 0.058397 | | 4 | 6 | Accept | 0.08 | 118.73 | 0.0792 | 0.079255 | 0.011605 | | 5 | 6 | Accept | 0.0796 | 91.535 | 0.0792 | 0.079246 | 8.8714e-05 | | 6 | 6 | Accept | 0.0792 | 37.754 | 0.0792 | 0.079198 | 7.1653e-08 | | 7 | 6 | Accept | 0.0792 | 37.645 | 0.0792 | 0.0792 | 2.5694e-07 | | 8 | 6 | Accept | 0.0792 | 36.984 | 0.0792 | 0.0792 | 3.3746e-08 | | 9 | 6 | Accept | 0.0803 | 133.05 | 0.0792 | 0.0792 | 0.0010049 | | 10 | 6 | Accept | 0.0792 | 37.367 | 0.0792 | 0.0792 | 1.0013e-09 | | 11 | 6 | Best | 0.0778 | 188.44 | 0.0778 | 0.0778 | 3.2973 | | 12 | 6 | Accept | 0.0792 | 37.07 | 0.0778 | 0.0778 | 1.0195e-08 | | 13 | 6 | Accept | 0.0792 | 37.573 | 0.0778 | 0.0778 | 6.6143e-07 | | 14 | 6 | Accept | 0.079 | 37.455 | 0.0778 | 0.0778 | 6.2409e-06 | | 15 | 6 | Best | 0.0749 | 220.81 | 0.0749 | 0.076311 | 6.805 | | 16 | 6 | Accept | 0.0792 | 37.151 | 0.0749 | 0.076259 | 1.7893e-09 | | 17 | 6 | Accept | 0.0791 | 46.448 | 0.0749 | 0.076219 | 2.2606e-05 | | 18 | 6 | Accept | 0.0807 | 109.6 | 0.0749 | 0.076173 | 0.025251 | | 19 | 6 | Accept | 0.0791 | 133.05 | 0.0749 | 0.076192 | 0.93487 | | 20 | 6 | Accept | 0.0762 | 229.75 | 0.0749 | 0.076073 | 9.956 | |================================================================================================| | Iter | Active | Eval | Objective | Objective | BestSoFar | BestSoFar | Lambda | | | workers | result | | runtime | (observed) | (estim.) | | |================================================================================================| | 21 | 6 | Accept | 0.078 | 106.83 | 0.0749 | 0.076281 | 0.25166 | | 22 | 6 | Best | 0.0748 | 219.11 | 0.0748 | 0.075799 | 9.996 | | 23 | 6 | Best | 0.0748 | 223.9 | 0.0748 | 0.075541 | 9.9783 | | 24 | 6 | Accept | 0.0761 | 216.46 | 0.0748 | 0.075657 | 9.9883 | | 25 | 6 | Accept | 0.0792 | 36.716 | 0.0748 | 0.075648 | 3.0333e-06 | | 26 | 6 | Accept | 0.0792 | 39.865 | 0.0748 | 0.075639 | 1.2161e-05 | | 27 | 6 | Accept | 0.0759 | 214.76 | 0.0748 | 0.075629 | 7.466 | | 28 | 6 | Accept | 0.0807 | 123.04 | 0.0748 | 0.075624 | 0.0031395 | | 29 | 6 | Accept | 0.0793 | 123.12 | 0.0748 | 0.075622 | 0.00027632 | | 30 | 6 | Accept | 0.0793 | 102.55 | 0.0748 | 0.075618 | 0.13165 |
__________________________________________________________ Optimization completed. MaxObjectiveEvaluations of 30 reached. Total function evaluations: 30 Total elapsed time: 581.3338 seconds Total objective function evaluation time: 3160.7509 Best observed feasible point: Lambda ______ 9.9783 Observed objective function value = 0.0748 Estimated objective function value = 0.07562 Function evaluation time = 223.899 Best estimated feasible point (according to models): Lambda ______ 9.996 Estimated objective function value = 0.075618 Estimated function evaluation time = 222.605
Losso = loss(Omdl,Xtest,LabelTest)
Losso = 0.0863
The classifier based on sparse filtering has a somewhat higher loss than the classifier based on the original data. However, the classifier uses only 100 features rather than the 784 features in the original data, and is much faster to create. Try to make a better sparse filtering classifier by increasing q
from 100 to 200, which is still far less than 784.
q = 200;
Mdl2 = sparsefilt(Xtrain,q,'IterationLimit',10);
Warning: Solver LBFGS was not able to converge to a solution.
NewX = transform(Mdl2,Xtrain); TestX = transform(Mdl2,Xtest); Cmdl = fitcecoc(NewX,LabelTrain,'Learners',t, ... 'OptimizeHyperparameters',{'Lambda'}, ... 'HyperparameterOptimizationOptions',options);
Copying objective function to workers...
Warning: Files that have already been attached are being ignored. To see which files are attached see the 'AttachedFiles' property of the parallel pool.
Done copying objective function to workers. |================================================================================================| | Iter | Active | Eval | Objective | Objective | BestSoFar | BestSoFar | Lambda | | | workers | result | | runtime | (observed) | (estim.) | | |================================================================================================| | 1 | 6 | Best | 0.6695 | 8.6442 | 0.6695 | 0.6695 | 0.29365 | | 2 | 6 | Best | 0.2547 | 9.5273 | 0.2547 | 0.4621 | 0.098317 | | 3 | 6 | Best | 0.1599 | 12.82 | 0.1599 | 0.36137 | 0.026351 | | 4 | 6 | Best | 0.0683 | 13.941 | 0.0683 | 0.2881 | 1.9863e-08 | | 5 | 6 | Best | 0.0682 | 13.249 | 0.0682 | 0.158 | 3.5754e-09 | | 6 | 6 | Accept | 0.0786 | 24.139 | 0.0682 | 0.13863 | 0.0012855 | | 7 | 6 | Best | 0.0662 | 24.071 | 0.0662 | 0.10796 | 1.2755e-07 | | 8 | 6 | Accept | 0.0681 | 13.257 | 0.0662 | 0.066211 | 7.4448e-09 | | 9 | 6 | Accept | 0.0677 | 13.403 | 0.0662 | 0.066209 | 2.3425e-09 | | 10 | 6 | Accept | 0.8865 | 9.8272 | 0.0662 | 0.066054 | 9.9964 | | 11 | 6 | Best | 0.0659 | 33.745 | 0.0659 | 0.066333 | 0.00033635 | | 12 | 6 | Accept | 0.0679 | 14.063 | 0.0659 | 0.065902 | 1.6591e-08 | | 13 | 6 | Accept | 0.0676 | 13.204 | 0.0659 | 0.0659 | 1.0006e-09 | | 14 | 6 | Best | 0.0632 | 59.593 | 0.0632 | 0.06719 | 9.0327e-06 | | 15 | 6 | Accept | 0.0673 | 14.441 | 0.0632 | 0.063208 | 2.6545e-08 | | 16 | 6 | Best | 0.0593 | 51.035 | 0.0593 | 0.05931 | 6.5008e-05 | | 17 | 6 | Accept | 0.1059 | 17.642 | 0.0593 | 0.059299 | 0.0054064 | | 18 | 6 | Accept | 0.0659 | 19.663 | 0.0593 | 0.059299 | 6.276e-08 | | 19 | 6 | Accept | 0.0681 | 12.896 | 0.0593 | 0.059299 | 1.0012e-09 | | 20 | 6 | Accept | 0.0663 | 31.697 | 0.0593 | 0.0593 | 5.9516e-07 | |================================================================================================| | Iter | Active | Eval | Objective | Objective | BestSoFar | BestSoFar | Lambda | | | workers | result | | runtime | (observed) | (estim.) | | |================================================================================================| | 21 | 6 | Accept | 0.0663 | 28.508 | 0.0593 | 0.059299 | 2.6603e-07 | | 22 | 6 | Accept | 0.0607 | 41.599 | 0.0593 | 0.059302 | 0.0001531 | | 23 | 6 | Accept | 0.0643 | 44.059 | 0.0593 | 0.059304 | 2.4159e-06 | | 24 | 6 | Accept | 0.0604 | 46.306 | 0.0593 | 0.059366 | 9.9279e-05 | | 25 | 6 | Accept | 0.0646 | 38.356 | 0.0593 | 0.059364 | 1.2098e-06 | | 26 | 6 | Accept | 0.0603 | 60.741 | 0.0593 | 0.059395 | 2.6204e-05 | | 27 | 6 | Accept | 0.0629 | 52.06 | 0.0593 | 0.059382 | 4.8483e-06 | | 28 | 6 | Accept | 0.0597 | 54.43 | 0.0593 | 0.0595 | 4.3026e-05 | | 29 | 6 | Accept | 0.0618 | 39.214 | 0.0593 | 0.0595 | 0.00020575 | | 30 | 6 | Accept | 0.0612 | 59.873 | 0.0593 | 0.059492 | 1.6115e-05 |
__________________________________________________________ Optimization completed. MaxObjectiveEvaluations of 30 reached. Total function evaluations: 30 Total elapsed time: 170.1119 seconds Total objective function evaluation time: 876 Best observed feasible point: Lambda __________ 6.5008e-05 Observed objective function value = 0.0593 Estimated objective function value = 0.059492 Function evaluation time = 51.0348 Best estimated feasible point (according to models): Lambda __________ 6.5008e-05 Estimated objective function value = 0.059492 Estimated function evaluation time = 50.9958
Loss2 = loss(Cmdl,TestX,LabelTest)
Loss2 = 0.0678
This time the classification loss is lower than that of the original data classifier.
Try RICA
Try the other feature extraction function, rica
. Extract 200 features, create a classifier, and examine its loss on the test data. Use more iterations for the rica
function, because rica
can perform better with more iterations than sparsefilt
uses.
Often prior to feature extraction, you "prewhiten" the input data as a data preprocessing step. The prewhitening step includes two transforms, decorrelation and standardization, which make the predictors have zero mean and identity covariance. rica
supports only the standardization transform. You use the Standardize
name-value pair argument to make the predictors have zero mean and unit variance. Alternatively, you can transform images for contrast normalization individually by applying the zscore
transformation before calling sparsefilt
or rica
.
Mdl3 = rica(Xtrain,q,'IterationLimit',400,'Standardize',true);
Warning: Solver LBFGS was not able to converge to a solution.
NewX = transform(Mdl3,Xtrain); TestX = transform(Mdl3,Xtest); Cmdl = fitcecoc(NewX,LabelTrain,'Learners',t, ... 'OptimizeHyperparameters',{'Lambda'}, ... 'HyperparameterOptimizationOptions',options);
Copying objective function to workers...
Warning: Files that have already been attached are being ignored. To see which files are attached see the 'AttachedFiles' property of the parallel pool.
Done copying objective function to workers. |================================================================================================| | Iter | Active | Eval | Objective | Objective | BestSoFar | BestSoFar | Lambda | | | workers | result | | runtime | (observed) | (estim.) | | |================================================================================================| | 1 | 6 | Best | 0.1186 | 17.927 | 0.1186 | 0.1186 | 8.3466 | | 2 | 6 | Best | 0.0829 | 21.361 | 0.0829 | 0.084699 | 2.6015e-09 | | 3 | 6 | Best | 0.0823 | 34.305 | 0.0823 | 0.082303 | 1.5451e-06 | | 4 | 6 | Best | 0.069 | 34.884 | 0.069 | 0.088196 | 0.33744 | | 5 | 6 | Accept | 0.0812 | 41.879 | 0.069 | 0.0868 | 0.00014259 | | 6 | 6 | Accept | 0.0829 | 21.993 | 0.069 | 0.069002 | 8.2324e-09 | | 7 | 6 | Accept | 0.0741 | 53.957 | 0.069 | 0.069002 | 0.0014702 | | 8 | 6 | Accept | 0.0824 | 20.984 | 0.069 | 0.069002 | 1.5123e-07 | | 9 | 6 | Accept | 0.082 | 20.557 | 0.069 | 0.069004 | 1.0003e-09 | | 10 | 6 | Best | 0.064 | 46.988 | 0.064 | 0.063995 | 0.063105 | | 11 | 6 | Accept | 0.0824 | 20.795 | 0.064 | 0.06399 | 3.761e-08 | | 12 | 6 | Accept | 0.0666 | 41.578 | 0.064 | 0.064022 | 0.20394 | | 13 | 6 | Accept | 0.0822 | 32.505 | 0.064 | 0.064019 | 1.391e-05 | | 14 | 6 | Accept | 0.0685 | 48.466 | 0.064 | 0.064045 | 0.0073912 | | 15 | 6 | Accept | 0.0663 | 45.251 | 0.064 | 0.064208 | 0.020432 | | 16 | 6 | Accept | 0.0652 | 45.406 | 0.064 | 0.064546 | 0.08758 | | 17 | 6 | Accept | 0.0838 | 25.477 | 0.064 | 0.064637 | 1.5766 | | 18 | 6 | Best | 0.0638 | 44.514 | 0.0638 | 0.064404 | 0.072308 | | 19 | 6 | Accept | 0.0647 | 45.146 | 0.0638 | 0.06446 | 0.054238 | | 20 | 6 | Accept | 0.065 | 45.968 | 0.0638 | 0.064551 | 0.053822 | |================================================================================================| | Iter | Active | Eval | Objective | Objective | BestSoFar | BestSoFar | Lambda | | | workers | result | | runtime | (observed) | (estim.) | | |================================================================================================| | 21 | 6 | Accept | 0.0832 | 31.813 | 0.0638 | 0.06455 | 4.6418e-07 | | 22 | 6 | Best | 0.0636 | 47.072 | 0.0636 | 0.064405 | 0.068073 | | 23 | 6 | Accept | 0.1009 | 20.674 | 0.0636 | 0.064404 | 3.9634 | | 24 | 6 | Accept | 0.0818 | 34.211 | 0.0636 | 0.064405 | 4.2826e-05 | | 25 | 6 | Accept | 0.0641 | 45.137 | 0.0636 | 0.064363 | 0.061006 | | 26 | 6 | Accept | 0.0819 | 31.684 | 0.0636 | 0.064362 | 4.6987e-06 | | 27 | 6 | Accept | 0.0823 | 21.323 | 0.0636 | 0.06436 | 1.7867e-08 | | 28 | 6 | Accept | 0.0818 | 20.483 | 0.0636 | 0.064359 | 7.7797e-08 | | 29 | 6 | Accept | 0.0826 | 21.028 | 0.0636 | 0.064357 | 1.496e-09 | | 30 | 6 | Accept | 0.0762 | 30.05 | 0.0636 | 0.064347 | 0.73746 |
__________________________________________________________ Optimization completed. MaxObjectiveEvaluations of 30 reached. Total function evaluations: 30 Total elapsed time: 190.4106 seconds Total objective function evaluation time: 1013.415 Best observed feasible point: Lambda ________ 0.068073 Observed objective function value = 0.0636 Estimated objective function value = 0.064347 Function evaluation time = 47.0725 Best estimated feasible point (according to models): Lambda ________ 0.068073 Estimated objective function value = 0.064347 Estimated function evaluation time = 45.7964
Loss3 = loss(Cmdl,TestX,LabelTest)
Loss3 = 0.0735
The rica
-based classifier has somewhat higher test loss compared to the sparse filtering classifier.
Try More Features
The feature extraction functions have few tuning parameters. One parameter that can affect results is the number of requested features. See how well classifiers work when based on 1000 features, rather than the 200 features previously tried, or the 784 features in the original data. Using more features than appear in the original data is called "overcomplete" learning. Conversely, using fewer features is called "undercomplete" learning. Overcomplete learning can lead to increased classification accuracy, while undercomplete learning can save memory and time.
q = 1000;
Mdl4 = sparsefilt(Xtrain,q,'IterationLimit',10);
Warning: Solver LBFGS was not able to converge to a solution.
NewX = transform(Mdl4,Xtrain); TestX = transform(Mdl4,Xtest); Cmdl = fitcecoc(NewX,LabelTrain,'Learners',t, ... 'OptimizeHyperparameters',{'Lambda'}, ... 'HyperparameterOptimizationOptions',options);
Copying objective function to workers...
Warning: Files that have already been attached are being ignored. To see which files are attached see the 'AttachedFiles' property of the parallel pool.
Done copying objective function to workers. |================================================================================================| | Iter | Active | Eval | Objective | Objective | BestSoFar | BestSoFar | Lambda | | | workers | result | | runtime | (observed) | (estim.) | | |================================================================================================| | 1 | 6 | Best | 0.7872 | 33.23 | 0.7872 | 0.7872 | 0.67927 | | 2 | 6 | Best | 0.2549 | 36.766 | 0.2549 | 0.52105 | 0.092083 | | 3 | 6 | Best | 0.0403 | 44.337 | 0.0403 | 0.3608 | 7.2769e-09 | | 4 | 6 | Accept | 0.0404 | 42.304 | 0.0403 | 0.04032 | 1.9894e-08 | | 5 | 6 | Accept | 0.2377 | 39.684 | 0.0403 | 0.040315 | 0.076841 | | 6 | 6 | Best | 0.04 | 50.278 | 0.04 | 0.040023 | 8.1952e-08 | | 7 | 6 | Accept | 0.0411 | 42.65 | 0.04 | 0.040307 | 1.0011e-09 | | 8 | 6 | Best | 0.0388 | 133.03 | 0.0388 | 0.040199 | 7.8451e-07 | | 9 | 6 | Accept | 0.0459 | 150.47 | 0.0388 | 0.040751 | 0.00034164 | | 10 | 6 | Accept | 0.0411 | 42.525 | 0.0388 | 0.040485 | 2.3961e-09 | | 11 | 6 | Accept | 0.0985 | 77.303 | 0.0388 | 0.040524 | 0.0051411 | | 12 | 6 | Accept | 0.0698 | 108.61 | 0.0388 | 0.040495 | 0.0014614 | | 13 | 6 | Accept | 0.0403 | 102.97 | 0.0388 | 0.038842 | 2.5146e-07 | | 14 | 6 | Best | 0.0372 | 250.45 | 0.0372 | 0.037146 | 5.0247e-05 | | 15 | 6 | Best | 0.037 | 253.46 | 0.037 | 0.037129 | 4.3826e-05 | | 16 | 6 | Accept | 0.0408 | 43.793 | 0.037 | 0.037134 | 3.9641e-08 | | 17 | 6 | Accept | 0.0408 | 42.125 | 0.037 | 0.037137 | 4.2716e-09 | | 18 | 6 | Accept | 0.0376 | 227.25 | 0.037 | 0.037058 | 5.7928e-06 | | 19 | 6 | Accept | 0.0387 | 201.19 | 0.037 | 0.037089 | 0.00012709 | | 20 | 6 | Accept | 0.0383 | 171.39 | 0.037 | 0.037087 | 2.0436e-06 | |================================================================================================| | Iter | Active | Eval | Objective | Objective | BestSoFar | BestSoFar | Lambda | | | workers | result | | runtime | (observed) | (estim.) | | |================================================================================================| | 21 | 6 | Accept | 0.0383 | 203.57 | 0.037 | 0.03709 | 3.521e-06 | | 22 | 6 | Accept | 0.039 | 158.63 | 0.037 | 0.037101 | 1.4692e-06 | | 23 | 6 | Best | 0.0369 | 258.05 | 0.0369 | 0.037028 | 1.515e-05 | | 24 | 6 | Accept | 0.0378 | 241.52 | 0.0369 | 0.037014 | 8.1138e-06 | | 25 | 6 | Best | 0.0362 | 267.9 | 0.0362 | 0.036535 | 2.4979e-05 | | 26 | 6 | Accept | 0.8865 | 44.112 | 0.0362 | 0.036942 | 9.9751 | | 27 | 6 | Accept | 0.0377 | 242.47 | 0.0362 | 0.037197 | 6.6549e-05 | | 28 | 6 | Accept | 0.0409 | 43.351 | 0.0362 | 0.037197 | 1.1966e-08 | | 29 | 6 | Accept | 0.0399 | 67.131 | 0.0362 | 0.037197 | 1.3523e-07 | | 30 | 6 | Accept | 0.0362 | 266.92 | 0.0362 | 0.036276 | 2.5057e-05 |
__________________________________________________________ Optimization completed. MaxObjectiveEvaluations of 30 reached. Total function evaluations: 30 Total elapsed time: 779.273 seconds Total objective function evaluation time: 3887.4704 Best observed feasible point: Lambda __________ 2.4979e-05 Observed objective function value = 0.0362 Estimated objective function value = 0.036276 Function evaluation time = 267.8985 Best estimated feasible point (according to models): Lambda __________ 2.4979e-05 Estimated objective function value = 0.036276 Estimated function evaluation time = 266.792
Loss4 = loss(Cmdl,TestX,LabelTest)
Loss4 = 0.0449
The classifier based on overcomplete sparse filtering with 1000 extracted features has the lowest test loss of any classifier yet tested.
Mdl5 = rica(Xtrain,q,'IterationLimit',400,'Standardize',true);
Warning: Solver LBFGS was not able to converge to a solution.
NewX = transform(Mdl5,Xtrain); TestX = transform(Mdl5,Xtest); Cmdl = fitcecoc(NewX,LabelTrain,'Learners',t, ... 'OptimizeHyperparameters',{'Lambda'}, ... 'HyperparameterOptimizationOptions',options);
Copying objective function to workers...
Warning: Files that have already been attached are being ignored. To see which files are attached see the 'AttachedFiles' property of the parallel pool.
Done copying objective function to workers. |================================================================================================| | Iter | Active | Eval | Objective | Objective | BestSoFar | BestSoFar | Lambda | | | workers | result | | runtime | (observed) | (estim.) | | |================================================================================================| | 1 | 6 | Best | 0.0795 | 35.93 | 0.0795 | 0.0795 | 3.5819e-09 | | 2 | 6 | Accept | 0.0795 | 36.166 | 0.0795 | 0.0795 | 1.5721e-08 | | 3 | 6 | Accept | 0.1037 | 94.354 | 0.0795 | 0.079498 | 4.8039 | | 4 | 6 | Best | 0.0789 | 126.29 | 0.0789 | 0.078905 | 3.2079e-06 | | 5 | 6 | Accept | 0.0794 | 36.198 | 0.0789 | 0.078906 | 1.0004e-09 | | 6 | 6 | Accept | 0.0791 | 118.38 | 0.0789 | 0.078889 | 3.2745e-05 | | 7 | 6 | Accept | 0.0794 | 120.31 | 0.0789 | 0.078899 | 2.4649e-05 | | 8 | 6 | Accept | 0.079 | 228.54 | 0.0789 | 0.078948 | 0.00024575 | | 9 | 6 | Best | 0.0691 | 235.34 | 0.0691 | 0.069116 | 0.0087005 | | 10 | 6 | Accept | 0.0796 | 121.82 | 0.0691 | 0.06913 | 5.9774e-07 | | 11 | 6 | Accept | 0.0799 | 36.466 | 0.0691 | 0.069129 | 1.8621e-07 | | 12 | 6 | Accept | 0.0795 | 137.37 | 0.0691 | 0.069128 | 1.3617e-06 | | 13 | 6 | Accept | 0.078 | 119.34 | 0.0691 | 0.069144 | 7.092e-06 | | 14 | 6 | Accept | 0.079 | 127.47 | 0.0691 | 0.069147 | 3.9936e-06 | | 15 | 6 | Best | 0.0672 | 207.24 | 0.0672 | 0.067318 | 0.021943 | | 16 | 6 | Accept | 0.0715 | 163.53 | 0.0672 | 0.067096 | 0.43709 | | 17 | 6 | Accept | 0.069 | 231.93 | 0.0672 | 0.067127 | 0.009768 | | 18 | 6 | Accept | 0.0691 | 224.28 | 0.0672 | 0.067183 | 0.010904 | | 19 | 6 | Accept | 0.0684 | 223.5 | 0.0672 | 0.067135 | 0.011229 | | 20 | 6 | Accept | 0.0694 | 234.72 | 0.0672 | 0.067129 | 0.0088152 | |================================================================================================| | Iter | Active | Eval | Objective | Objective | BestSoFar | BestSoFar | Lambda | | | workers | result | | runtime | (observed) | (estim.) | | |================================================================================================| | 21 | 6 | Accept | 0.0676 | 199.85 | 0.0672 | 0.067209 | 0.02834 | | 22 | 6 | Best | 0.0651 | 216.28 | 0.0651 | 0.065247 | 0.094097 | | 23 | 6 | Accept | 0.0655 | 208.49 | 0.0651 | 0.06518 | 0.05859 | | 24 | 6 | Accept | 0.067 | 201.15 | 0.0651 | 0.06573 | 0.14014 | | 25 | 6 | Accept | 0.0663 | 206.16 | 0.0651 | 0.065789 | 0.050114 | | 26 | 6 | Accept | 0.0794 | 35.295 | 0.0651 | 0.065794 | 5.3306e-08 | | 27 | 6 | Accept | 0.1255 | 76.98 | 0.0651 | 0.065847 | 9.9927 | | 28 | 6 | Accept | 0.0717 | 279.2 | 0.0651 | 0.06583 | 0.0021402 | | 29 | 6 | Accept | 0.0795 | 35.036 | 0.0651 | 0.065827 | 1.7333e-09 | | 30 | 6 | Accept | 0.0664 | 218.21 | 0.0651 | 0.065887 | 0.10658 |
__________________________________________________________ Optimization completed. MaxObjectiveEvaluations of 30 reached. Total function evaluations: 30 Total elapsed time: 866.1193 seconds Total objective function evaluation time: 4535.8246 Best observed feasible point: Lambda ________ 0.094097 Observed objective function value = 0.0651 Estimated objective function value = 0.065934 Function evaluation time = 216.2758 Best estimated feasible point (according to models): Lambda _______ 0.05859 Estimated objective function value = 0.065887 Estimated function evaluation time = 209.1534
Loss5 = loss(Cmdl,TestX,LabelTest)
Loss5 = 0.0742
The classifier based on RICA with 1000 extracted features has a similar test loss to the RICA classifier based on 200 extracted features.
Optimize Hyperparameters by Using bayesopt
Feature extraction functions have these tuning parameters:
Iteration limit
Function, either
rica
orsparsefilt
Parameter
Lambda
Number of learned features
q
The fitcecoc
regularization parameter also affects the accuracy of the learned classifier. Include that parameter in the list of hyperparameters as well.
To search among the available parameters effectively, try bayesopt
. Use the following objective function, which includes parameters passed from the workspace.
<include>filterica.m</include>
To remove sources of variation, fix an initial transform weight matrix.
W = randn(1e4,1e3);
Create hyperparameters for the objective function.
iterlim = optimizableVariable('iterlim',[5,500],'Type','integer'); lambda = optimizableVariable('lambda',[0,10]); solver = optimizableVariable('solver',{'r','s'},'Type','categorical'); qvar = optimizableVariable('q',[10,1000],'Type','integer'); lambdareg = optimizableVariable('lambdareg',[1e-6,1],'Transform','log'); vars = [iterlim,lambda,solver,qvar,lambdareg];
Run the optimization without the warnings that occur when the internal optimizations do not run to completion. Run for 60 iterations instead of the default 30 to give the optimization a better chance of locating a good value.
warning('off','stats:classreg:learning:fsutils:Solver:LBFGSUnableToConverge'); results = bayesopt(@(x) filterica(x,Xtrain,Xtest,LabelTrain,LabelTest,W),vars, ... 'UseParallel',true,'MaxObjectiveEvaluations',60);
Copying objective function to workers... Done copying objective function to workers. |============================================================================================================================================================| | Iter | Active | Eval | Objective | Objective | BestSoFar | BestSoFar | iterlim | lambda | solver | q | lambdareg | | | workers | result | | runtime | (observed) | (estim.) | | | | | | |============================================================================================================================================================| | 1 | 6 | Best | 0.076837 | 38.76 | 0.076837 | 0.076837 | 53 | 3.1158 | r | 209 | 0.017058 | | 2 | 6 | Accept | 0.083138 | 80.44 | 0.076837 | 0.077436 | 15 | 6.6076 | r | 812 | 0.0040398 | | 3 | 6 | Accept | 0.081956 | 29.023 | 0.076837 | 0.080643 | 35 | 2.1932 | r | 195 | 0.0043137 | | 4 | 6 | Accept | 0.10455 | 133.28 | 0.076837 | 0.07684 | 198 | 8.9251 | s | 321 | 4.7557e-06 | | 5 | 6 | Best | 0.073902 | 65.1 | 0.073902 | 0.0739 | 120 | 4.3267 | r | 206 | 0.062183 | | 6 | 6 | Accept | 0.16551 | 176.62 | 0.073902 | 0.073892 | 469 | 4.2837 | s | 264 | 0.00037171 | | 7 | 6 | Accept | 0.35745 | 208.37 | 0.073902 | 0.073922 | 438 | 0.41877 | s | 360 | 0.10592 | | 8 | 6 | Accept | 0.089436 | 46.201 | 0.073902 | 0.074064 | 118 | 0.90542 | r | 159 | 0.96758 | | 9 | 6 | Accept | 0.078061 | 19.25 | 0.073902 | 0.074128 | 23 | 0.62259 | r | 163 | 0.05916 | | 10 | 6 | Accept | 0.88652 | 135.64 | 0.073902 | 0.080619 | 329 | 0.014203 | r | 183 | 0.0093358 | | 11 | 6 | Accept | 0.091084 | 106.09 | 0.073902 | 0.080455 | 485 | 1.0295 | r | 95 | 0.73027 | | 12 | 6 | Accept | 0.73842 | 52.9 | 0.073902 | 0.073969 | 169 | 0.080792 | r | 129 | 0.0060433 | | 13 | 6 | Accept | 0.11033 | 5.1178 | 0.073902 | 0.078319 | 19 | 5.2202 | r | 30 | 0.68403 | | 14 | 6 | Accept | 0.34332 | 101.16 | 0.073902 | 0.07399 | 66 | 9.6885 | s | 999 | 0.16101 | | 15 | 6 | Accept | 0.088032 | 19.41 | 0.073902 | 0.08087 | 104 | 9.8941 | r | 51 | 6.8341e-05 | | 16 | 6 | Accept | 0.10229 | 5.4613 | 0.073902 | 0.08115 | 9 | 8.7388 | r | 30 | 1.6465e-06 | | 17 | 6 | Accept | 0.10596 | 6.0772 | 0.073902 | 0.074348 | 41 | 2.8436 | r | 21 | 0.058461 | | 18 | 6 | Accept | 0.45747 | 7.8052 | 0.073902 | 0.074332 | 153 | 9.7223 | s | 11 | 0.00038449 | | 19 | 6 | Accept | 0.076343 | 163.99 | 0.073902 | 0.081769 | 76 | 0.73948 | r | 811 | 0.19358 | | 20 | 6 | Accept | 0.73359 | 2.5326 | 0.073902 | 0.08146 | 8 | 4.776 | s | 28 | 0.31063 | |============================================================================================================================================================| | Iter | Active | Eval | Objective | Objective | BestSoFar | BestSoFar | iterlim | lambda | solver | q | lambdareg | | | workers | result | | runtime | (observed) | (estim.) | | | | | | |============================================================================================================================================================| | 21 | 6 | Accept | 0.40021 | 10.584 | 0.073902 | 0.074292 | 236 | 9.5008 | s | 12 | 0.0016817 | | 22 | 6 | Accept | 0.091285 | 13.055 | 0.073902 | 0.080988 | 69 | 3.6545 | r | 43 | 3.358e-06 | | 23 | 6 | Accept | 0.095511 | 10.767 | 0.073902 | 0.08121 | 23 | 0.42764 | r | 68 | 1.2782e-06 | | 24 | 6 | Accept | 0.10951 | 9.8215 | 0.073902 | 0.080268 | 85 | 2.3878 | r | 25 | 0.31652 | | 25 | 6 | Accept | 0.30642 | 24.332 | 0.073902 | 0.079975 | 215 | 2.3306 | s | 63 | 0.036152 | | 26 | 6 | Accept | 0.16879 | 8.1371 | 0.073902 | 0.073935 | 114 | 2.788 | r | 10 | 0.0048925 | | 27 | 6 | Accept | 0.17939 | 4.1933 | 0.073902 | 0.073937 | 18 | 9.6026 | s | 20 | 1.1884e-06 | | 28 | 6 | Accept | 0.10652 | 5.1782 | 0.073902 | 0.073932 | 19 | 4.5217 | r | 22 | 0.00026314 | | 29 | 6 | Accept | 0.34549 | 11.476 | 0.073902 | 0.073931 | 186 | 3.9151 | s | 17 | 3.0184e-06 | | 30 | 6 | Accept | 0.14277 | 4.9863 | 0.073902 | 0.074079 | 44 | 9.1143 | r | 15 | 0.78694 | | 31 | 6 | Accept | 0.12704 | 4.6914 | 0.073902 | 0.074082 | 30 | 6.8979 | r | 14 | 3.1246e-05 | | 32 | 6 | Accept | 0.099523 | 386.84 | 0.073902 | 0.074071 | 350 | 5.7922 | s | 721 | 9.0727e-06 | | 33 | 6 | Accept | 0.18856 | 8.2919 | 0.073902 | 0.074075 | 59 | 0.39559 | s | 33 | 1.1958e-06 | | 34 | 6 | Accept | 0.1205 | 35.241 | 0.073902 | 0.074077 | 497 | 1.8975 | r | 22 | 0.49306 | | 35 | 5 | Accept | 0.087827 | 23.96 | 0.073902 | 0.074114 | 115 | 8.9208 | r | 77 | 0.51949 | | 36 | 5 | Accept | 0.55217 | 16.112 | 0.073902 | 0.074114 | 319 | 8.9016 | s | 18 | 0.23654 | | 37 | 6 | Accept | 0.48395 | 17.094 | 0.073902 | 0.074197 | 376 | 4.6055 | s | 15 | 0.14721 | | 38 | 6 | Accept | 0.1709 | 6.9831 | 0.073902 | 0.073912 | 87 | 9.311 | r | 10 | 6.0611e-06 | | 39 | 6 | Accept | 0.13417 | 9.5463 | 0.073902 | 0.087269 | 38 | 6.1676 | s | 44 | 2.7956e-06 | | 40 | 6 | Accept | 0.21073 | 4.0123 | 0.073902 | 0.073934 | 28 | 8.9149 | s | 21 | 0.00056287 | |============================================================================================================================================================| | Iter | Active | Eval | Objective | Objective | BestSoFar | BestSoFar | iterlim | lambda | solver | q | lambdareg | | | workers | result | | runtime | (observed) | (estim.) | | | | | | |============================================================================================================================================================| | 41 | 6 | Accept | 0.33918 | 13.886 | 0.073902 | 0.07393 | 228 | 5.7208 | s | 20 | 1.3903e-06 | | 42 | 6 | Accept | 0.12899 | 6.3325 | 0.073902 | 0.073808 | 59 | 7.3212 | r | 14 | 0.00011279 | | 43 | 6 | Accept | 0.10752 | 31.295 | 0.073902 | 0.073808 | 468 | 4.5548 | r | 20 | 5.9851e-06 | | 44 | 6 | Accept | 0.43323 | 4.969 | 0.073902 | 0.073803 | 88 | 0.13742 | s | 12 | 0.080009 | | 45 | 6 | Accept | 0.12232 | 11.055 | 0.073902 | 0.073881 | 47 | 9.8954 | s | 50 | 3.2668e-06 | | 46 | 6 | Accept | 0.39303 | 22.937 | 0.073902 | 0.073893 | 500 | 9.6175 | s | 15 | 1.2957e-06 | | 47 | 6 | Accept | 0.10309 | 30.848 | 0.073902 | 0.074034 | 429 | 4.0665 | r | 23 | 0.036138 | | 48 | 6 | Accept | 0.12057 | 10.689 | 0.073902 | 0.074051 | 136 | 6.3754 | r | 18 | 0.19857 | | 49 | 6 | Accept | 0.49756 | 2.9294 | 0.073902 | 0.073955 | 9 | 0.24926 | r | 11 | 0.00029432 | | 50 | 6 | Accept | 0.34992 | 3.1352 | 0.073902 | 0.073951 | 24 | 4.6744 | s | 11 | 0.0009824 | | 51 | 6 | Accept | 0.087601 | 7.1697 | 0.073902 | 0.073958 | 9 | 4.8622 | r | 53 | 0.0078539 | | 52 | 6 | Accept | 0.13926 | 3.7676 | 0.073902 | 0.073926 | 12 | 3.8046 | r | 14 | 0.0021858 | | 53 | 6 | Accept | 0.17455 | 4.5662 | 0.073902 | 0.073941 | 22 | 7.6257 | s | 23 | 1.0888e-05 | | 54 | 6 | Accept | 0.1132 | 7.7284 | 0.073902 | 0.073928 | 37 | 0.49456 | r | 41 | 0.41539 | | 55 | 6 | Accept | 0.10089 | 5.2168 | 0.073902 | 0.073944 | 7 | 5.5505 | r | 39 | 0.06378 | | 56 | 6 | Accept | 0.099791 | 7.2415 | 0.073902 | 0.07395 | 50 | 9.5376 | r | 24 | 0.0010467 | | 57 | 6 | Accept | 0.13782 | 4.0894 | 0.073902 | 0.073945 | 15 | 4.5914 | r | 14 | 6.2156e-06 | | 58 | 6 | Accept | 0.080689 | 125.43 | 0.073902 | 0.073946 | 99 | 9.8436 | r | 522 | 0.008047 | | 59 | 6 | Accept | 0.11027 | 4.7782 | 0.073902 | 0.073945 | 8 | 9.0603 | r | 26 | 4.0436e-05 | | 60 | 6 | Accept | 0.097848 | 5.0788 | 0.073902 | 0.073943 | 20 | 9.7227 | r | 26 | 0.042242 |
__________________________________________________________ Optimization completed. MaxObjectiveEvaluations of 60 reached. Total function evaluations: 60 Total elapsed time: 606.4791 seconds Total objective function evaluation time: 2331.6633 Best observed feasible point: iterlim lambda solver q lambdareg _______ ______ ______ ___ _________ 120 4.3267 r 206 0.062183 Observed objective function value = 0.073902 Estimated objective function value = 0.073943 Function evaluation time = 65.1002 Best estimated feasible point (according to models): iterlim lambda solver q lambdareg _______ ______ ______ ___ _________ 120 4.3267 r 206 0.062183 Estimated objective function value = 0.073943 Estimated function evaluation time = 65.1934
warning('on','stats:classreg:learning:fsutils:Solver:LBFGSUnableToConverge');
The resulting classifier does not have better (lower) loss than the classifier using sparsefilt
for 1000 features, trained for 10 iterations.
View the filter coefficients for the best hyperparameters that bayesopt
found. The resulting images show the shapes of the extracted features. These shapes are recognizable as portions of handwritten digits.
Xtbl = results.XAtMinObjective; Q = Xtbl.q; initW = W(1:size(Xtrain,2),1:Q); if char(Xtbl.solver) == 'r' Mdl = rica(Xtrain,Q,'Lambda',Xtbl.lambda,'IterationLimit',Xtbl.iterlim, ... 'InitialTransformWeights',initW,'Standardize',true); else Mdl = sparsefilt(Xtrain,Q,'Lambda',Xtbl.lambda,'IterationLimit',Xtbl.iterlim, ... 'InitialTransformWeights',initW); end
Warning: Solver LBFGS was not able to converge to a solution.
Wts = Mdl.TransformWeights; Wts = reshape(Wts,[28,28,Q]); [dx,dy,~,~] = size(Wts); for f = 1:Q Wvec = Wts(:,:,f); Wvec = Wvec(:); Wvec =(Wvec - min(Wvec))/(max(Wvec) - min(Wvec)); Wts(:,:,f) = reshape(Wvec,dx,dy); end m = ceil(sqrt(Q)); n = m; img = zeros(m*dx,n*dy); f = 1; for i = 1:m for j = 1:n if (f <= Q) img((i-1)*dx+1:i*dx,(j-1)*dy+1:j*dy,:) = Wts(:,:,f); f = f+1; end end end imshow(img);
Code for Reading MNIST Data
The code of the function that reads the data into the workspace is:
function [X,L] = processMNISTdata(imageFileName,labelFileName) [fileID,errmsg] = fopen(imageFileName,'r','b'); if fileID < 0 error(errmsg); end %% % First read the magic number. This number is 2051 for image data, and % 2049 for label data magicNum = fread(fileID,1,'int32',0,'b'); if magicNum == 2051 fprintf('\nRead MNIST image data...\n') end %% % Then read the number of images, number of rows, and number of columns numImages = fread(fileID,1,'int32',0,'b'); fprintf('Number of images in the dataset: %6d ...\n',numImages); numRows = fread(fileID,1,'int32',0,'b'); numCols = fread(fileID,1,'int32',0,'b'); fprintf('Each image is of %2d by %2d pixels...\n',numRows,numCols); %% % Read the image data X = fread(fileID,inf,'unsigned char'); %% % Reshape the data to array X X = reshape(X,numCols,numRows,numImages); X = permute(X,[2 1 3]); %% % Then flatten each image data into a 1 by (numRows*numCols) vector, and % store all the image data into a numImages by (numRows*numCols) array. X = reshape(X,numRows*numCols,numImages)'; fprintf(['The image data is read to a matrix of dimensions: %6d by %4d...\n',... 'End of reading image data.\n'],size(X,1),size(X,2)); %% % Close the file fclose(fileID); %% % Similarly, read the label data. [fileID,errmsg] = fopen(labelFileName,'r','b'); if fileID < 0 error(errmsg); end magicNum = fread(fileID,1,'int32',0,'b'); if magicNum == 2049 fprintf('\nRead MNIST label data...\n') end numItems = fread(fileID,1,'int32',0,'b'); fprintf('Number of labels in the dataset: %6d ...\n',numItems); L = fread(fileID,inf,'unsigned char'); fprintf(['The label data is read to a matrix of dimensions: %6d by %2d...\n',... 'End of reading label data.\n'],size(L,1),size(L,2)); fclose(fileID);
References
[1] Yann LeCun (Courant Institute, NYU) and Corinna Cortes (Google Labs, New York) hold the copyright of MNIST dataset, which is a derivative work from original NIST datasets. MNIST dataset is made available under the terms of the Creative Commons Attribution-Share Alike 3.0 license, https://creativecommons.org/licenses/by-sa/3.0/
See Also
rica
| sparsefilt
| ReconstructionICA
| SparseFiltering