MATLAB Examples

Feature Extraction Workflow

This example shows a complete workflow for feature extraction from image data.

Contents

Obtain Data

This example uses the MNIST image data, which consists of images of handwritten digits. The images are 28-by-28 pixels in grayscale. 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.

[Xtrain,LabelTrain] = processMNISTdata(imageFileName,labelFileName);
numrows = size(Xtrain,1);
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
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 | Parallel | Eval   | Objective  | Objective  | BestSoFar  | BestSoFar  |       Lambda |
|      | jobs     | result |            | runtime    | (observed) | (estim.)   |              |
|=============================================================================================|
|    1 |        4 | Best   |     0.8865 |     6.1325 |     0.8865 |     0.8865 |       8.8234 |
|    2 |        4 | Best   |     0.2041 |     6.8636 |     0.2041 |    0.33802 |     0.026804 |
|    3 |        4 | Best   |     0.1091 |     12.571 |     0.1091 |    0.18238 |   1.7309e-09 |
|    4 |        4 | Accept |     0.2042 |     6.5827 |     0.1091 |    0.10913 |     0.026821 |
|    5 |        4 | Accept |     0.2311 |     6.0866 |     0.1091 |    0.10912 |     0.051879 |
|    6 |        4 | Best   |     0.0973 |     20.342 |     0.0973 |   0.097317 |   8.0495e-06 |
|    7 |        4 | Best   |     0.0946 |     14.394 |     0.0946 |   0.094625 |   0.00013499 |
|    8 |        4 | Accept |     0.1456 |     7.7484 |     0.0946 |    0.09457 |    0.0066573 |
|    9 |        4 | Accept |     0.1071 |     13.472 |     0.0946 |   0.094599 |   3.0159e-08 |
|   10 |        4 | Accept |     0.1057 |     11.223 |     0.0946 |   0.094853 |   0.00071182 |
|   11 |        4 | Accept |     0.1027 |     16.709 |     0.0946 |   0.095012 |   5.4709e-07 |
|   12 |        4 | Best   |     0.0929 |     17.644 |     0.0929 |   0.093437 |   3.9603e-05 |
|   13 |        4 | Best   |     0.0911 |     15.599 |     0.0911 |   0.092669 |    8.337e-05 |
|   14 |        4 | Accept |     0.0933 |     16.775 |     0.0911 |   0.092679 |   5.2626e-05 |
|   15 |        4 | Accept |     0.1082 |     12.626 |     0.0911 |   0.092683 |   1.0062e-09 |
|   16 |        4 | Accept |      0.093 |     15.941 |     0.0911 |   0.092573 |   6.5233e-05 |
|   17 |        4 | Accept |     0.7999 |     5.6052 |     0.0911 |   0.092397 |      0.71139 |
|   18 |        4 | Accept |      0.105 |     15.386 |     0.0911 |    0.09239 |   1.2321e-07 |
|   19 |        4 | Accept |     0.1075 |     12.329 |     0.0911 |   0.092385 |   7.5069e-09 |
|   20 |        4 | Accept |      0.117 |     9.2747 |     0.0911 |   0.092402 |    0.0018588 |
|=============================================================================================|
| Iter | Parallel | Eval   | Objective  | Objective  | BestSoFar  | BestSoFar  |       Lambda |
|      | jobs     | result |            | runtime    | (observed) | (estim.)   |              |
|=============================================================================================|
|   21 |        4 | Accept |      0.101 |     17.878 |     0.0911 |   0.092399 |   2.0732e-06 |
|   22 |        4 | Accept |     0.0915 |     15.524 |     0.0911 |   0.091934 |   8.7215e-05 |
|   23 |        4 | Accept |     0.0972 |     12.625 |     0.0911 |   0.092009 |   0.00027432 |
|   24 |        4 | Accept |      0.097 |     19.339 |     0.0911 |   0.091991 |   1.8932e-05 |
|   25 |        4 | Accept |     0.0917 |     15.805 |     0.0911 |   0.091911 |   8.2616e-05 |
|   26 |        4 | Accept |     0.5107 |     5.4732 |     0.0911 |   0.091839 |      0.17911 |
|   27 |        4 | Accept |     0.8865 |     5.7215 |     0.0911 |   0.091885 |       2.9497 |
|   28 |        4 | Accept |     0.0915 |     15.617 |     0.0911 |     0.0918 |   8.4951e-05 |
|   29 |        4 | Accept |     0.1075 |     12.487 |     0.0911 |   0.091804 |   1.4926e-08 |
|   30 |        4 | Accept |     0.1292 |     8.5929 |     0.0911 |   0.091807 |    0.0035809 |

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

Best observed feasible point:
     Lambda  
    _________

    8.337e-05

Observed objective function value = 0.0911
Estimated objective function value = 0.091807
Function evaluation time = 15.5993

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

    8.2616e-05

