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

http://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. To execute the code, add the directory of the function to the search path.

addpath(fullfile(matlabroot,'examples','stats'));
[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);
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 |       4.588 |      0.5777 |      0.5777 |      0.20606 |
|    2 |       6 | Accept |      0.8865 |      5.0017 |      0.5777 |     0.72706 |       8.8234 |
|    3 |       6 | Best   |      0.2041 |       5.822 |      0.2041 |     0.27206 |     0.026804 |
|    4 |       6 | Accept |      0.6711 |       4.735 |      0.2041 |     0.20896 |      0.25208 |
|    5 |       5 | Accept |      0.1077 |      11.522 |      0.0962 |    0.097197 |   1.7309e-09 |
|    6 |       5 | Best   |      0.0962 |      11.451 |      0.0962 |    0.097197 |    0.0002442 |
|    7 |       6 | Accept |      0.1999 |      5.3235 |      0.0962 |    0.096851 |     0.024845 |
|    8 |       6 | Accept |      0.1065 |      11.499 |      0.0962 |    0.096449 |    2.037e-08 |
|    9 |       6 | Accept |       0.149 |      6.2729 |      0.0962 |    0.097855 |    0.0073968 |
|   10 |       6 | Accept |      0.0977 |      18.259 |      0.0962 |    0.097622 |   8.0495e-06 |
|   11 |       6 | Accept |      0.1164 |       7.436 |      0.0962 |    0.097047 |    0.0016862 |
|   12 |       6 | Accept |      0.1034 |      12.911 |      0.0962 |    0.097821 |    3.286e-07 |
|   13 |       6 | Best   |      0.0924 |      13.723 |      0.0924 |     0.09249 |   6.8087e-05 |
|   14 |       6 | Accept |      0.1082 |      10.592 |      0.0924 |    0.092422 |   5.7675e-09 |
|   15 |       6 | Accept |      0.0996 |      10.522 |      0.0924 |    0.096464 |   0.00039079 |
|   16 |       6 | Accept |      0.1081 |      10.278 |      0.0924 |     0.09636 |   1.0009e-09 |
|   17 |       6 | Accept |      0.1012 |      17.031 |      0.0924 |    0.096376 |   1.9714e-06 |
|   18 |       6 | Accept |       0.106 |      13.308 |      0.0924 |    0.096341 |   8.1431e-08 |
|   19 |       6 | Accept |      0.0927 |      13.253 |      0.0924 |    0.092789 |    0.0001085 |
|   20 |       6 | Accept |      0.0931 |      14.945 |      0.0924 |    0.092413 |   3.8581e-05 |
|================================================================================================|
| Iter | Active  | Eval   | Objective   | Objective   | BestSoFar   | BestSoFar   |       Lambda |
|      | workers | result |             | runtime     | (observed)  | (estim.)    |              |
|================================================================================================|
|   21 |       6 | Accept |      0.0929 |      13.616 |      0.0924 |    0.092821 |   0.00011324 |
|   22 |       6 | Accept |      0.0946 |      16.769 |      0.0924 |      0.0925 |   2.6147e-05 |
|   23 |       6 | Accept |      0.8865 |      4.5411 |      0.0924 |    0.092479 |       2.3768 |
|   24 |       6 | Best   |      0.0918 |      14.081 |      0.0918 |    0.092369 |   8.6878e-05 |
|   25 |       6 | Accept |      0.0942 |      14.432 |      0.0918 |    0.092609 |   5.4788e-05 |
|   26 |       6 | Accept |       0.094 |      12.821 |      0.0918 |    0.092689 |   0.00014063 |
|   27 |       6 | Accept |      0.1285 |      7.4672 |      0.0918 |    0.092689 |    0.0034269 |
|   28 |       6 | Accept |      0.0959 |      16.916 |      0.0918 |    0.092681 |   2.1997e-05 |
|   29 |       6 | Accept |      0.1014 |      15.457 |      0.0918 |    0.092678 |   7.7637e-07 |
|   30 |       6 | Accept |      0.1082 |      8.9728 |      0.0918 |    0.092666 |   0.00083613 |

__________________________________________________________
Optimization completed.
MaxObjectiveEvaluations of 30 reached.
Total function evaluations: 30
Total elapsed time: 65.2452 seconds.
Total objective function evaluation time: 333.5456

Best observed feasible point:
      Lambda  
    __________

    8.6878e-05

Observed objective function value = 0.0918
Estimated objective function value = 0.092666
Function evaluation time = 14.0811

Best estimated feasible point (according to models):
      Lambda  
    __________

    8.6878e-05

