I want to make a prediction from a trained neural network and have the prediction displayed on a display block

I am doing a school project using Neural networks to predict faults, I have already trained the neural network, but I am finding it difficult to use the trained model directly on simulink, using the predict blocks. I was able to use script to extract the data and make prediction and display on the workspace, but I want everything to run directly from simulink

Antworten (1)

Hi Olalekan,
I understand that you are facing problems in converting your neural network into a Simulink model.
I assume that you are using the latest dlnetwork object provided in MATLAB to train your Neural Network. To know more about how to use ‘dlnetwork’ please look at this documentation on how to train simple Neural Networks using 'dlnetwork.'
If you want to convert your dlnetwork to a Simulink block you can use the function given below provided by the Layers library in Simulink,
exportNetworkToSimulink(dlnetwork)
This will generate a Simulink block which you can further use in your Simulink model. To display the output of the network you can use a scope or a display block connected as output to this model.
Please look at the below documentation to know more regarding this,
Hope this helps.

7 Kommentare

Thanks so much @Krishna, I will give this a try and give my feedback also, I make use of Trainnet.
I will also go through the documentation to know about dlnetwork, thanks once again.
The output i got from the model is a 12 by 1 array , which i beleive is equivalent to the 12 classes i have/
i will provide the screenshot for the uput on a display module.
while following the example provided in the link, when i converted the data to .csv, it was showing some form of unrecognised characters, i used the .mat file to train the model.
i will provide the code i used for training the mode, and the screenshot for better understanding
% data preparation
load('Fault_Model_Data.mat');
labels_categorical = categorical(labels); % Ensure labels are categorical
data_table = array2table(data, 'VariableNames', {'CoefA', 'CoefB', 'CoefC', 'CoefG'});
data_table.Labels = labels_categorical;
% Split data into training and validation sets
train_ratio = 0.8;
cv = cvpartition(height(data_table), 'HoldOut', 1 - train_ratio);
train_data = data_table(training(cv), :);
val_data = data_table(test(cv), :);
% Extract features and labels for training
XTrain = table2array(train_data(:, 1:4)); % Features
YTrain = train_data.Labels; % Labels
% Extract features and labels for validation
XValidation = table2array(val_data(:, 1:4));
YValidation = val_data.Labels;
% Normalize features
mu = mean(XTrain);
sigma = std(XTrain);
%XTrain = (XTrain - mu) ./ sigma;
%XValidation = (XValidation - mu) ./ sigma;
% Define the deep learning network architecture
layers = [...
featureInputLayer(size(XTrain, 2), 'Normalization', 'none', 'Name', 'Input')
fullyConnectedLayer(128, 'Name', 'FC1')
reluLayer('Name', 'ReLU1')
dropoutLayer(0.5, 'Name', 'Dropout1')
fullyConnectedLayer(64, 'Name', 'FC2')
reluLayer('Name', 'ReLU2')
fullyConnectedLayer(numel(categories(YTrain)), 'Name', 'Output')
softmaxLayer('Name', 'Softmax')
classificationLayer('Name', 'Classification')];
% Define training options
options = trainingOptions('adam', ...
'MaxEpochs', 50, ...
'MiniBatchSize', 32, ...
'InitialLearnRate', 0.001, ...
'ValidationData', {XValidation, YValidation}, ...
'ValidationFrequency', 10, ...
'Plots', 'training-progress', ...
'Verbose', true);
% Train the neural network
trainedNet = trainNetwork(XTrain, YTrain, layers, options);
% Save the trained network
save('TrainedFaultClassifier.mat', 'trainedNet');
I will provide few screenshot i took for some fault class.
C-G fault class
this is foe NORMAL class
using this function
exportNetworkToSimulink(trainedNet);
Unrecognized function or variable 'trainedNet'.
retuened an error exportNetworkToSimulink
exportNetworkToSimulink was introduced in R2024b, and is part of the Deep Learning toolbox.
We can assume that you are running R2024b as you did not fill out the Release category when creating the question, and surely you would have filled out the Release if you are using an older release of MATLAB.
I am sorry for not specifying the release version, I am running MATLAB 2021a
In that case, exportNetworkToSimulink did not exist yet.
As far as I know your only option is to upgrade MATLAB versions.
Thank you @Walter Roberson, I will upgrade to the recent version and try it out.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Deep Learning Toolbox finden Sie in Hilfe-Center und File Exchange

Produkte

Version

R2021a

Gefragt:

am 27 Jan. 2025

Kommentiert:

am 30 Jan. 2025

Community Treasure Hunt

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

Start Hunting!

Translated by