Code Generation for Nearest Neighbor Searcher
The object functionsknnsearch
andrangesearch
of the nearest neighbor searcher objects,ExhaustiveSearcher
andKDTreeSearcher
, support code generation. This example shows how to generate code for finding the nearest neighbor using an exhaustive searcher object at the command line. The example shows two different ways to generate code, depending on the way you use the object: load the object by usingloadLearnerForCoder
in an entry-point function, and pass a compile-time constant object to the generated code.
Train Exhaustive Nearest Neighbor Searcher
Load Fisher's iris data set.
loadfisheriris
Remove five irises randomly from the predictor data to use as a query set.
rng('default');% For reproducibilityn = size(meas,1);% Sample sizeqIdx = randsample(n,5);% Indices of query dataX = meas(~ismember(1:n,qIdx),:); Y = meas(qIdx,:);
Prepare an exhaustive nearest neighbor searcher using the training data. Specify the'Distance'
and'P'
name-value pair arguments to use the Minkowski distance with an exponent of 1 for finding the nearest neighbor.
Mdl = ExhaustiveSearcher(X,'Distance','minkowski','P',1);
Find the index of the training data (X
) that is the nearest neighbor of each point in the query data (Y
).
Idx = knnsearch(Mdl,Y);
Generate Code UsingsaveLearnerForCoder
andloadLearnerForCoder
Generate code that loads an exhaustive searcher, takes query data as an input argument, and then finds the nearest neighbor.
Save the exhaustive searcher to a file usingsaveLearnerForCoder
.
saveLearnerForCoder(Mdl,'searcherModel')
saveLearnerForCoder
saves the model to the MATLAB binary file searcherModel.mat as a structure array in the current folder.
Define the entry-point functionmyknnsearch1
这需要查询数据作为输入参数。在the function, load the searcher object by usingloadLearnerForCoder
, and then pass the loaded model toknnsearch
.
typemyknnsearch1.m% Display contents of myknnsearch1.m file
function idx = myknnsearch1(Y) %#codegen Mdl = loadLearnerForCoder('searcherModel'); idx = knnsearch(Mdl,Y); end
Note:If you click the button located in the upper-right section of this page and open this example in MATLAB®, then MATLAB® opens the example folder. This folder includes the entry-point function files,myknnsearch1.m
,myknnsearch2.m
, andmyknnsearch3.m
.
Generate code formyknnsearch1
by usingcodegen
. Specify the data type and dimension of the input argument by usingcoder.typeof
so that the generated code accepts a variable-size array.
codegenmyknnsearch1-args{coder.typeof(Y,[Inf,4],[1,0])}
For a more detailed code generation example that usessaveLearnerForCoder
andloadLearnerForCoder
, seeCode Generation for Prediction of Machine Learning Model at Command Line. For more details about specifying variable-size arguments, seeSpecify Variable-Size Arguments for Code Generation.
Pass the query data (Y
) to verify thatmyknnsearch1
and the MEX file return the same indices.
myIdx1 = myknnsearch1(Y); myIdx1_mex = myknnsearch1_mex(Y);
ComparemyIdx1
andmyIdx1_mex
by usingisequal
.
verifyMEX1 = isequal(Idx,myIdx1,myIdx1_mex)
verifyMEX1 =logical1
isequal
returns logical 1 (true
) if all the inputs are equal. This comparison confirms thatmyknnsearch1
and the MEX file return the same results.
Generate Code with Constant Folded Model Object
Nearest neighbor searcher objects can be an input argument of a function you define for code generation. The-args
option ofcodegen
accept a compile-time constant searcher object.
Define the entry-point functionmyknnsearch2
that takes both an exhaustive searcher model and query data as input arguments instead of loading the model in the function.
typemyknnsearch2.m% Display contents of myknnsearch2.m file
function idx = myknnsearch2(Mdl,Y) %#codegen idx = knnsearch(Mdl,Y); end
To generate code that takes the model object as well as the query data, designate the model object as a compile-time constant by usingcoder.Constant
and include the constant folded model object in the-args
value ofcodegen
.
codegenmyknnsearch2-args{coder.Constant(Mdl),coder.typeof(Y,[Inf,4],[1,0])}
The code generation workflow with a constant folded model object follows general code generation workflow. For details, seeGeneral Code Generation Workflow.
Verify thatmyknnsearch2
and the MEX file return the same results.
myIdx2 = myknnsearch2(Mdl,Y); myIdx2_mex = myknnsearch2_mex(Mdl,Y); verifyMEX2 = isequal(Idx,myIdx2,myIdx2_mex)
verifyMEX2 =logical1
Generate Code with Name-Value Pair Arguments
Define the entry-point functionmyknnsearch3
that takes a model object, query data, and name-value pair arguments. You can allow for optional name-value arguments by specifyingvarargin
as an input argument. For details, seeCode Generation for Variable Length Argument Lists(MATLAB Coder).
typemyknnsearch3.m% Display contents of myknnsearch3.m file
function idx = myknnsearch3(Mdl,Y,varargin) %#codegen idx = knnsearch(Mdl,Y,varargin{:}); end
To generate code that allows a user-defined exponent for the Minkowski distance, include{coder.Constant('P'),0}
in the-args
value ofcodegen
. Usecoder.Constant
because the name of a name-value pair argument must be a compile-time constant.
codegenmyknnsearch3-args{coder.Constant(Mdl),coder.typeof(Y,[Inf,4],[1,0]),coder.Constant('P'),0}
Verify thatmyknnsearch3
and the MEX file return the same results.
newIdx = knnsearch(Mdl,Y,'P',2); myIdx3 = myknnsearch3(Mdl,Y,'P',2); myIdx3_mex = myknnsearch3_mex(Mdl,Y,'P',2); verifyMEX3 = isequal(newIdx,myIdx3,myIdx3_mex)
verifyMEX3 =logical1
See Also
ExhaustiveSearcher
|KDTreeSearcher
|codegen
|knnsearch
|loadLearnerForCoder
|rangesearch
|saveLearnerForCoder