Main Content

sparsefilt

Feature extraction by using sparse filtering

Description

Mdl= sparsefilt(X,q)returns a sparse filtering model object that contains the results from applying sparse filtering to the table or matrix of predictor dataXcontainingpvariables.qis the number of features to extract fromX, thereforesparsefiltlearns ap-by-qmatrix of transformation weights. For undercomplete or overcomplete feature representations,qcan be less than or greater than the number of predictor variables, respectively.

  • To access the learned transformation weights, useMdl.TransformWeights.

  • To transformXto the new set of features by using the learned transformation, passMdlandXtotransform.

example

Mdl= sparsefilt(X,q,Name,Value)uses additional options specified by one or moreName,Valuepair arguments. For example, you can standardize the predictor data or applyL2regularization.

Examples

collapse all

Create aSparseFilteringobject by using thesparsefiltfunction.

Load theSampleImagePatchesimage patches.

data = load('SampleImagePatches'); size(data.X)
ans =1×25000 363

There are 5,000 image patches, each containing 363 features.

Extract 100 features from the data.

rngdefault% For reproducibilityQ = 100; obj = sparsefilt(data.X,Q,'IterationLimit',100)
警告:解决LBFGS无法收敛a solution.
obj = SparseFiltering ModelParameters: [1x1 struct] NumPredictors: 363 NumLearnedFeatures: 100 Mu: [] Sigma: [] FitInfo: [1x1 struct] TransformWeights: [363x100 double] InitialTransformWeights: [] Properties, Methods

sparsefiltissues a warning because it stopped due to reaching the iteration limit, instead of reaching a step-size limit or a gradient-size limit. You can still use the learned features in the returned object by calling thetransformfunction.

Continue optimizing a sparse filter.

Load theSampleImagePatchesimage patches.

data = load('SampleImagePatches'); size(data.X)
ans =1×25000 363

There are 5,000 image patches, each containing 363 features.

Extract 100 features from the data and use an iteration limit of 20.

rngdefault% For reproducibilityq = 100; Mdl = sparsefilt(data.X,q,'IterationLimit',20);
警告:解决LBFGS无法收敛a solution.

View the resulting transformation matrix as image patches.

wts = Mdl.TransformWeights; W = reshape(wts,[11,11,3,q]); [dx,dy,~,~] = size(W);forf = 1:q Wvec = W(:,:,:,f); Wvec = Wvec(:); Wvec =(Wvec - min(Wvec))/(max(Wvec) - min(Wvec)); W(:,:,:,f) = reshape(Wvec,dx,dy,3);endm = ceil(sqrt(q)); n = m; img = zeros(m*dx,n*dy,3); f = 1;fori = 1:mforj = 1:nif(f <= q) img((i-1)*dx+1:i*dx,(j-1)*dy+1:j*dy,:) = W(:,:,:,f); f = f+1;endendendimshow(img,'InitialMagnification',300);

Figure contains an axes object. The axes object contains an object of type image.

The image patches appear noisy. To clean up the noise, try more iterations. Restart the optimization from where it stopped for another 40 iterations.

Mdl = sparsefilt(data.X,q,'IterationLimit',40,'InitialTransformWeights',wts);
警告:解决LBFGS无法收敛a solution.

View the updated transformation matrix as image patches.

wts = Mdl.TransformWeights; W = reshape(wts,[11,11,3,q]); [dx,dy,~,~] = size(W);forf = 1:q Wvec = W(:,:,:,f); Wvec = Wvec(:); Wvec =(Wvec - min(Wvec))/(max(Wvec) - min(Wvec)); W(:,:,:,f) = reshape(Wvec,dx,dy,3);endm = ceil(sqrt(q)); n = m; img = zeros(m*dx,n*dy,3); f = 1;fori = 1:mforj = 1:nif(f <= q) img((i-1)*dx+1:i*dx,(j-1)*dy+1:j*dy,:) = W(:,:,:,f); f = f+1;endendendimshow(img,'InitialMagnification',300);

Figure contains an axes object. The axes object contains an object of type image.

These images are less noisy.

Input Arguments

collapse all

Predictor data, specified as ann-by-pnumeric matrix or table. Rows correspond to individual observations and columns correspond to individual predictor variables. IfXis a table, then all of its variables must be numeric vectors.