Estimated objective function value = 0.091807
Estimated function evaluation time = 15.6212

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

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 | Parallel | Eval   | Objective  | Objective  | BestSoFar  | BestSoFar  |       Lambda |
|      | jobs     | result |            | runtime    | (observed) | (estim.)   |              |
|=============================================================================================|
|    1 |        4 | Best   |     0.0779 |      57.11 |     0.0779 |     0.0779 |   1.3269e-06 |
|    2 |        4 | Accept |     0.0779 |     57.208 |     0.0779 |     0.0779 |   3.8643e-09 |
|    3 |        4 | Accept |     0.0779 |     57.159 |     0.0779 |     0.0779 |   1.0024e-09 |
|    4 |        4 | Accept |     0.0787 |     173.74 |     0.0779 |     0.0779 |     0.011605 |
|    5 |        4 | Accept |     0.0779 |     57.848 |     0.0779 |     0.0779 |   1.3583e-07 |
|    6 |        4 | Best   |     0.0776 |     207.36 |     0.0776 |   0.077983 |    0.0010049 |
|    7 |        4 | Accept |     0.0779 |     57.409 |     0.0776 |   0.077971 |   4.8798e-07 |
|    8 |        4 | Accept |     0.0783 |     187.99 |     0.0776 |   0.078012 |     0.005753 |
|    9 |        4 | Best   |     0.0744 |     312.93 |     0.0744 |     0.0744 |        6.805 |
|   10 |        4 | Accept |     0.0783 |     91.396 |     0.0744 |     0.0744 |   4.2136e-05 |
|   11 |        4 | Accept |     0.0769 |     169.28 |     0.0744 |   0.074808 |     0.031427 |
|   12 |        4 | Accept |     0.0767 |     247.13 |     0.0744 |   0.075749 |       1.6879 |
|   13 |        4 | Best   |     0.0716 |     334.13 |     0.0716 |   0.074214 |       9.1102 |
|   14 |        4 | Accept |     0.0728 |     332.66 |     0.0716 |   0.073604 |       9.9198 |
|   15 |        4 | Accept |     0.0779 |     56.869 |     0.0716 |   0.073559 |   2.1447e-08 |
|   16 |        4 | Accept |     0.0719 |     329.36 |     0.0716 |    0.07307 |       9.9873 |
|   17 |        4 | Accept |      0.078 |     58.842 |     0.0716 |   0.073048 |   7.9207e-06 |
|   18 |        4 | Accept |      0.073 |     319.72 |     0.0716 |   0.072646 |       7.0371 |
|   19 |        4 | Accept |     0.0779 |     58.209 |     0.0716 |   0.072618 |   5.2693e-08 |
|   20 |        4 | Accept |     0.0733 |     338.53 |     0.0716 |    0.07294 |       9.9916 |
|=============================================================================================|
| Iter | Parallel | Eval   | Objective  | Objective  | BestSoFar  | BestSoFar  |       Lambda |
|      | jobs     | result |            | runtime    | (observed) | (estim.)   |              |
|=============================================================================================|
|   21 |        4 | Accept |     0.0784 |     166.29 |     0.0716 |   0.072774 |      0.21574 |
|   22 |        4 | Accept |     0.0779 |     57.901 |     0.0716 |   0.072757 |   3.3591e-06 |
|   23 |        4 | Accept |     0.0779 |     175.52 |     0.0716 |   0.072748 |   0.00017067 |
|   24 |        4 | Accept |     0.0779 |     57.903 |     0.0716 |   0.072734 |   8.6455e-09 |
|   25 |        4 | Accept |     0.0767 |     192.41 |     0.0716 |   0.072785 |      0.59114 |
|   26 |        4 | Accept |     0.0781 |     67.523 |     0.0716 |   0.072774 |   1.8095e-05 |
|   27 |        4 | Accept |     0.0779 |     56.024 |     0.0716 |   0.072762 |   1.7531e-09 |
|   28 |        4 | Accept |     0.0782 |     158.12 |     0.0716 |   0.072752 |     0.076056 |
|   29 |        4 | Accept |     0.0779 |      58.37 |     0.0716 |   0.072742 |   2.5711e-07 |
|   30 |        4 | Accept |     0.0727 |     327.58 |     0.0716 |   0.072726 |       9.9965 |

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

Best observed feasible point:
    Lambda
    ______

    9.1102

Observed objective function value = 0.0716
Estimated objective function value = 0.072726
Function evaluation time = 334.1341

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

    9.9965

Estimated objective function value = 0.072726
Estimated function evaluation time = 332.7126


