How to implement an LSTM model in simulink using the ROM app?

12 visualizzazioni (ultimi 30 giorni)
I have modeled a basic mass-spring-damper system in simulink, with 2 different input options, step and a chirp block for finding freqeuncey reponse.
I have followed the steps listed in the video tutorial for the Reduced Order Modeling add on "AI with Model-Based Design: Reduced Order Modeling." So far, I have been able to follow all the way up until the LSTM model has been trained (selecting the one with the lowest values for training and test loss.) At this point, the video says to save the training output, in which it then pops up in the matlab workspace. (I have it saved as trainingOutput_062325.) I also tried saving the output file (as lstm_model_062325) to my desktop and retreiving it from there.
I am now wanting to use this trained LSTM model in my simulink file, and compare it's output with the output from running the actual system. However, I am having difficulty with this. I was planning on using a Stateful Predict block and using the network as an input, but no matter how I save the files, it seems to give errors. I first tried to save the entire outputfie as a .mat file and use this in the statful prediction but that did not work. Here is my current attemp to save the dlnetwork found within the output file.
loadedModel = load('lstm_model_062325.mat');
lstmNet = loadedModel.trainingOutput_062325.Network;
save('lstm_model_062325.mat', 'lstmNet');
myDlnet = lstmNet;
save('my_dlnetwork.mat', 'myDlnet');
I am able to see that the file my_dlnetwork.mat is not empty and I can see that myDInet is a 1x1 dlnetwork variable in the matlab workspace, however, I get this error no matter how I try to implement it in the staful predect block.
"Error using second_order_script
Error in 'second_order/Stateful Predict': Failed to evaluate mask initialization commands.
Caused by:
Error using second_order_script
Network input data format string cannot be empty"
Once the LSTM model output is saved, what is the simplest way to use the model? The video turorial uses preloaded files and does not explain the method. Any help is appreciated, thanks.
  1 Commento
Alexandra
Alexandra il 23 Giu 2025
After continuing to mess with it, I am beginning to realize it might be an error on how im formatting my input. Any advice on how to format it to make it match this the deep learning data format? I attempted to create a matlab function block and convert my data to a dlarray but am still having trouble.
  • "S" — Spatial
  • "C" — Channel
  • "B" — Batch
  • "T" — Time
  • "U" — Unspecified

Accedi per commentare.

Risposte (1)

Deepak
Deepak il 27 Giu 2025
Hi Alexandra,
To use the trained LSTM model in Simulink with the Stateful Predict block, make sure you save only the “dlnetwork” object from your training output into a .mat file (e.g., my_dlnetwork.mat). Then, in the Stateful Predict block, select “Network from MAT-file” and load this file. One common error happens if the “Input data formats” field is left blank, this must be filled based on how your input is shaped. Eg: if your input is a time series (samples over time), you can use "TC" (T = time, C = features).
Also, ensure that the input signal is shaped correctly. For a single signal over time, the input can be a column vector (e.g., N×1), which matches the "TC" format. If needed, use a MATLAB Function block to convert the input to a “dlarray” with the correct labels like “dlarray(u, 'TC')”. Once these are set, Simulink should be able to run the LSTM and give predictions that you can compare with your actual system output.
please find attached relevant documentation for reference:
I hope this helps.
  1 Commento
Alexandra
Alexandra il 30 Giu 2025
Thank you for the response.
It seems that dlarray is not a compatible output type for a Matlab Function block. It also thows a similar error when I attempt to convert it using a script and load it in to the model with a From Workspace block.
Is there something else I could be missing?
Thank you again.

Accedi per commentare.

Categorie

Scopri di più su Sequence and Numeric Feature Data Workflows in Help Center e File Exchange

Prodotti


Release

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by