Generating predictions from trained neural net in simulink
4 ビュー (過去 30 日間)
Background: I have trained a neural network in python using tensorflow and keras, and saved the model in the h5 format. I have succesfully been able to load that model into the matlab environment and make the same predictions using a modified version of the following script:
% Load model into Matlab workspace
loaded_model = importKerasNetwork('Python_Trained_NN.h5');
% Normalize input data
X1_norm = X1 / max(X1);
X2_norm = X2 / max(X2);
X3_norm = X3 / max(X3);
X4_norm = X4 / max(X4);
% Create Input Vector
inputs = [X1_norm, X2_norm, X3_norm, X4_norm];
% Create zeros for predictions with length equal to the length of the time series data I am trying to make predictions on
test_preds = zeros(length(time), 1);
% % Make predictions using inputs
for i = 1:length(time)
test_preds(i) = predict(loaded_model, inputs(i, :));
I know there is probably a faster way to do this, but I am limited to matlab 2020a for my application and am unable to upgrade. Also, this works for now which was a good starting point at least. Please feel free to suggest a better way to do this.
Is there any way I can use a matlab function block to get the same behavior in simulink? Again, I am using matlab 2020a, so I do not have access to the full deep learning toolbox library. Right now I am trying the following.
function y = fcn(X1, X2, X3, X4)
inputs = [X1, X2, X3, X4];
y = predict(loaded_model, inputs);
But I get an error saying that 'loaded_model' is an undefined variable even though it is saved in the matlab workspace as a SeriesNetwork object.
回答 (1 件)
David Willingham 2022 年 8 月 19 日
Great to hear your exploring workflows that include TensorFlow and MATLAB/Simulink.
There are more options for running the model in Simulink as of 21a. For example there is a stateful predict for LSTMs: Stateful Predict.