Losso =

    0.0859

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 | Parallel | Eval   | Objective  | Objective  | BestSoFar  | BestSoFar  |       Lambda |
|      | jobs     | result |            | runtime    | (observed) | (estim.)   |              |
|=============================================================================================|
|    1 |        4 | Best   |     0.2146 |     10.042 |     0.2146 |     0.2146 |     0.069755 |
|    2 |        4 | Best   |     0.0615 |     11.875 |     0.0615 |   0.069571 |     2.13e-09 |
|    3 |        4 | Best   |     0.0582 |     19.778 |     0.0582 |   0.058213 |   2.1158e-07 |
|    4 |        4 | Accept |      0.067 |     21.605 |     0.0582 |   0.060443 |   0.00043586 |
|    5 |        4 | Accept |     0.0613 |      11.27 |     0.0582 |   0.058198 |   5.1428e-09 |
|    6 |        4 | Accept |     0.0582 |     20.023 |     0.0582 |   0.058055 |   2.6329e-07 |
|    7 |        4 | Accept |     0.0596 |     12.579 |     0.0582 |   0.058056 |   3.9519e-08 |
|    8 |        4 | Accept |     0.0621 |     11.577 |     0.0582 |    0.05805 |   1.0044e-09 |
|    9 |        4 | Accept |     0.8865 |     9.6478 |     0.0582 |   0.058195 |       9.9935 |
|   10 |        4 | Best   |     0.0565 |     28.664 |     0.0565 |   0.058005 |   2.6533e-06 |
|   11 |        4 | Accept |     0.0909 |     16.912 |     0.0565 |   0.056423 |    0.0024064 |
|   12 |        4 | Best   |     0.0553 |     37.868 |     0.0553 |   0.055225 |   2.2579e-05 |
|   13 |        4 | Accept |     0.0564 |     30.593 |     0.0553 |   0.054954 |   9.8323e-05 |
|   14 |        4 | Accept |     0.0581 |     27.268 |     0.0553 |   0.054913 |   1.0786e-06 |
|   15 |        4 | Accept |     0.0556 |     39.009 |     0.0553 |    0.05494 |   9.5822e-06 |
|   16 |        4 | Accept |     0.0556 |     39.835 |     0.0553 |   0.055186 |   2.4374e-05 |
|   17 |        4 | Best   |     0.0551 |      37.67 |     0.0551 |   0.055142 |   3.8837e-05 |
|   18 |        4 | Accept |     0.0601 |     10.875 |     0.0551 |   0.055143 |   1.5111e-08 |
|   19 |        4 | Accept |     0.1338 |     11.441 |     0.0551 |   0.055159 |     0.012242 |
|   20 |        4 | Accept |     0.0588 |     16.072 |     0.0551 |    0.05516 |   8.8967e-08 |
|=============================================================================================|
| Iter | Parallel | Eval   | Objective  | Objective  | BestSoFar  | BestSoFar  |       Lambda |
|      | jobs     | result |            | runtime    | (observed) | (estim.)   |              |
|=============================================================================================|
|   21 |        4 | Accept |     0.8001 |     8.6042 |     0.0551 |   0.055413 |      0.76226 |
|   22 |        4 | Accept |     0.0554 |     35.233 |     0.0551 |   0.055267 |   3.4027e-05 |
|   23 |        4 | Accept |     0.0575 |      22.67 |     0.0551 |   0.055265 |   5.1241e-07 |
|   24 |        4 | Accept |     0.0576 |     34.061 |     0.0551 |   0.055293 |   4.9756e-06 |
|   25 |        4 | Accept |     0.0606 |     27.611 |     0.0551 |   0.055293 |   0.00018987 |
|   26 |        4 | Accept |     0.0556 |      34.85 |     0.0551 |   0.055331 |   6.0294e-05 |
|   27 |        4 | Accept |     0.0565 |     38.832 |     0.0551 |   0.055283 |   1.3985e-05 |
|   28 |        4 | Accept |     0.0557 |     35.797 |     0.0551 |    0.05531 |   5.7013e-05 |
|   29 |        4 | Best   |     0.0548 |     35.756 |     0.0548 |   0.055255 |   5.1687e-05 |
|   30 |        4 | Accept |     0.5449 |     8.4334 |     0.0548 |   0.055266 |      0.19906 |

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

Best observed feasible point:
      Lambda  
    __________

    5.1687e-05

Observed objective function value = 0.0548
Estimated objective function value = 0.055266
Function evaluation time = 35.756

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

    5.1687e-05

Estimated objective function value = 0.055266
Estimated function evaluation time = 35.4954


