Calling a machine learning model on simulink
6 次查看(过去 30 天)
显示 更早的评论
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?
0 个评论
回答(2 个)
Yash
2023-12-2
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!
Hamid Satarboroujeni
2023-12-14
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.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Code Generation 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!