Data Types:single|double|table

Number of features to extract from the predictor data, specified as a positive integer.

sparsefiltstores ap-by-qtransform weight matrix inMdl.TransformWeights. Therefore, setting very large values forqcan result in greater memory consumption and increased computation time.

Data Types:single|double

Name-Value Arguments

Specify optional pairs of arguments asName1=Value1,...,NameN=ValueN, whereNameis the argument name andValueis the corresponding value. Name-value arguments must appear after other arguments, but the order of the pairs does not matter.

Before R2021a, use commas to separate each name and value, and encloseNamein quotes.

Example:'Standardize',true,'Lambda',1standardizes the predictor data and applies a penalty of1to the transform weight matrix.

Maximum number of iterations, specified as the comma-separated pair consisting of'IterationLimit'and a positive integer.

Example:'IterationLimit',1e6

Data Types:single|double

Verbosity level for monitoring algorithm convergence, specified as the comma-separated pair consisting of'VerbosityLevel'and a value in this table.

Value Description
0 sparsefiltdoes not display convergence information at the command line.
Positive integer sparsefiltdisplays convergence information at the command line.

Convergence Information

Heading Meaning
FUN VALUE Objective function value.
NORM GRAD Norm of the gradient of the objective function.
NORM STEP Norm of the iterative step, meaning the distance between the previous point and the current point.
CURV OKmeans the weak Wolfe condition is satisfied. This condition is a combination of sufficient decrease of the objective function and a curvature condition.
GAMMA Inner product of the step times the gradient difference, divided by the inner product of the gradient difference with itself. The gradient difference is the gradient at the current point minus the gradient at the previous point. Gives diagnostic information on the objective function curvature.
ALPHA Step direction multiplier, which differs from1when the algorithm performed a line search.
ACCEPT YESmeans the algorithm found an acceptable step to take.

Example:'VerbosityLevel',1

Data Types:single|double

L2regularization coefficient value for the transform weight matrix, specified as the comma-separated pair consisting of'Lambda'scala和积极的数字r. If you specify0, the default, then there is no regularization term in the objective function.

Example:'Lambda',0.1

Data Types:single|double

Flag to standardize the predictor data, specified as the comma-separated pair consisting of'Standardize'andtrue(1) orfalse(0).

IfStandardizeistrue, then:

  • sparsefiltcenters and scales each column of the predictor data (X) by the column mean and standard deviation, respectively.

  • sparsefiltextracts new features by using the standardized predictor matrix, and stores the predictor variable means and standard deviations in propertiesMuandSigmaofMdl.

Example:'Standardize',true

Data Types:logical

Transformation weights that initialize optimization, specified as the comma-separated pair consisting of'InitialTransformWeights'and ap-by-qnumeric matrix.pmust be the number of columns or variables inXandqis the value ofq.

Tip

You can continue optimizing a previously returned transform weight matrix by passing it as an initial value in another call tosparsefilt. The output model objectMdlstores a learned transform weight matrix in theTransformWeightsproperty.

Example:'InitialTransformWeights',Mdl.TransformWeights

Data Types:single|double

Relative convergence tolerance on gradient norm, specified as the comma-separated pair consisting of'GradientTolerance'scala和积极的数字r. This gradient is the gradient of the objective function.

Example:'GradientTolerance',1e-4

Data Types:single|double

Absolute convergence tolerance on the step size, specified as the comma-separated pair consisting of'StepTolerance'scala和积极的数字r.

Example:'StepTolerance',1e-4

Data Types:single|double

Output Arguments

collapse all

Learned sparse filtering model, returned as aSparseFilteringmodel object.

To access properties ofMdl,使用点符号。对进行了le:

  • To access the learned transform weights, useMdl.TransformWeights.

  • To access the fitting information structure, useMdl.FitInfo.

To find sparse filtering coefficients for new data, use thetransformfunction.

Algorithms

Thesparsefiltfunction creates a nonlinear transformation of input features to output features. The transformation is based on optimizing an objective function that encourages the representation of each example by as few output features as possible while at the same time keeping the output features equally active across examples.

For details, seeSparse Filtering Algorithm.

Version History

Introduced in R2017a