Estimated objective function value = 0.092666
Estimated function evaluation time = 13.7827

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.1010

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);
Losso = loss(Omdl,Xtest,LabelTest)
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.0797 |      45.347 |      0.0797 |      0.0797 |   1.3269e-06 |
|    2 |       6 | Accept |      0.0798 |      45.934 |      0.0797 |     0.07975 |   3.1279e-07 |
|    3 |       6 | Accept |      0.0798 |      46.552 |      0.0797 |    0.079767 |   3.8643e-09 |
|    4 |       6 | Accept |      0.0797 |       66.18 |      0.0797 |     0.07975 |   3.0332e-05 |
|    5 |       6 | Best   |      0.0795 |      81.456 |      0.0795 |      0.0797 |   4.9634e-05 |
|    6 |       6 | Accept |      0.0808 |      142.01 |      0.0795 |    0.079883 |   0.00021639 |
|    7 |       6 | Accept |      0.0803 |      144.36 |      0.0795 |    0.079943 |     0.011605 |
|    8 |       6 | Accept |      0.0798 |      45.745 |      0.0795 |    0.079925 |   2.5594e-07 |
|    9 |       6 | Accept |      0.0798 |      45.088 |      0.0795 |    0.079911 |   1.0001e-09 |
|   10 |       6 | Accept |      0.0797 |      46.712 |      0.0795 |     0.07989 |   2.6909e-06 |
|   11 |       6 | Accept |      0.0798 |      96.618 |      0.0795 |    0.079882 |   6.9084e-05 |
|   12 |       6 | Best   |      0.0738 |      240.11 |      0.0738 |    0.073815 |        6.805 |
|   13 |       6 | Accept |      0.0798 |      50.119 |      0.0738 |    0.073808 |   1.0836e-05 |
|   14 |       6 | Accept |      0.0805 |      144.96 |      0.0738 |    0.073811 |     0.025772 |
|   15 |       6 | Best   |      0.0722 |      267.93 |      0.0722 |    0.072752 |       9.9712 |
|   16 |       6 | Accept |      0.0799 |      171.82 |      0.0722 |    0.072759 |   0.00072554 |
|   17 |       6 | Accept |      0.0801 |       163.6 |      0.0722 |    0.072744 |    0.0021717 |
|   18 |       6 | Accept |      0.0798 |      46.893 |      0.0722 |     0.07273 |    2.455e-08 |
|   19 |       6 | Accept |      0.0757 |      217.35 |      0.0722 |    0.072538 |       3.5798 |
|   20 |       6 | Accept |      0.0792 |      146.23 |      0.0722 |    0.072558 |      0.54129 |
|================================================================================================|
| Iter | Active  | Eval   | Objective   | Objective   | BestSoFar   | BestSoFar   |       Lambda |
|      | workers | result |             | runtime     | (observed)  | (estim.)    |              |
|================================================================================================|
|   21 |       6 | Best   |      0.0721 |      281.61 |      0.0721 |    0.072355 |        9.998 |
|   22 |       6 | Accept |      0.0793 |      125.68 |      0.0721 |     0.07226 |      0.13594 |
|   23 |       6 | Accept |      0.0798 |      44.846 |      0.0721 |     0.07226 |   6.6359e-08 |
|   24 |       6 | Accept |      0.0725 |      259.43 |      0.0721 |    0.072339 |       9.9821 |
|   25 |       6 | Accept |      0.0798 |      43.868 |      0.0721 |    0.072334 |   9.1497e-09 |
|   26 |       6 | Accept |      0.0798 |      45.423 |      0.0721 |    0.072328 |    1.735e-09 |
|   27 |       6 | Accept |      0.0725 |      278.93 |      0.0721 |    0.072368 |       9.9832 |
|   28 |       6 | Accept |      0.0798 |      45.271 |      0.0721 |    0.072365 |   5.4313e-06 |
|   29 |       6 | Accept |      0.0797 |       47.24 |      0.0721 |    0.072362 |   6.8343e-07 |
|   30 |       6 | Accept |      0.0798 |      49.129 |      0.0721 |     0.07236 |   1.1983e-07 |

__________________________________________________________
Optimization completed.
MaxObjectiveEvaluations of 30 reached.
Total function evaluations: 30
Total elapsed time: 648.3735 seconds.
Total objective function evaluation time: 3476.4476

Best observed feasible point:
    Lambda
    ______

    9.998 

Observed objective function value = 0.0721
Estimated objective function value = 0.07236
Function evaluation time = 281.6136

Best estimated feasible point (according to models):
    Lambda
    ______

    9.998 

Estimated objective function value = 0.07236
Estimated function evaluation time = 270.0021


Losso =

    0.0858

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);
NewX = transform(Mdl2,Xtrain);
TestX = transform(Mdl2,Xtest);
Cmdl = fitcecoc(NewX,LabelTrain,'Learners',t, ...
    'OptimizeHyperparameters',{'Lambda'}, ...
    'HyperparameterOptimizationOptions',options);