Loss2 =

    0.0653

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, you "prewhiten" the input data prior to applying rica, which makes the preprocessed predictors have zero mean and identity covariance. Preprocess the predictors to make them zero mean and unit variance using the Standardize name-value pair. Alternatively, you can tranform 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 | Parallel | Eval   | Objective  | Objective  | BestSoFar  | BestSoFar  |       Lambda |
|      | jobs     | result |            | runtime    | (observed) | (estim.)   |              |
|=============================================================================================|
|    1 |        4 | Best   |       0.11 |     15.069 |       0.11 |       0.11 |       6.4639 |
|    2 |        4 | Best   |     0.0828 |      16.65 |     0.0828 |   0.084192 |   2.0708e-08 |
|    3 |        4 | Best   |     0.0825 |     23.347 |     0.0825 |   0.082502 |   1.3261e-05 |
|    4 |        4 | Best   |     0.0671 |     31.728 |     0.0671 |   0.070823 |     0.006235 |
|    5 |        4 | Accept |     0.0833 |     15.697 |     0.0671 |   0.067103 |   6.7203e-08 |
|    6 |        4 | Accept |     0.0831 |     15.868 |     0.0671 |   0.067103 |   7.4434e-09 |
|    7 |        4 | Accept |     0.0831 |     23.287 |     0.0671 |   0.067103 |   1.5708e-06 |
|    8 |        4 | Best   |     0.0625 |     29.925 |     0.0625 |   0.062506 |     0.027771 |
|    9 |        4 | Accept |      0.068 |     32.448 |     0.0625 |   0.062512 |    0.0046091 |
|   10 |        4 | Accept |     0.0781 |     36.021 |     0.0625 |   0.062571 |   0.00053165 |
|   11 |        4 | Accept |     0.0709 |     32.144 |     0.0625 |   0.062566 |    0.0028883 |
|   12 |        4 | Best   |      0.062 |     28.614 |      0.062 |    0.06222 |      0.03425 |
|   13 |        4 | Best   |     0.0614 |     29.659 |     0.0614 |   0.061643 |      0.04738 |
|   14 |        4 | Accept |     0.0628 |     30.446 |     0.0614 |   0.061584 |     0.029192 |
|   15 |        4 | Accept |     0.0631 |     28.154 |     0.0614 |   0.061513 |     0.027822 |
|   16 |        4 | Accept |      0.082 |     15.822 |     0.0614 |   0.061545 |   1.0036e-09 |
|   17 |        4 | Accept |     0.0615 |     29.805 |     0.0614 |   0.061591 |     0.060363 |
|   18 |        4 | Best   |     0.0613 |     29.221 |     0.0613 |   0.061374 |     0.064793 |
|   19 |        4 | Accept |     0.0626 |     29.106 |     0.0613 |   0.061782 |     0.067101 |
|   20 |        4 | Accept |     0.0625 |     29.502 |     0.0613 |   0.061875 |      0.06363 |
|=============================================================================================|
| Iter | Parallel | Eval   | Objective  | Objective  | BestSoFar  | BestSoFar  |       Lambda |
|      | jobs     | result |            | runtime    | (observed) | (estim.)   |              |
|=============================================================================================|
|   21 |        4 | Accept |     0.0776 |     19.345 |     0.0613 |   0.061943 |      0.95708 |
|   22 |        4 | Accept |     0.0807 |     24.343 |     0.0613 |   0.061944 |   7.1209e-05 |
|   23 |        4 | Accept |     0.0825 |      20.78 |     0.0613 |   0.061943 |   3.4271e-07 |
|   24 |        4 | Accept |     0.0658 |     24.693 |     0.0613 |    0.06195 |      0.23533 |
|   25 |        4 | Accept |     0.0617 |     29.622 |     0.0613 |   0.061921 |     0.046981 |
|   26 |        4 | Accept |     0.1246 |     13.382 |     0.0613 |   0.061933 |       9.9886 |
|   27 |        4 | Accept |     0.0613 |     27.734 |     0.0613 |   0.061868 |     0.047823 |
|   28 |        4 | Accept |     0.0613 |     28.522 |     0.0613 |   0.061814 |     0.048215 |
|   29 |        4 | Accept |     0.0813 |     16.097 |     0.0613 |   0.061815 |   2.2284e-09 |
|   30 |        4 | Accept |     0.0806 |     23.225 |     0.0613 |   0.061817 |   4.6552e-06 |

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

Best observed feasible point:
     Lambda 
    ________

    0.064793

Observed objective function value = 0.0613
Estimated objective function value = 0.061817
Function evaluation time = 29.2211

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

    0.048215

Estimated objective function value = 0.061817
Estimated function evaluation time = 29.1709


