Simulink Model for Trained Regression Model Throws "Not Enough Input Arguments" Error
2 views (last 30 days)
Show older comments
I trained a regression model using MATLAB scripts and followed the instructions in this link (https://in.mathworks.com/help/stats/predict-class-labels-using-matlab-function-block.html) to create a Simulink model that predicts output using the trained model. However, when I try to run the Simulink model, I get the following error message:
"Not enough input arguments.
Error in GradientBoostModel1Predict (line 3) Ypred1=predict(model1,Xtest);"
I have defined Xtest before calling the GradientBoostModel1Predict function and passed it to the function, so I'm not sure what the issue is. Can someone help me understand why I'm getting this error message and how I can fix it?
Here is my code:
% Load the engine_dataset
load engine_dataset
% Prepare the data
X = engineInputs.';
Y= engineTargets.';
% Split the data into training and testing sets
cv = cvpartition(size(X,1),'HoldOut',0.2);
Xtrain = X(cv.training,:);
Ytrain = Y(cv.training,:);
Xtest = X(cv.test,:);
Ytest = Y(cv.test,:);
% Train the Gradient Boosting model
model1 = fitrensemble(Xtrain,Ytrain(:,1),'Method','LSBoost','NumLearningCycles',500,'Learners',templateTree('MaxNumSplits',10));
model2 = fitrensemble(Xtrain,Ytrain(:,2),'Method','LSBoost','NumLearningCycles',500,'Learners',templateTree('MaxNumSplits',10));
saveLearnerForCoder(model1,'GradientBoostModel1');
saveLearnerForCoder(model2,'GradientBoostModel2');
Function File:
function Ypred1 = GradientBoostModel1Predict(Xtest)%#codegen
Model1 = loadLearnerForCoder('GradientBoostModel1.mat');
Ypred1=predict(Model1,Xtest);
end
0 Comments
Accepted Answer
Rohit
on 18 May 2023
Hi Dhanushya,
I understand that you want to use your trained neural network in Simulink using the MATLAB Function Block but are facing an error.
I was able to reproduce your error and I got the same error message if I don’t pass “Xtest” to the function when calling it. However, if I call “GradientBoostModel1Predict(Xtest)” after saving the learner, I am able to get the predictions. I have added the code below for your reference.
% Load the engine_dataset
load engine_dataset
% Prepare the data
X = engineInputs.';
Y= engineTargets.';
% Split the data into training and testing sets
cv = cvpartition(size(X,1),'HoldOut',0.2);
Xtrain = X(cv.training,:);
Ytrain = Y(cv.training,:);
Xtest = X(cv.test,:);
Ytest = Y(cv.test,:);
% Train the Gradient Boosting model
model1 = fitrensemble(Xtrain,Ytrain(:,1),'Method','LSBoost','NumLearningCycles',500,'Learners',templateTree('MaxNumSplits',10));
model2 = fitrensemble(Xtrain,Ytrain(:,2),'Method','LSBoost','NumLearningCycles',500,'Learners',templateTree('MaxNumSplits',10));
saveLearnerForCoder(model1,'GradientBoostModel1');
saveLearnerForCoder(model2,'GradientBoostModel2');
GradientBoostModel1Predict(Xtest)
function Ypred1 = GradientBoostModel1Predict(Xtest)%#codegen
Model1 = loadLearnerForCoder('GradientBoostModel1.mat');
Ypred1=predict(Model1,Xtest);
end
2 Comments
More Answers (1)
Neeraja
on 22 May 2023
Hi Dhanushya,
I wanted to add a suggestion. Instead of using MATLAB function blocks to create a simulink model that models the trained regression model, you can try using RegressionEnsemble Predict Block or RegressionTree Predict Block in Simulink to predict responses.
The link below shows an example of creating Simulink model for prediction using trained regression ensemble model.
The code you gave uses the example data set engine_dataset for ANN model training, which can predict both Y responses simultaneously. But for regression models, I believe a model can have several inputs but only a single output. So for engine_dataset, you may need two models each predicting one of the two responses.
Also you need to format the input before giving it to the model. See https://in.mathworks.com/help/simulink/ug/load-data-using-the-from-workspace-block.html.
0 Comments
See Also
Categories
Find more on Regression Tree Ensembles in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!