Loss2 = loss(Cmdl,TestX,LabelTest)
Warning: Solver LBFGS was not able to converge to a solution. 
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.7893 |      7.4469 |      0.7893 |      0.7893 |      0.72663 |
|    2 |       6 | Best   |      0.0716 |      10.204 |      0.0716 |     0.10795 |   1.6062e-09 |
|    3 |       6 | Accept |      0.0966 |      14.286 |      0.0716 |     0.12852 |    0.0025081 |
|    4 |       6 | Accept |       0.225 |      8.7065 |      0.0716 |     0.29562 |     0.060791 |
|    5 |       6 | Best   |      0.0702 |      10.078 |      0.0702 |    0.070213 |   4.5147e-09 |
|    6 |       6 | Best   |      0.0681 |      23.182 |      0.0681 |    0.068117 |   1.2698e-06 |
|    7 |       6 | Best   |      0.0621 |      24.307 |      0.0621 |    0.062113 |   0.00012975 |
|    8 |       6 | Accept |      0.0725 |      9.6408 |      0.0621 |    0.062114 |   1.0663e-09 |
|    9 |       6 | Accept |      0.0717 |      9.9688 |      0.0621 |    0.062119 |   2.3779e-09 |
|   10 |       6 | Best   |      0.0614 |      28.534 |      0.0614 |    0.061347 |   5.1855e-05 |
|   11 |       6 | Accept |      0.0709 |        11.5 |      0.0614 |    0.061309 |   4.2488e-08 |
|   12 |       6 | Accept |       0.144 |      9.8798 |      0.0614 |    0.061421 |     0.012136 |
|   13 |       6 | Accept |      0.0707 |      9.8557 |      0.0614 |    0.061429 |   1.5994e-08 |
|   14 |       6 | Accept |      0.0699 |      17.066 |      0.0614 |    0.061435 |   2.5888e-07 |
|   15 |       6 | Accept |      0.0721 |      19.149 |      0.0614 |     0.06132 |   0.00044297 |
|   16 |       6 | Accept |      0.0642 |      29.277 |      0.0614 |    0.061271 |   7.2575e-06 |
|   17 |       6 | Accept |       0.062 |      28.302 |      0.0614 |    0.061572 |    8.065e-05 |
|   18 |       6 | Accept |      0.0653 |      22.933 |      0.0614 |    0.061575 |   0.00022074 |
|   19 |       6 | Accept |      0.0623 |      32.726 |      0.0614 |    0.061578 |   1.9248e-05 |
|   20 |       6 | Accept |      0.8865 |      7.9368 |      0.0614 |    0.061654 |       9.9874 |
|================================================================================================|
| Iter | Active  | Eval   | Objective   | Objective   | BestSoFar   | BestSoFar   |       Lambda |
|      | workers | result |             | runtime     | (observed)  | (estim.)    |              |
|================================================================================================|
|   21 |       6 | Best   |      0.0613 |      30.636 |      0.0613 |    0.061435 |   3.3842e-05 |
|   22 |       6 | Accept |      0.0616 |      28.619 |      0.0613 |    0.061612 |   7.3968e-05 |
|   23 |       6 | Accept |      0.0619 |       28.16 |      0.0613 |    0.061653 |   6.7016e-05 |
|   24 |       6 | Accept |      0.0692 |      14.968 |      0.0613 |    0.061651 |   1.0727e-07 |
|   25 |       6 | Accept |      0.0616 |      30.129 |      0.0613 |    0.061671 |   4.7562e-05 |
|   26 |       6 | Accept |      0.0617 |      31.403 |      0.0613 |    0.061593 |   4.4587e-05 |
|   27 |       6 | Accept |      0.0675 |      27.259 |      0.0613 |    0.061595 |   3.1202e-06 |
|   28 |       6 | Accept |      0.4735 |      6.8424 |      0.0613 |    0.061597 |      0.18242 |
|   29 |       6 | Accept |      0.0616 |      33.119 |      0.0613 |    0.061565 |    3.016e-05 |
|   30 |       6 | Accept |      0.0618 |       24.59 |      0.0613 |    0.061568 |   0.00010398 |

__________________________________________________________
Optimization completed.
MaxObjectiveEvaluations of 30 reached.
Total function evaluations: 30
Total elapsed time: 115.3067 seconds.
Total objective function evaluation time: 590.7039

Best observed feasible point:
      Lambda  
    __________

    3.3842e-05

Observed objective function value = 0.0613
Estimated objective function value = 0.061568
Function evaluation time = 30.6361

Best estimated feasible point (according to models):
      Lambda  
    __________

    4.7562e-05

Estimated objective function value = 0.061568
Estimated function evaluation time = 29.9808


Loss2 =

    0.0711

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);
NewX = transform(Mdl3,Xtrain);
TestX = transform(Mdl3,Xtest);
Cmdl = fitcecoc(NewX,LabelTrain,'Learners',t, ...
    'OptimizeHyperparameters',{'Lambda'}, ...
    'HyperparameterOptimizationOptions',options);
