Clear Filters
Clear Filters

How to specify toolbox for function call

11 views (last 30 days)
Patrick Gavigan
Patrick Gavigan on 21 Mar 2018
Commented: Steven Lord on 2 Nov 2023
In a scenario where Matlab has functions with the same name in multiple toolboxes, how do I specify the specific toolbox I want it to use?
Here are the details of my specific problem: I have implemented an LSTM Neural Network and am using it to perform classification. The neural network toolbox includes a classify() function which I am trying to use. Unfortunately, the statistics and machine learning toolbox also has a classify() function, but which takes different arguments. I have both of these toolboxes installed. As a result, I am getting the following error:
K>> predictedY = classify(lstmModel, X);
Error using classify (line 123)
Requires at least three arguments.
In the above snippet, lstmModel is a trained neural network I have loaded from a mat file and X is the data I wish to classify. The neural network toolbox version of classify() does specify that it should work with these two arguments.
From what I can tell, Matalb is calling the classify function from the wrong toolbox. How do I specify the specific toolbox I want it to use?
  2 Comments
Greg
Greg on 22 Mar 2018
Typically, TheMathWorks is pretty good about shadowing, and it knows how to call the correct one based on the class of the input variable. The documentation for classify in Neural Network says input 1 must be a SeriesNetwork or DAGNetwork object, are you certain your lstmModel input is of that class?
Patrick Gavigan
Patrick Gavigan on 22 Mar 2018
I can confirm that the lstmModel is the correct type of class. What's interesting about this problem is that when I keep the train and classify functions in the same m file (same context) it works fine. The issue arises when I pass the model as a parameter to another function and then try to classify in that function. It is also present when I save the model to a mat file and then load it up and try to classify with it later.

Sign in to comment.

Answers (1)

