Generating predictions from trained neural net in simulink
1 次查看(过去 30 天)
显示 更早的评论
Hello,
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, :));
end
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)
%#codegen
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.
0 个评论
回答(1 个)
David Willingham
2022-8-19
Hi Mike,
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.
4 个评论
David Willingham
2022-8-22
I have checked back with R2020b, and this example highlights how you can load your own network using the command in the MATLAB function block:
coder.loadDeepLearningNetwork
Note: this example is different in the current release as there are now dedicated blocks.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Predictive Maintenance Toolbox 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!