Loss3 = loss(Cmdl,TestX,LabelTest)
Warning: Solver LBFGS was not able to converge to a solution. 
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.084 |      12.906 |       0.084 |       0.084 |   6.5597e-08 |
|    2 |       6 | Accept |       0.086 |        13.5 |       0.084 |       0.085 |   1.9245e-09 |
|    3 |       6 | Best   |      0.0829 |      16.128 |      0.0829 |      0.0829 |   3.7114e-07 |
|    4 |       6 | Best   |       0.074 |      16.821 |       0.074 |       0.074 |      0.62673 |
|    5 |       6 | Accept |      0.0804 |      20.545 |       0.074 |    0.074009 |   4.5964e-05 |
|    6 |       6 | Best   |      0.0646 |       23.01 |      0.0646 |    0.064603 |     0.021702 |
|    7 |       6 | Accept |      0.0849 |      12.416 |      0.0646 |    0.064603 |    2.328e-08 |
|    8 |       6 | Accept |      0.1241 |       10.95 |      0.0646 |      0.0646 |       9.9751 |
|    9 |       6 | Accept |      0.0835 |      17.719 |      0.0646 |    0.064598 |   6.4684e-06 |
|   10 |       6 | Accept |       0.124 |       11.31 |      0.0646 |    0.064597 |       9.9557 |
|   11 |       6 | Accept |      0.0779 |      28.634 |      0.0646 |    0.064598 |   0.00065059 |
|   12 |       6 | Accept |      0.0676 |      23.752 |      0.0646 |    0.064595 |     0.010667 |
|   13 |       6 | Accept |      0.0654 |      23.921 |      0.0646 |    0.064405 |      0.01542 |
|   14 |       6 | Best   |      0.0644 |      23.231 |      0.0644 |    0.064332 |      0.11521 |
|   15 |       6 | Best   |      0.0638 |      25.284 |      0.0638 |    0.063692 |     0.050984 |
|   16 |       6 | Accept |      0.0729 |      29.855 |      0.0638 |    0.063639 |    0.0027668 |
|   17 |       6 | Accept |      0.0677 |      21.495 |      0.0638 |    0.063673 |      0.20804 |
|   18 |       6 | Best   |      0.0637 |      23.363 |      0.0637 |     0.06356 |     0.080226 |
|   19 |       6 | Accept |       0.065 |      23.415 |      0.0637 |    0.063911 |      0.03778 |
|   20 |       6 | Accept |      0.0644 |      23.866 |      0.0637 |    0.064006 |     0.046571 |
|================================================================================================|
| Iter | Active  | Eval   | Objective   | Objective   | BestSoFar   | BestSoFar   |       Lambda |
|      | workers | result |             | runtime     | (observed)  | (estim.)    |              |
|================================================================================================|
|   21 |       6 | Accept |       0.084 |      13.208 |      0.0637 |    0.064006 |   1.0001e-09 |
|   22 |       6 | Accept |      0.0647 |      23.791 |      0.0637 |    0.064126 |     0.049823 |
|   23 |       6 | Accept |      0.0915 |      14.419 |      0.0637 |    0.064123 |       2.3605 |
|   24 |       6 | Accept |      0.0838 |      19.001 |      0.0637 |    0.064122 |   1.5751e-06 |
|   25 |       6 | Best   |      0.0635 |      24.083 |      0.0635 |    0.064028 |     0.069931 |
|   26 |       6 | Accept |      0.0636 |      23.293 |      0.0635 |     0.06397 |     0.074069 |
|   27 |       6 | Accept |      0.0791 |      25.555 |      0.0635 |    0.063968 |   0.00016255 |
|   28 |       6 | Accept |      0.0859 |      13.665 |      0.0635 |    0.063968 |   6.8062e-09 |
|   29 |       6 | Accept |      0.0637 |      25.079 |      0.0635 |    0.063927 |     0.086318 |
|   30 |       6 | Accept |      0.0637 |      25.773 |      0.0635 |    0.063903 |     0.066934 |

__________________________________________________________
Optimization completed.
MaxObjectiveEvaluations of 30 reached.
Total function evaluations: 30
Total elapsed time: 110.9718 seconds.
Total objective function evaluation time: 609.9879

Best observed feasible point:
     Lambda 
    ________

    0.069931

Observed objective function value = 0.0635
Estimated objective function value = 0.063903
Function evaluation time = 24.0826

Best estimated feasible point (according to models):
     Lambda 
    ________

    0.066934

Estimated objective function value = 0.063903
Estimated function evaluation time = 24.2314


Loss3 =

    0.0743

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);
NewX = transform(Mdl4,Xtrain);
TestX = transform(Mdl4,Xtest);
Cmdl = fitcecoc(NewX,LabelTrain,'Learners',t, ...
    'OptimizeHyperparameters',{'Lambda'}, ...
    'HyperparameterOptimizationOptions',options);