Maksym Tymchenko
Maksym Tymchenko on 2 Nov 2023
This is a very good question that can be generalized to many similar situations.
First of all, you can check all the versions of the "classify" function that can be found on your MATLAB path by executing the following command:
which -all classify
/MATLAB/toolbox/stats/stats/classify.m /MATLAB/toolbox/econ/econ/@dtmc/classify.m % dtmc method /MATLAB/toolbox/nnet/cnn/@DAGNetwork/classify.m % DAGNetwork method
As you can see there are several options.
You can check the syntax for each option by using "help", for example:
help("/MATLAB/toolbox/nnet/cnn/@DAGNetwork/classify.m")
classify Classify data with the network [labels, scores] = classify(net, X) will classify the data X using the network net. labels will be an N-by-1 categorical vector where N is the number of observations, and scores will be an N-by-K matrix where K is the number of output classes. If the output of the network is a sequence, labels and scores will be N-by-1 cell arrays. Each element of labels will be a 1-by-S categorical array and each element of scores will be a K-by-S numeric array, where S is the sequence length of the output. The format of X will depend on the input layer for the network. The format of X will depend on the input layer for the network. For a network with an image input layer, X must be: - A single image. - A 4-D array of images, where the first three dimensions index the height, width and channels of an image, and the fourth dimension indexes the individual images. - A 5-D array of images, where the first four dimensions index the height, width, depth and channels of an image, and the fifth dimension indexes the individual images. - A datastore, where the output of the datastore read function is one image, a cell array of images, or a table whose first column contains images. - A table, where the first column contains either image paths or images. For a network with a sequence input layer, X must be: - A numeric array representing a single sequence. - A cell array of sequences. - A datastore, where the output of the datastore read function is a cell array or a table containing sequences in the first column. Each sequence must be either: - A vector sequence, specified by a C-by-S matrix, where C is the number of features and S is the number of time steps. - A 2-D image sequence, specified by an H-by-W-by-C-by-S array, where H-by-W-by-C is the 2-D image size and S is the number of time steps. - A 3-D image sequence, specified by an H-by-W-by-D-by-C-by-S array, where H-by-W-by-D-by-C is the 3-D image size and S is the number of time steps. For a network with a feature input layer, X must be: - A single observation specified as a row vector. - A 2-D array of observations, where the first dimension indexes the observations and the second dimension indexes the features. - A datastore, where the output of the datastore read function is a cell array or a table containing feature vectors in the first column. - A table, where each column represents a feature of the dataset. For a network with a point cloud input layer, X must be: - A single observation of point cloud data specified as a 2-D or 3-D numeric array. - A 3-D or 4-D numeric array of point clouds, where the second to last dimension indexes the channels and the last dimension indexes the individual observations. - A datastore, where the output of the datastore read function is a cell array containing point cloud data as a numeric array. [labels, scores] = classify(net, cds) classifies data using a multiple input network with the combined datastore cds. The combined datastore must return a 1-by-N cell array, where N is the number of network inputs. [labels, scores] = classify(net, X1, ..., XN) classifies data using a multi input network with N inputs, where the input data is X1, ..., XN. [labels, scores] = classify(__, 'PARAM1', VAL1, ...) specifies optional name-value pairs described below: 'MiniBatchSize' - The size of the mini-batches for computing predictions. Larger mini-batch sizes lead to faster predictions, at the cost of more memory. The default is 128. 'ExecutionEnvironment' - The execution environment for the network. This determines what hardware resources will be used to run the network. - 'auto' - Use a GPU if it is available, otherwise use the CPU. - 'gpu' - Use the GPU. To use a GPU, you must have Parallel Computing Toolbox(TM) and a supported GPU device. If a suitable GPU is not available, classify returns an error message. - 'cpu' - Use the CPU. - 'multi-gpu' - Use multiple GPUs on one machine, using a local parallel pool. If no pool is open, one is opened with one worker per supported GPU device. - 'parallel' - Use a compute cluster. If no pool is open, one is opened using the default cluster profile. If the pool has access to GPUs then they will be used and excess workers will be idle. If the pool has no GPUs then classification will take place on all cluster CPUs. The default is 'auto'. 'Acceleration' - Optimizations that can improve performance at the expense of some overhead on the first call and possible additional memory usage. - 'auto' - Automatically select optimizations suitable for the input network and environment. - 'mex' - (GPU only) Generate and execute a MEX function. Subsequent calls with the same network and options use the MEX function. This option is not supported when 'ExecutionEnvironment' value is either 'parallel' or 'multi-gpu'. - 'none' - Disable all acceleration. The default is 'auto'. 'SequenceLength' - Strategy to determine the length of the sequences used per mini-batch. Options are: - 'longest' to pad all sequences in a batch to the length of the longest sequence. - 'shortest' to truncate all sequences in a batch to the length of the shortest sequence. - Positive integer - For each mini-batch, pad the sequences to the nearest multiple of the specified length that is greater than the longest sequence length in the mini-batch, and then split the sequences into smaller sequences of the specified length. If splitting occurs, then the software creates extra mini-batches. The default is 'longest'. 'SequencePaddingValue' - Scalar value used to pad sequences where necessary. The default is 0. 'SequencePaddingDirection' - Direction of padding or truncation, specified as: - 'right' - Pad or truncate sequences on the right. - 'left' - Pad or truncate sequences on the left. The default is 'right'. See also DAGNetwork/predict, DAGNetwork/activations. Documentation for DAGNetwork/classify doc DAGNetwork/classify
MATLAB is smart enough to call the correct version of classify by looking at the class of the input.
For example, if the variable "net" is of class "DAGNetwork", then the DAGNetwork method version of classify should be called:
net = shufflenet;
class(net)
ans = 'DAGNetwork'
inputSize = net.Layers(1).InputSize;
X = 100*ones(inputSize);
classify(net, X)
ans = categorical
letter opener
You can see that the DAGNetwork version of classify has been called successfully.
Alternatively, because the DAGNetwork version of classify is a class method, you can also enforce it to be called by using the method syntax:
net.classify(X)
ans = categorical
letter opener
If after following these steps you still encounter the same error, this is probably because the DAGNetwork version of classify is not on your MATLAB path. To fix this, make sure that the Deep Learning Toolbox is installed and try executing "rehash toolboxcache".
  1 Comment
Steven Lord
Steven Lord on 2 Nov 2023
In addition, if you call the which function with a function call as the "name" to display information about, MATLAB will take the variables you pass into that call into account when determining which overload of the function is called.
For example, a few classes define their own version of the sine function sin.
which -all sin
built-in (/MATLAB/toolbox/matlab/elfun/@double/sin) % double method built-in (/MATLAB/toolbox/matlab/elfun/@single/sin) % single method /MATLAB/toolbox/matlab/datatypes/tabular/@tabular/sin.m % tabular method sin is a built-in method % matlab.unittest.internal.fixtures.FolderScope method sin is a built-in method % matlab.automation.Verbosity method built-in % gpuArray method /MATLAB/toolbox/coder/half/@half/sin.p % half method /MATLAB/toolbox/nnet/deep/@dlarray/sin.m % dlarray method /MATLAB/toolbox/parallel/gpu/@gpuArray/sin.m % gpuArray method /MATLAB/toolbox/parallel/parallel/@codistributed/sin.m % codistributed method /MATLAB/toolbox/symbolic/symbolic/@sym/sin.m % sym method
If I create a sym object I can confirm that when I call sin on that object the method in toolbox/symbolic/symbolic/@sym is called.
two = sym(2);
which -all sin(two)
/MATLAB/toolbox/symbolic/symbolic/@sym/sin.m % sym method
But if I try to ask the same thing for a class that doesn't define sin it will tell me that fact.
f = figure;
class(f) % This doesn't appear in the first list above
ans = 'matlab.ui.Figure'
which -all sin(f)
'sin(f)' not found.

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!