Simulink Model for Trained Regression Model Throws "Not Enough Input Arguments" Error

3 views (last 30 days)
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

Accepted Answer

Rohit
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)
ans = 239×1
-21.4872 21.2125 -7.7464 -6.5276 -0.5813 9.1135 8.4623 9.1444 56.4885 9.3798
function Ypred1 = GradientBoostModel1Predict(Xtest)%#codegen
Model1 = loadLearnerForCoder('GradientBoostModel1.mat');
Ypred1=predict(Model1,Xtest);
end
  2 Comments
Dhanushya M
Dhanushya M on 22 May 2023
Edited: Dhanushya M on 22 May 2023
Thank you for the response.
So the function is working, but not with input X, but with Xtest or Xtest(1:5,:) etc. The predicted data is too erraneous though. I will have to train the model in a different way i guess.
Dhanushya M
Dhanushya M on 22 May 2023
Edited: Dhanushya M on 22 May 2023
Giving input as X in commandline as
GradientBoostModel1Predict(X)
This also works. So the function seems to be working.

Sign in to comment.

More Answers (1)

Neeraja
Neeraja on 22 May 2023
Hi Dhanushya,
as @Rohit said, I too was able to see the predicted output for Xtest. So the function is working.
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.

Products


Release

R2023a

Community Treasure Hunt

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

Start Hunting!