Loss4 = loss(Cmdl,TestX,LabelTest)
Warning: Solver LBFGS was not able to converge to a solution. 
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.3196 |      43.804 |      0.3196 |      0.3196 |        0.124 |
|    2 |       6 | Accept |      0.8865 |      48.728 |      0.3196 |     0.60296 |       3.1179 |
|    3 |       6 | Best   |      0.0422 |      52.415 |      0.0422 |     0.41605 |   3.9778e-09 |
|    4 |       6 | Accept |      0.1768 |      60.799 |      0.0422 |    0.042229 |     0.033943 |
|    5 |       6 | Best   |      0.0414 |      58.091 |      0.0414 |    0.041413 |   4.7965e-08 |
|    6 |       6 | Accept |      0.0428 |      52.652 |      0.0414 |    0.041417 |   1.0016e-09 |
|    7 |       6 | Accept |      0.1204 |      79.097 |      0.0414 |     0.04143 |     0.010245 |
|    8 |       6 | Accept |      0.0428 |      55.811 |      0.0414 |    0.041497 |   1.4541e-08 |
|    9 |       6 | Accept |      0.0731 |      122.53 |      0.0414 |    0.041405 |    0.0018454 |
|   10 |       6 | Accept |      0.0481 |      174.08 |      0.0414 |    0.041443 |   0.00035999 |
|   11 |       6 | Best   |      0.0388 |      182.69 |      0.0388 |    0.038757 |   1.1526e-06 |
|   12 |       6 | Accept |      0.0418 |      89.978 |      0.0388 |    0.038834 |   1.5445e-07 |
|   13 |       6 | Accept |       0.043 |       55.82 |      0.0388 |    0.038832 |   2.0088e-09 |
|   14 |       6 | Accept |      0.0405 |      163.43 |      0.0388 |    0.038791 |   4.0912e-07 |
|   15 |       6 | Best   |      0.0375 |      301.81 |      0.0375 |    0.037465 |    1.738e-05 |
|   16 |       6 | Best   |      0.0374 |      252.82 |      0.0374 |    0.037435 |   4.1237e-06 |
|   17 |       6 | Accept |      0.0377 |      275.48 |      0.0374 |    0.037258 |   8.0552e-05 |
|   18 |       6 | Accept |      0.0376 |      226.05 |      0.0374 |    0.037794 |   2.1572e-06 |
|   19 |       6 | Best   |      0.0373 |      309.71 |      0.0373 |    0.037161 |   3.9051e-05 |
|   20 |       6 | Best   |      0.0372 |      315.73 |      0.0372 |    0.037162 |   8.4702e-06 |
|================================================================================================|
| Iter | Active  | Eval   | Objective   | Objective   | BestSoFar   | BestSoFar   |       Lambda |
|      | workers | result |             | runtime     | (observed)  | (estim.)    |              |
|================================================================================================|
|   21 |       6 | Accept |      0.0376 |      275.03 |      0.0372 |    0.037409 |   4.6574e-06 |
|   22 |       6 | Best   |      0.0367 |      320.27 |      0.0367 |    0.037176 |    9.926e-06 |
|   23 |       6 | Accept |      0.0376 |         313 |      0.0367 |    0.037175 |   3.7424e-05 |
|   24 |       6 | Best   |      0.0365 |      244.51 |      0.0365 |    0.037109 |   3.3385e-06 |
|   25 |       6 | Accept |      0.0378 |      333.03 |      0.0365 |    0.037098 |   3.2038e-05 |
|   26 |       6 | Accept |      0.8865 |      53.663 |      0.0365 |    0.037143 |       9.9808 |
|   27 |       6 | Accept |      0.0421 |      53.502 |      0.0365 |    0.037143 |   7.7838e-09 |
|   28 |       6 | Accept |       0.042 |      54.756 |      0.0365 |    0.037144 |   2.7559e-08 |
|   29 |       6 | Accept |       0.037 |      302.01 |      0.0365 |    0.037142 |    5.065e-05 |
|   30 |       6 | Accept |      0.0379 |      225.31 |      0.0365 |    0.037291 |   2.5359e-06 |

__________________________________________________________
Optimization completed.
MaxObjectiveEvaluations of 30 reached.
Total function evaluations: 30
Total elapsed time: 1045.5819 seconds.
Total objective function evaluation time: 5096.6009

Best observed feasible point:
      Lambda  
    __________

    3.3385e-06

Observed objective function value = 0.0365
Estimated objective function value = 0.037291
Function evaluation time = 244.5086

Best estimated feasible point (according to models):
      Lambda  
    __________

    3.3385e-06

Estimated objective function value = 0.037291
Estimated function evaluation time = 246.3645


Loss4 =

    0.0445

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);
NewX = transform(Mdl5,Xtrain);
TestX = transform(Mdl5,Xtest);
Cmdl = fitcecoc(NewX,LabelTrain,'Learners',t, ...
    'OptimizeHyperparameters',{'Lambda'}, ...
    'HyperparameterOptimizationOptions',options);
