Calling a machine learning model on simulink

15 visualizaciones (últimos 30 días)
Meriem zanoun
Meriem zanoun el 30 de Nov. de 2023
Respondida: Hamid Satarboroujeni el 14 de Dic. de 2023

Hello everyone, I'm using Simulink for running simulations, and I'd like to call a trained machine learning model (in my case, a TreeBagger) in MATLAB to make predictions in Simulink. However, I'm having some trouble with it. Has anyone already worked with this?

Respuestas (2)

Yash
Yash el 2 de Dic. de 2023
Hi Meriem,
I understand that you want to call a machine learning model in Simulink that you trained in MATLAB. To do this you can export your trained model to a MATLAB function and then call that function in Simulink using a MATLAB Function block. To export your trained model to a MATLAB function you can use the codegen function. This function generates C/C++ code from MATLAB code, including trained models. Here's an example code snippet:
>> net = trainNetwork(XTrain,YTrain,layers,options); % trains a neural network
>> codegen net -args {XTest} -config:dll % generates a DLL file containing the network
This code trains a neural network using trainNetwork, and then generates a DLL file containing the network using codegen. The -args option specifies the input arguments for the generated function, and the -config:dll option specifies that the output should be a DLL file.
Please note that here are some limitations to what types of models can be exported using codegen. Not all MATLAB functions and toolboxes are supported, and some models may require additional configuration or customization to be exported successfully. Additionally, some models may not be suitable for deployment on certain hardware platforms. To determine whether a specific model can be exported using codegen, you can consult the documentation for the function or toolbox that was used to create the model here: https://in.mathworks.com/help/coder/ref/codegen.html
To implement MATLAB functions in Simulink, you can use the MATLAB Function Block. To know how to use it, you can read its documentation here: https://in.mathworks.com/help/simulink/ug/what-is-a-matlab-function-block.html
Hope this helps!
  1 comentario
Meriem zanoun
Meriem zanoun el 6 de Dic. de 2023
Hi @Yash,
Thank your for your response !
I've figured out a way to achieve this using two functions that, in my opinion(understanding), perform the same task. First, I saved the model using saveLearnerForCoder , and then I loaded it into the Matlab function block using loadLearnerForCoder. Finally, I called the loaded model using the predict function.
Please let me know if its accurate.
Best regards !

Iniciar sesión para comentar.


Hamid Satarboroujeni
Hamid Satarboroujeni el 14 de Dic. de 2023
Hi Meriem,
If you have a trained machine learning model in MATLAB and want to use it for predictions in Simulink, you can use one of the Predict blocks in the Statistics and Machine Learning Toolbox. You can see a list of all native Simulink blocks for prediction here:
In the case of a tree bagger model, you can use the ClassificationEnsemble Predict or the RegressionEnsemble Predict blocks:
For these blocks, you do not need to use the saveLearnerForCoder function because the blocks can work with the trained model directly and do not need any conversions.
There is an "older" approach using a user-created MATLAB Function block as pointed out above. Compared to the native Simulink blocks, the MATLAB Function block needs to be created manually and has certain limitations, such as variable-size signals and data type support. The blocks support all data types, including single precision and fixed-point.
The answer by Yash above is for deep learning models and cannot be used for a TreeBagger model, which is under the Statistics and Machine Learning Toolbox.
I hope this answers the question.

Categorías

Más información sobre Code Generation en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by