Export Models from Machine Learning Apps to Simulink
In this blog post, I am going to show you the most interactive way to create a Simulink model that includes a machine learning model by using the Classification Learner app. Simulating and testing machine learning models is becoming increasingly popular, but how do you integrate machine learning models into Simulink models?
The workflow shown here is very similar if you are using the Regression Learner app. The export functionality was introduced in MATLAB R2024a and you can learn more in the documentation topics Export Classification Model to Make Predictions in Simulink and Export Regression Model to Make Predictions in Simulink.
In the following sections, I ‘ll outline all the steps including data preparation, creating a machine learning model with the Classification Learner app, and finally creating a Simulink model from the app. Note that the Classification Learner and Regression Learner apps are not the only AI apps from which you can export to Simulink. You can also export to Simulink from the Deep Network Designer and Curve Fitter apps.
Prepare Data
We are going to start at the command line to prepare the data for training classifiers. Read the sample file CreditRating_Historical.dat into a table. The predictor data contains financial ratios and industry sector information for a list of corporate customers. The response variable contains credit ratings assigned by a rating agency.creditrating = readtable("CreditRating_Historical.dat");
Create Model with Classification Learner
Open the Classification Learner app at the command line. Alternatively, you can open the app by clicking the Apps tab, and then click the arrow at the right of the Apps section to open the apps gallery. In the Machine Learning and Deep Learning group, click Classification Learner.classificationLearnerIn the New Session from the Workspace dialog box, select the creditrating table from the Data Set Variable list. The app selects the response and predictor variables. The default response variable is Rating. Check that none of the predictors is categorical. At the moment, machine learning models trained with categorical predictors cannot be exported to Simulink from the app. The default validation option is 5-fold cross-validation, to protect against overfitting. In the Test section, click the check box to set aside a test data set. Specify 15 percent of the imported data as a test set. To accept the options and continue, click Start Session. Not all available classifiers in the app are supported for Simulink export. When I started reading the relevant documentation topic, I thought I would have to manually select which models to train given that my end goal was to export the best performing model to Simulink. Fortunately, the app designers anticipated my (and most likely other users’) frustration and provided an option to train only the models supported for Simulink export. To train all supported models, click All Simulink Supported in the Models section of the Learn tab and then click Train Selected. Then, click Train Selected in the Train tab. You can train models in parallel using Classification Learner if you have Parallel Computing Toolbox. Parallel training allows you to train multiple classifiers at once. Make sure that the Use Parallel button is selected.
Export to Simulink
To find the best classifier, sort the trained models based on the validation accuracy. In the Models pane, open the Sort by list and select Accuracy (Validation). In the Export section of the Learn tab, click Export Model and select Export Model to Simulink. It’s a one-click process. Maybe I got ahead of myself. You are one more click away from creating a Simulink model. In the Export Classification Model to Simulink dialog box, you can select to save the model and input data to the MATLAB workspace or the Simulink model workspace. Click Export. The app launches Simulink and creates a new Simulink model, which you can save in your current directory. The new variables (for example, trainedModel and inputData) appear in the MATLAB workspace. The From Workspace (Simulink) block is connected to a Predict block of the type corresponding to your exported classification model. In this case, the Simulink model is using the ClassificationNeuralNetwork Predict block. The To Workspace block outputs the predicted labels from the Predict block to a new variable named outputPredictions in the MATLAB workspace. You can double-click any of these blocks to change their settings. For an example, see Predict Class Labels Using ClassificationNeuralNetwork Predict Block. The Simulink model with an integrated machine learning model is ready to use. This has been the easiest way that I ‘ve ever used to create a Simulink model with a machine learning model. All the right blocks are already connected, and I didn’t have to search through Simulink libraries or documentation. To run the Simulink model, click Run on the Simulation tab. By simulating a machine learning in Simulink, you can test its integration into a larger system. That is, you can assess the machine learning behavior and system performance.
コメント
コメントを残すには、ここ をクリックして MathWorks アカウントにサインインするか新しい MathWorks アカウントを作成します。