Loss5 = loss(Cmdl,TestX,LabelTest)
Warning: Solver LBFGS was not able to converge to a solution. 
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.078 |      46.859 |       0.078 |       0.078 |   1.2308e-09 |
|    2 |       6 | Accept |      0.0781 |       46.42 |       0.078 |     0.07805 |   2.2784e-08 |
|    3 |       6 | Accept |      0.1195 |      101.59 |       0.078 |    0.077999 |       8.0386 |
|    4 |       6 | Accept |      0.1168 |      104.65 |       0.078 |    0.077984 |       7.4463 |
|    5 |       6 | Accept |       0.078 |      44.306 |       0.078 |    0.077953 |    1.006e-09 |
|    6 |       6 | Accept |       0.078 |      142.32 |       0.078 |    0.078099 |   3.0121e-06 |
|    7 |       6 | Accept |      0.0797 |      146.55 |       0.078 |    0.078076 |   3.8939e-05 |
|    8 |       6 | Accept |      0.0782 |      45.731 |       0.078 |    0.078065 |   4.9138e-09 |
|    9 |       6 | Accept |      0.0782 |      46.357 |       0.078 |    0.078109 |   6.0502e-09 |
|   10 |       6 | Accept |       0.078 |      43.019 |       0.078 |    0.078068 |    1.009e-09 |
|   11 |       6 | Best   |      0.0779 |      43.605 |      0.0779 |    0.078109 |   7.7898e-08 |
|   12 |       6 | Accept |       0.078 |      46.871 |      0.0779 |    0.078053 |   1.0011e-09 |
|   13 |       6 | Best   |       0.066 |      215.65 |       0.066 |    0.066037 |     0.029868 |
|   14 |       6 | Accept |      0.0781 |      89.744 |       0.066 |    0.066034 |    2.842e-07 |
|   15 |       6 | Accept |      0.0782 |      44.408 |       0.066 |    0.066026 |   1.0315e-07 |
|   16 |       6 | Accept |      0.0775 |      97.157 |       0.066 |    0.066025 |   2.9453e-07 |
|   17 |       6 | Accept |       0.078 |      47.626 |       0.066 |     0.06602 |   2.0811e-09 |
|   18 |       6 | Accept |      0.0778 |      149.13 |       0.066 |    0.066017 |   5.3527e-07 |
|   19 |       6 | Accept |      0.0669 |      247.48 |       0.066 |    0.066075 |     0.014913 |
|   20 |       5 | Accept |      0.0673 |       269.2 |       0.066 |    0.066329 |    0.0061747 |
|================================================================================================|
| Iter | Active  | Eval   | Objective   | Objective   | BestSoFar   | BestSoFar   |       Lambda |
|      | workers | result |             | runtime     | (observed)  | (estim.)    |              |
|================================================================================================|
|   21 |       5 | Accept |      0.0663 |      251.22 |       0.066 |    0.066329 |      0.01358 |
|   22 |       6 | Accept |      0.0661 |      258.36 |       0.066 |    0.066181 |     0.019141 |
|   23 |       6 | Best   |      0.0659 |      267.66 |      0.0659 |    0.066104 |     0.014568 |
|   24 |       6 | Best   |      0.0657 |      235.62 |      0.0657 |     0.06603 |      0.02148 |
|   25 |       6 | Accept |      0.0711 |      180.65 |      0.0657 |    0.065756 |      0.51912 |
|   26 |       6 | Best   |      0.0653 |      238.44 |      0.0653 |    0.065386 |     0.041566 |
|   27 |       6 | Accept |      0.0742 |       291.2 |      0.0653 |     0.06538 |   0.00041546 |
|   28 |       6 | Best   |       0.064 |      264.04 |       0.064 |    0.064214 |      0.11302 |
|   29 |       6 | Accept |      0.0668 |      237.99 |       0.064 |    0.064218 |     0.025249 |
|   30 |       6 | Accept |      0.0703 |      348.15 |       0.064 |    0.064231 |    0.0016746 |

__________________________________________________________
Optimization completed.
MaxObjectiveEvaluations of 30 reached.
Total function evaluations: 30
Total elapsed time: 859.161 seconds.
Total objective function evaluation time: 4591.998

Best observed feasible point:
    Lambda 
    _______

    0.11302

Observed objective function value = 0.064
Estimated objective function value = 0.064231
Function evaluation time = 264.0375

Best estimated feasible point (according to models):
    Lambda 
    _______

    0.11302

Estimated objective function value = 0.064231
Estimated function evaluation time = 261.2155


Loss5 =

    0.0744

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 or sparsefilt

  • 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.

function objective = filterica(x,Xtrain,Xtest,LabelTrain,LabelTest,winit)

initW = winit(1:size(Xtrain,2),1:x.q);
if char(x.solver) == 'r'
    Mdl = rica(Xtrain,x.q,'Lambda',x.lambda,'IterationLimit',x.iterlim, ...
        'InitialTransformWeights',initW,'Standardize',true);
else
    Mdl = sparsefilt(Xtrain,x.q,'Lambda',x.lambda,'IterationLimit',x.iterlim, ...
        'InitialTransformWeights',initW);
end