Loss3 =

    0.0745

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 | Parallel | Eval   | Objective  | Objective  | BestSoFar  | BestSoFar  |       Lambda |
|      | jobs     | result |            | runtime    | (observed) | (estim.)   |              |
|=============================================================================================|
|    1 |        4 | Best   |     0.8865 |     64.379 |     0.8865 |     0.8865 |       6.5626 |
|    2 |        4 | Best   |     0.0417 |     65.812 |     0.0417 |    0.08212 |   1.0187e-09 |
|    3 |        4 | Accept |     0.1195 |     103.19 |     0.0417 |    0.10094 |    0.0098397 |
|    4 |        4 | Accept |     0.0419 |     65.801 |     0.0417 |   0.041722 |   1.9108e-09 |
|    5 |        4 | Best   |     0.0409 |     65.937 |     0.0409 |   0.040862 |   1.4398e-08 |
|    6 |        4 | Accept |     0.1474 |     88.664 |     0.0409 |   0.040952 |     0.018667 |
|    7 |        4 | Best   |     0.0403 |     72.769 |     0.0403 |    0.04034 |   5.1251e-08 |
|    8 |        4 | Best   |     0.0368 |     313.66 |     0.0368 |   0.036834 |   4.8843e-06 |
|    9 |        4 | Accept |     0.0388 |     182.27 |     0.0368 |   0.036847 |   3.2716e-07 |
|   10 |        4 | Accept |      0.039 |     83.211 |     0.0368 |   0.036869 |   8.4237e-08 |
|   11 |        4 | Accept |     0.0461 |      232.6 |     0.0368 |   0.036823 |   0.00032572 |
|   12 |        4 | Accept |     0.0377 |      249.8 |     0.0368 |   0.036841 |   1.5451e-06 |
|   13 |        4 | Accept |     0.0368 |     350.11 |     0.0368 |   0.036879 |    6.161e-06 |
|   14 |        4 | Best   |     0.0364 |        398 |     0.0364 |   0.036693 |   2.2945e-05 |
|   15 |        4 | Best   |     0.0363 |     398.64 |     0.0363 |   0.036343 |   2.7992e-05 |
|   16 |        4 | Accept |     0.0406 |     67.555 |     0.0363 |   0.036342 |   5.3917e-09 |
|   17 |        4 | Accept |     0.7228 |     52.853 |     0.0363 |   0.036179 |      0.34235 |
|   18 |        4 | Accept |      0.037 |     379.28 |     0.0363 |    0.03651 |   1.1988e-05 |
|   19 |        4 | Accept |     0.0366 |     362.87 |     0.0363 |   0.036754 |   9.4731e-06 |
|   20 |        4 | Accept |     0.0396 |     124.02 |     0.0363 |   0.036755 |   1.5775e-07 |
|=============================================================================================|
| Iter | Parallel | Eval   | Objective  | Objective  | BestSoFar  | BestSoFar  |       Lambda |
|      | jobs     | result |            | runtime    | (observed) | (estim.)   |              |
|=============================================================================================|
|   21 |        4 | Accept |     0.0363 |     381.82 |     0.0363 |   0.036297 |   3.0466e-05 |
|   22 |        4 | Accept |     0.0388 |     310.98 |     0.0363 |   0.036355 |   0.00011202 |
|   23 |        4 | Accept |     0.0408 |      68.59 |     0.0363 |   0.036355 |   2.8221e-08 |
|   24 |        4 | Accept |     0.0382 |     213.56 |     0.0363 |   0.036356 |   7.2711e-07 |
|   25 |        4 | Accept |     0.0369 |     282.89 |     0.0363 |   0.036355 |   2.7988e-06 |
|   26 |        4 | Accept |     0.0371 |      360.5 |     0.0363 |   0.036341 |   6.2397e-05 |
|   27 |        4 | Accept |     0.0365 |      381.1 |     0.0363 |   0.036337 |   4.1496e-05 |
|   28 |        4 | Best   |     0.0362 |     387.77 |     0.0362 |   0.036293 |   1.8031e-05 |
|   29 |        4 | Accept |     0.0365 |     303.78 |     0.0362 |   0.036291 |   3.3163e-06 |
|   30 |        4 | Best   |     0.0361 |     388.37 |     0.0361 |   0.036245 |   3.7922e-05 |

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

Best observed feasible point:
      Lambda  
    __________

    3.7922e-05

Observed objective function value = 0.0361
Estimated objective function value = 0.036245
Function evaluation time = 388.3677

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

    3.0466e-05

Estimated objective function value = 0.036245
Estimated function evaluation time = 391.4562