NewX = transform(Mdl,Xtrain);
TestX = transform(Mdl,Xtest);
t = templateLinear('Lambda',x.lambdareg,'Solver','lbfgs');
Cmdl = fitcecoc(NewX,LabelTrain,'Learners',t);
objective = loss(Cmdl,TestX,LabelTest);

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);
warning('on','stats:classreg:learning:fsutils:Solver:LBFGSUnableToConverge');
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.064262 |      126.17 |    0.064262 |    0.064262 |           66 |       8.8009 |            s |          600 |   1.2008e-05 |
|    2 |       6 | Accept |    0.091053 |      170.49 |    0.064262 |    0.067421 |          104 |       2.7081 |            r |          620 |   1.0597e-06 |
|    3 |       6 | Accept |    0.069199 |      193.21 |    0.064262 |    0.064268 |           81 |       7.6504 |            s |          827 |   2.0631e-06 |
|    4 |       6 | Accept |     0.73482 |      215.93 |    0.064262 |    0.064317 |          142 |       4.7506 |            s |          782 |      0.55768 |
|    5 |       6 | Best   |    0.051542 |      67.014 |    0.051542 |    0.052067 |           29 |       9.0814 |            s |          501 |   2.3068e-06 |
|    6 |       6 | Accept |     0.15652 |      131.85 |    0.051542 |    0.051469 |           97 |       8.5824 |            s |          559 |    0.0028902 |
|    7 |       6 | Accept |    0.085989 |      326.56 |    0.051542 |    0.051436 |          230 |       2.4621 |            r |          593 |   0.00042692 |
|    8 |       6 | Accept |      0.1415 |      433.71 |    0.051542 |    0.051752 |          498 |       9.9255 |            s |          425 |   5.0258e-06 |
|    9 |       6 | Accept |     0.10899 |      642.29 |    0.051542 |    0.051655 |          390 |       1.7236 |            s |          831 |   1.4466e-05 |
|   10 |       6 | Accept |    0.066158 |      23.658 |    0.051542 |    0.057452 |            5 |    0.0062838 |            s |          400 |   9.1177e-06 |
|   11 |       6 | Accept |      0.0864 |      460.31 |    0.051542 |    0.057426 |          270 |       9.5546 |            r |          719 |       0.9978 |
|   12 |       6 | Accept |    0.086213 |      44.924 |    0.051542 |    0.055856 |            6 |      0.86023 |            s |          830 |   0.00017092 |
|   13 |       6 | Accept |    0.098428 |      10.845 |    0.051542 |    0.055982 |            6 |       3.0153 |            r |          178 |   2.1535e-05 |
|   14 |       6 | Accept |    0.079025 |      34.148 |    0.051542 |    0.056356 |           13 |      0.47288 |            r |          450 |     0.017162 |
|   15 |       6 | Accept |    0.061372 |      18.605 |    0.051542 |    0.055396 |           15 |        2.577 |            s |          227 |   1.0123e-06 |
|   16 |       6 | Accept |      0.1007 |      898.77 |    0.051542 |    0.059737 |          470 |       4.5405 |            s |          968 |   4.4297e-06 |
|   17 |       6 | Accept |    0.088058 |      7.7917 |    0.051542 |    0.060002 |           22 |       5.2427 |            r |           48 |      0.18609 |
|   18 |       6 | Accept |     0.11215 |      5.3505 |    0.051542 |     0.06724 |           13 |       8.4696 |            s |           33 |   1.0601e-06 |
|   19 |       6 | Accept |    0.092844 |      5.5426 |    0.051542 |    0.067667 |           16 |       5.4013 |            r |           33 |    0.0024407 |
|   20 |       6 | Accept |     0.11231 |      6.4147 |    0.051542 |    0.058539 |            9 |       9.6144 |            s |           49 |      2.1e-05 |
|============================================================================================================================================================|
| Iter | Active  | Eval   | Objective   | Objective   | BestSoFar   | BestSoFar   |      iterlim |       lambda |       solver |            q |    lambdareg |
|      | workers | result |             | runtime     | (observed)  | (estim.)    |              |              |              |              |              |
|============================================================================================================================================================|
|   21 |       6 | Accept |    0.052947 |      100.39 |    0.051542 |    0.048284 |           11 |       3.2379 |            s |          936 |   2.8141e-06 |
|   22 |       6 | Accept |     0.17027 |      317.34 |    0.051542 |    0.047274 |          500 |       9.8544 |            s |          335 |   0.00044873 |
|   23 |       6 | Best   |    0.049615 |      100.15 |    0.049615 |    0.046873 |           24 |       1.1247 |            s |          928 |   1.8948e-06 |
|   24 |       6 | Accept |    0.049779 |      102.68 |    0.049615 |    0.046294 |           29 |      0.21655 |            s |          926 |   1.0882e-06 |
|   25 |       6 | Accept |      0.3282 |       3.957 |    0.049615 |    0.052032 |           29 |      0.57083 |            s |           12 |     0.044925 |
|   26 |       6 | Accept |     0.11051 |      4.8946 |    0.049615 |    0.046302 |           18 |       2.1221 |            r |           20 |    1.144e-06 |
|   27 |       6 | Accept |     0.16667 |      24.316 |    0.049615 |    0.045618 |          487 |       0.4395 |            r |           11 |       0.0496 |
|   28 |       6 | Accept |     0.41854 |      965.85 |    0.049615 |    0.048809 |          498 |     0.033885 |            r |          842 |   1.2635e-06 |
|   29 |       6 | Accept |    0.096205 |      9.4635 |    0.049615 |    0.048795 |            7 |       5.0586 |            r |          183 |      0.83636 |
|   30 |       6 | Accept |    0.050722 |      101.52 |    0.049615 |    0.050209 |           14 |       3.1604 |            s |          969 |   3.7785e-06 |
|   31 |       6 | Accept |     0.26034 |      6.9658 |    0.049615 |    0.049734 |           58 |       5.1596 |            s |           21 |    0.0005473 |
|   32 |       6 | Accept |     0.12776 |      20.259 |    0.049615 |    0.049734 |          248 |        9.949 |            r |           21 |      0.99061 |
|   33 |       6 | Accept |     0.09304 |      47.089 |    0.049615 |     0.04974 |            6 |       2.1073 |            r |          924 |   2.3816e-05 |
|   34 |       6 | Accept |    0.088285 |      34.007 |    0.049615 |    0.050909 |            5 |       9.6702 |            r |          442 |    0.0004791 |
|   35 |       6 | Accept |     0.09339 |      27.479 |    0.049615 |    0.049706 |           13 |       9.7304 |            r |          394 |   3.0327e-06 |
|   36 |       6 | Accept |    0.091085 |       62.75 |    0.049615 |    0.050493 |           17 |       8.5264 |            r |          980 |   1.1545e-06 |
|   37 |       6 | Accept |        0.17 |       12.57 |    0.049615 |    0.049746 |          190 |       5.3012 |            r |           10 |   0.00042575 |
|   38 |       6 | Accept |    0.086806 |      85.464 |    0.049615 |    0.049741 |            9 |       6.6819 |            r |          991 |     0.002056 |
|   39 |       6 | Accept |    0.075891 |      94.476 |    0.049615 |     0.04974 |           21 |       4.9454 |            r |          994 |      0.14463 |
|   40 |       6 | Accept |    0.078312 |      16.037 |    0.049615 |    0.049699 |           53 |        9.543 |            r |           92 |     0.015697 |
|============================================================================================================================================================|
| Iter | Active  | Eval   | Objective   | Objective   | BestSoFar   | BestSoFar   |      iterlim |       lambda |       solver |            q |    lambdareg |
|      | workers | result |             | runtime     | (observed)  | (estim.)    |              |              |              |              |              |
|============================================================================================================================================================|
|   41 |       6 | Accept |    0.089155 |      47.116 |    0.049615 |    0.049706 |            6 |       2.5293 |            r |          868 |      0.92378 |
|   42 |       6 | Accept |    0.056023 |      78.465 |    0.049615 |    0.049647 |           40 |       1.8207 |            s |          564 |   1.0475e-06 |
|   43 |       6 | Accept |    0.063352 |      72.628 |    0.049615 |    0.049656 |           12 |       8.7705 |            s |          957 |    5.727e-05 |
|   44 |       6 | Accept |    0.092644 |      56.239 |    0.049615 |    0.049964 |          104 |       1.4793 |            s |          235 |   3.2664e-06 |
|   45 |       6 | Accept |    0.055112 |      73.963 |    0.049615 |    0.049684 |            9 |       5.7467 |            s |          637 |   1.0436e-06 |
|   46 |       6 | Accept |     0.08649 |        1046 |    0.049615 |    0.049712 |          492 |       5.2609 |            r |          928 |      0.99518 |
|   47 |       6 | Accept |     0.07603 |      176.47 |    0.049615 |    0.049621 |          219 |      0.49896 |            r |          337 |     0.053179 |
|   48 |       6 | Accept |    0.075218 |      43.728 |    0.049615 |     0.04963 |           14 |       4.6009 |            r |          520 |      0.16968 |
|   49 |       6 | Accept |    0.088084 |      1016.5 |    0.049615 |    0.049896 |          476 |       7.0662 |            r |          919 |    1.058e-06 |
|   50 |       6 | Accept |     0.24315 |      47.732 |    0.049615 |    0.049826 |           15 |     0.028891 |            r |          665 |   1.7832e-06 |
|   51 |       6 | Accept |     0.05989 |       38.28 |    0.049615 |    0.049957 |            6 |       9.7534 |            s |          329 |   1.9938e-06 |
|   52 |       6 | Accept |     0.09876 |      6.4346 |    0.049615 |    0.049875 |            8 |       5.8723 |            r |           43 |   1.1255e-06 |
|   53 |       6 | Accept |    0.083956 |      10.221 |    0.049615 |    0.049947 |           41 |        2.431 |            r |           51 |    0.0091721 |
|   54 |       6 | Accept |    0.092206 |      46.604 |    0.049615 |    0.049909 |          426 |       9.7709 |            r |           33 |   1.6144e-06 |
|   55 |       6 | Accept |    0.078547 |      973.17 |    0.049615 |    0.049917 |          499 |       9.9769 |            r |          833 |     0.012758 |
|   56 |       6 | Accept |    0.050934 |      126.66 |    0.049615 |    0.049251 |           13 |       3.6028 |            s |          996 |   1.0591e-06 |
|   57 |       6 | Accept |     0.16122 |      4.3444 |    0.049615 |    0.049413 |           26 |       8.0895 |            r |           13 |      0.88933 |
|   58 |       6 | Accept |    0.088597 |      34.132 |    0.049615 |    0.049377 |            7 |       9.8372 |            r |          631 |       0.9506 |
|   59 |       6 | Accept |     0.11565 |      26.756 |    0.049615 |    0.049356 |          313 |       2.6184 |            r |           26 |      0.67946 |
|   60 |       6 | Accept |    0.090431 |      65.217 |    0.049615 |    0.049392 |           16 |       4.3879 |            r |          959 |   1.4487e-06 |

__________________________________________________________
Optimization completed.
MaxObjectiveEvaluations of 60 reached.
Total function evaluations: 60
Total elapsed time: 1771.623 seconds.
Total objective function evaluation time: 9951.9062

Best observed feasible point:
    iterlim    lambda    solver     q     lambdareg 
    _______    ______    ______    ___    __________

      24       1.1247      s       928    1.8948e-06

Observed objective function value = 0.049615
Estimated objective function value = 0.049392
Function evaluation time = 100.1503

Best estimated feasible point (according to models):
    iterlim    lambda    solver     q     lambdareg 
    _______    ______    ______    ___    __________

      24       1.1247      s       928    1.8948e-06

Estimated objective function value = 0.049392
Estimated function evaluation time = 99.6131

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
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);
Warning: Solver LBFGS was not able to converge to a solution. 

Remove the directory of the functions processMNISTdata and filterica from the search path.

rmpath(fullfile(matlabroot,'examples','stats'));

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

| | |

Related Examples

More About