Loss4 =

    0.0438

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 | Parallel | Eval   | Objective  | Objective  | BestSoFar  | BestSoFar  |       Lambda |
|      | jobs     | result |            | runtime    | (observed) | (estim.)   |              |
|=============================================================================================|
|    1 |        4 | Best   |     0.0802 |     55.922 |     0.0802 |     0.0802 |   1.4418e-09 |
|    2 |        4 | Accept |     0.1005 |     139.27 |     0.0802 |   0.081235 |       3.7619 |
|    3 |        4 | Best   |     0.0801 |       56.1 |     0.0801 |   0.080105 |   4.5043e-09 |
|    4 |        4 | Best   |     0.0788 |     199.43 |     0.0788 |   0.078803 |    3.107e-06 |
|    5 |        4 | Accept |     0.0799 |      55.68 |     0.0788 |   0.078804 |   1.3381e-08 |
|    6 |        4 | Best   |     0.0664 |     299.06 |     0.0664 |   0.066402 |     0.018935 |
|    7 |        4 | Accept |     0.0682 |     253.72 |     0.0664 |   0.066401 |      0.34439 |
|    8 |        4 | Accept |     0.0793 |     201.26 |     0.0664 |   0.066401 |   6.0661e-07 |
|    9 |        4 | Accept |     0.0788 |     215.51 |     0.0664 |     0.0664 |   1.3752e-06 |
|   10 |        4 | Accept |     0.0795 |     223.56 |     0.0639 |   0.063896 |   8.5981e-05 |
|   11 |        4 | Best   |     0.0639 |     316.29 |     0.0639 |   0.063896 |     0.092407 |
|   12 |        4 | Accept |       0.07 |     396.93 |     0.0639 |   0.063898 |    0.0018022 |
|   13 |        4 | Accept |     0.0682 |     345.56 |     0.0639 |   0.063895 |    0.0073997 |
|   14 |        4 | Accept |     0.0641 |     317.25 |     0.0639 |   0.063926 |      0.11559 |
|   15 |        4 | Accept |     0.0649 |     326.07 |     0.0639 |   0.064254 |     0.082293 |
|   16 |        4 | Best   |     0.0637 |     312.77 |     0.0637 |   0.064088 |      0.10092 |
|   17 |        4 | Accept |     0.0795 |     56.141 |     0.0637 |   0.064094 |   6.5578e-08 |
|   18 |        4 | Accept |     0.0649 |     322.81 |     0.0637 |   0.064237 |     0.089207 |
|   19 |        4 | Accept |     0.1258 |        117 |     0.0637 |   0.064304 |        9.981 |
|   20 |        4 | Accept |     0.0644 |     309.13 |     0.0637 |   0.064302 |      0.11639 |
|=============================================================================================|
| Iter | Parallel | Eval   | Objective  | Objective  | BestSoFar  | BestSoFar  |       Lambda |
|      | jobs     | result |            | runtime    | (observed) | (estim.)   |              |
|=============================================================================================|
|   21 |        4 | Accept |     0.0646 |     317.53 |     0.0637 |   0.064343 |      0.08175 |
|   22 |        4 | Accept |     0.0803 |     55.455 |     0.0637 |   0.064343 |   1.0049e-09 |
|   23 |        4 | Accept |     0.0799 |     187.16 |     0.0637 |   0.064343 |    1.888e-05 |
|   24 |        4 | Accept |      0.077 |     208.49 |     0.0637 |   0.064328 |      0.86471 |
|   25 |        4 | Accept |       0.08 |     57.795 |     0.0637 |   0.064328 |   1.7675e-07 |
|   26 |        4 | Accept |     0.0779 |     359.13 |     0.0637 |   0.064324 |    0.0004205 |
|   27 |        4 | Accept |     0.0652 |     286.35 |     0.0637 |   0.064317 |     0.036644 |
|   28 |        4 | Accept |     0.0642 |     312.19 |     0.0637 |   0.064295 |      0.11071 |
|   29 |        4 | Accept |       0.08 |     56.205 |     0.0637 |   0.064295 |   2.9625e-08 |
|   30 |        4 | Accept |     0.0796 |     187.78 |     0.0637 |   0.064294 |   8.0657e-06 |

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

Best observed feasible point:
    Lambda 
    _______

    0.10092

Observed objective function value = 0.0637
Estimated objective function value = 0.064294
Function evaluation time = 312.7696

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

    0.10092

Estimated objective function value = 0.064294
Estimated function evaluation time = 316.708


Loss5 =

    0.0746

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 | Parallel | Eval   | Objective  | Objective  | BestSoFar  | BestSoFar  |      iterlim |       lambda |       solver |            q |    lambdareg |
|      | jobs     | result |            | runtime    | (observed) | (estim.)   |              |              |              |              |              |
|=========================================================================================================================================================|
|    1 |        4 | Best   |     0.1166 |     25.711 |     0.1166 |     0.1166 |          225 |       1.4675 |            r |           17 |   1.9214e-05 |
|    2 |        4 | Accept |    0.71605 |     76.241 |     0.1166 |      0.168 |          226 |       1.1718 |            s |          115 |       0.4532 |
|    3 |        4 | Best   |   0.087949 |     91.947 |   0.087949 |   0.089846 |          351 |       1.5536 |            r |           66 |   1.7003e-05 |
|    4 |        4 | Accept |   0.089246 |     181.63 |   0.087949 |    0.08788 |           62 |       6.4073 |            r |          729 |   1.8026e-05 |
|    5 |        4 | Best   |   0.075686 |     186.94 |   0.075686 |   0.088336 |          181 |       7.4978 |            r |          281 |      0.22079 |
|    6 |        4 | Accept |   0.083408 |     87.642 |   0.075686 |   0.087038 |          100 |       9.9999 |            r |          229 |      0.70367 |
|    7 |        4 | Accept |   0.085505 |      121.9 |   0.075686 |   0.083637 |           17 |       9.9987 |            r |          890 |    0.0015107 |
|    8 |        4 | Accept |   0.089916 |     435.74 |   0.075686 |    0.08727 |          199 |       1.9114 |            r |          620 |   4.1746e-06 |
|    9 |        4 | Accept |   0.081326 |     737.08 |   0.075686 |   0.083747 |          238 |       4.6867 |            r |          853 |    0.0045154 |
|   10 |        4 | Accept |   0.092619 |     411.11 |   0.075686 |   0.083186 |          481 |       9.9969 |            r |          242 |   7.2334e-05 |
|   11 |        4 | Accept |    0.12474 |     224.74 |   0.075686 |   0.081713 |          134 |       9.9984 |            s |          545 |   0.00026652 |
|   12 |        4 | Accept |   0.086312 |     104.82 |   0.075686 |   0.081801 |           18 |       9.8718 |            r |          999 |       0.9734 |
|   13 |        4 | Accept |   0.085033 |     119.54 |   0.075686 |   0.082712 |          261 |       7.2483 |            r |          128 |      0.67832 |
|   14 |        4 | Accept |   0.077026 |     415.92 |   0.075686 |   0.081297 |          444 |       7.4977 |            r |          270 |      0.28339 |
|   15 |        4 | Accept |    0.20179 |     138.29 |   0.075686 |   0.078728 |           59 |       6.4562 |            s |          638 |     0.012154 |
|   16 |        4 | Accept |   0.089593 |     1687.4 |   0.075686 |   0.079257 |          492 |        8.479 |            r |          993 |   8.7092e-06 |
|   17 |        4 | Accept |   0.086069 |     1355.9 |   0.075686 |   0.082026 |          400 |       4.1744 |            r |          989 |      0.94452 |
|   18 |        4 | Accept |   0.094693 |     13.743 |   0.075686 |   0.079135 |           56 |       3.0147 |            r |           29 |   0.00015051 |
|   19 |        4 | Accept |    0.15955 |     755.64 |   0.075686 |   0.082258 |          268 |       9.9851 |            s |          993 |   0.00068062 |
|   20 |        4 | Accept |    0.45329 |     5.8813 |   0.075686 |   0.076457 |           47 |       9.8951 |            s |           12 |       0.2191 |
|=========================================================================================================================================================|
| Iter | Parallel | Eval   | Objective  | Objective  | BestSoFar  | BestSoFar  |      iterlim |       lambda |       solver |            q |    lambdareg |
|      | jobs     | result |            | runtime    | (observed) | (estim.)   |              |              |              |              |              |
|=========================================================================================================================================================|
|   21 |        4 | Accept |     0.1278 |     6.8923 |   0.075686 |   0.074967 |           30 |        6.061 |            r |           17 |      0.38399 |
|   22 |        4 | Accept |    0.29678 |     19.171 |   0.075686 |   0.075986 |            6 |     0.019283 |            r |          256 |     0.034675 |
|   23 |        4 | Accept |   0.096232 |     17.294 |   0.075686 |   0.072978 |            7 |       3.6632 |            r |          221 |   2.0743e-05 |
|   24 |        4 | Accept |    0.11569 |       8.09 |   0.075686 |   0.071506 |           41 |       7.2688 |            r |           17 |   1.7003e-06 |
|   25 |        4 | Accept |    0.17124 |     29.686 |   0.075686 |   0.076131 |          357 |       3.8963 |            r |           10 |   1.1588e-06 |
|   26 |        4 | Accept |    0.11884 |     8.0113 |   0.075686 |   0.075665 |           20 |        1.033 |            s |           38 |   0.00010145 |
|   27 |        4 | Accept |   0.092672 |     59.528 |   0.075686 |   0.075077 |            9 |       0.6472 |            r |          992 |   1.1883e-06 |
|   28 |        4 | Accept |    0.15897 |      11.83 |   0.075686 |   0.070814 |           45 |       8.5223 |            s |           37 |   2.1289e-05 |
|   29 |        4 | Accept |    0.14063 |     6.5558 |   0.075686 |   0.069568 |           32 |       4.9384 |            r |           12 |   0.00045937 |
|   30 |        4 | Accept |   0.095535 |     72.362 |   0.075686 |   0.068216 |          495 |       1.5754 |            r |           29 |   0.00033545 |
|   31 |        4 | Accept |   0.082814 |       1406 |   0.075686 |   0.069761 |          495 |       5.0123 |            r |          828 |      0.68736 |
|   32 |        4 | Accept |   0.078977 |     12.791 |   0.075686 |   0.069219 |            9 |       9.7091 |            r |          132 |     0.027423 |
|   33 |        4 | Accept |    0.14244 |     6.2437 |   0.075686 |   0.069104 |           11 |       5.4822 |            s |           53 |   0.00064985 |
|   34 |        4 | Accept |    0.25919 |     5.1841 |   0.075686 |   0.068823 |           27 |       8.2361 |            s |           14 |   1.2617e-06 |
|   35 |        4 | Accept |   0.082781 |     115.91 |   0.075686 |   0.071099 |          478 |       9.6558 |            r |           59 |    0.0027496 |
|   36 |        4 | Accept |   0.080993 |     138.05 |   0.075686 |   0.077505 |          498 |        9.992 |            r |           71 |      0.02564 |
|   37 |        4 | Accept |    0.18643 |     7.7357 |   0.075686 |    0.07372 |           33 |       1.8603 |            s |           24 |   2.4526e-06 |
|   38 |        4 | Accept |   0.096094 |     142.51 |   0.075686 |   0.073644 |           28 |      0.38874 |            r |          981 |   5.2825e-05 |
|   39 |        4 | Accept |   0.091199 |     10.004 |   0.075686 |    0.07333 |           32 |       9.6902 |            r |           34 |    1.747e-05 |
|   40 |        4 | Best   |   0.074537 |     403.37 |   0.074537 |   0.068331 |          105 |       9.9842 |            r |          996 |      0.13585 |
|=========================================================================================================================================================|
| Iter | Parallel | Eval   | Objective  | Objective  | BestSoFar  | BestSoFar  |      iterlim |       lambda |       solver |            q |    lambdareg |
|      | jobs     | result |            | runtime    | (observed) | (estim.)   |              |              |              |              |              |
|=========================================================================================================================================================|
|   41 |        4 | Accept |   0.087459 |     314.12 |   0.074537 |   0.068088 |          498 |       9.8831 |            r |          177 |      0.94706 |
|   42 |        4 | Accept |   0.089712 |     113.91 |   0.074537 |   0.068136 |           25 |       9.9151 |            r |          894 |   1.5403e-05 |
|   43 |        4 | Accept |     0.1144 |     25.419 |   0.074537 |   0.073893 |          220 |       9.9223 |            r |           18 |     0.050258 |
|   44 |        4 | Accept |   0.083762 |     99.778 |   0.074537 |   0.079615 |          478 |       7.0265 |            r |           49 |     0.018268 |
|   45 |        4 | Accept |   0.088283 |     25.546 |   0.074537 |   0.079767 |           42 |       9.9814 |            r |          116 |   1.3005e-06 |
|   46 |        4 | Accept |    0.13867 |     25.672 |   0.074537 |   0.079374 |           65 |      0.32698 |            s |           99 |   0.00064251 |
|   47 |        4 | Accept |    0.10812 |     1196.2 |   0.074537 |   0.076877 |          429 |     0.045336 |            s |          985 |   2.0492e-06 |
|   48 |        4 | Accept |   0.090668 |     64.519 |   0.074537 |   0.076929 |            7 |       9.8007 |            r |          933 |   1.3313e-06 |
|   49 |        4 | Accept |   0.080217 |     151.22 |   0.074537 |   0.076138 |           25 |       9.2966 |            r |          985 |     0.013661 |
|   50 |        4 | Best   |   0.066878 |     118.07 |   0.066878 |   0.075555 |           17 |       2.4175 |            s |          998 |    9.838e-05 |
|   51 |        4 | Best   |   0.050424 |     110.41 |   0.050424 |   0.055395 |           18 |     0.062887 |            s |          800 |    8.698e-05 |
|   52 |        4 | Accept |   0.078742 |     103.03 |   0.050424 |   0.056657 |            7 |       7.5208 |            r |          966 |     0.066909 |
|   53 |        4 | Accept |   0.054667 |     129.35 |   0.050424 |   0.051813 |           21 |      0.49091 |            s |          998 |    4.749e-05 |
|   54 |        4 | Accept |   0.069942 |     253.63 |   0.050424 |   0.057901 |           69 |      0.21365 |            s |          948 |   7.1044e-05 |
|   55 |        4 | Accept |   0.087118 |     235.77 |   0.050424 |   0.057902 |           65 |        1.033 |            s |          995 |   0.00024416 |
|   56 |        4 | Best   |   0.046525 |     81.362 |   0.046525 |    0.05457 |            7 |      0.15531 |            s |          913 |   5.0013e-05 |
|   57 |        4 | Accept |   0.067507 |     303.24 |   0.046525 |   0.056027 |           84 |      0.23207 |            s |          980 |   2.4676e-05 |
|   58 |        4 | Accept |   0.089009 |       1696 |   0.046525 |   0.057513 |          486 |       4.7819 |            r |          987 |   0.00023139 |
|   59 |        4 | Accept |    0.12258 |     1218.6 |   0.046525 |   0.052949 |          435 |      0.13322 |            s |          987 |   5.1053e-05 |
|   60 |        4 | Accept |   0.053558 |      87.08 |   0.046525 |   0.052416 |           10 |      0.60538 |            s |          848 |   1.5123e-05 |

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

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

    7          0.15531    s         913    5.0013e-05

Observed objective function value = 0.046525
Estimated objective function value = 0.052416
Function evaluation time = 81.3616

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

    7          0.15531    s         913    5.0013e-05

Estimated objective function value = 0.052416
Estimated function evaluation time = 91.2652

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. 

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);