how to use the parameters of neural network in simulink?

Hello all, @Sam Chak @Stephen23 @Paul
I trained a neural network dpending on two inputs (T and p) to predect density.
Now I have the best parameters to predect density using this code:
density = model (parameters, p, T);
Plese note that I have a simulink model with the time-step is 0.001 s, the problem is how to use this parameters of the NN in the Simulink model.
input are: p and T
output is: density
time-step is 0.001
Best regards, Ahmad

댓글 수: 3

Hi Ahmad,
I'm afraid I can't help you with this. Good luck.
Hello @TAB
can please you solve this problem?

댓글을 달려면 로그인하십시오.

답변 (1개)

Sam Chak
Sam Chak 2024년 7월 7일

1 개 추천

If you want to implement a trained neural network in Simulink to predict the Desity based on two inputs (p, T), then use the Predict block.

댓글 수: 12

Ahmad Al-Issa
Ahmad Al-Issa 2024년 7월 7일
편집: Ahmad Al-Issa 2024년 7월 7일
Thank you @Sam Chak for your response
However, I know this block, but i donot know how to use this block.
just to know, I have the best parameters (weights, biases), so I have one mat file (parameters.mat) have all the parameters.
in matlab, I use this mat file using this script:
density = model (parameters, p, T);
So, what I want now is to use this file in simulink.
Can predict block do that? and how?
Also, there is only one input and I have two inputs, and i did not find the way to add another input.
best regards, Ahmad
If the "model" is the custom function of your trained neural network, and the syntax "density = model(parameters, p, T);" is how you successfully call the neural network in MATLAB to predict the density, then I would suggest trying to utilize the MATLAB Function block within your Simulink implementation and insert the code.
The code for this block attempts to produce exactly the same results as MATLAB. However, take note that some special functions are not supported.
yes you @Sam Chak are right and i am trying to do so like this:
function Den_Pred = model(p, T)
parameters = load('parameters.mat');
TP = [p; T];
numLayers = numel(fieldnames(parameters));
% First fully connect operation.
weights = parameters.fc1.Weights;
bias = parameters.fc1.Bias;
R = fullyconnect(TP,weights,bias);
for i=2:numLayers
name = "fc" + i;
R = sigmoid(R);
weights = parameters.(name).Weights;
bias = parameters.(name).Bias;
R = fullyconnect(R, weights, bias);
Den_Pred = R(1,:);
end
end
but the proble is the simulink can not read the parameters inside my parameters.mat
I see. The Predict block requires a dlnetwork object. Have you considered utilizing the fullyConnectedLayer() function to create the necessary dlnetwork object?
yes I trained my neural network considering fullyconnectedlayer.
Good then. Now put the trained dlnetwork object in the Predict block to see if it works.
Ahmad Al-Issa
Ahmad Al-Issa 2024년 7월 7일
편집: Ahmad Al-Issa 2024년 7월 7일
Hello @Sam Chak
I do not konw what you really want. So in the attached 3 files you will find
1- the parameters (weights and bias)
2- the model function
3- the matlabe code
you can just run the code easily.
what I want is how to use these parameters in simulink model.
can you help me with that?
Your code appears to be functioning correctly within MATLAB, and it is able to generate a plot for the density prediction. However, the "model" function you have created is not a dlnetwork class object, but rather a dlarray object. You have utilized the fullyconnect() function to construct the dlarray object.
Unfortunately, the Predict block in Simulink does not accept a dlarray object as its input.
I haven't tested this, but instead, I would suggest preloading the parameters into the MATLAB Workspace. The MATLAB Function block should then be able to access the parameters from the Workspace, without the need to load them directly within the MATLAB Function block itself.
Thank you for detailed answer.
The point is the MATLAB Function block can not access parameters.mat because it is saved as dlarray.
however, I succeed to pass one problem that is reading (weights and bias) by convert the struct from dlarry to single.
however, the code reach to fullyconnect Syntax and appears error because the undefined.
best regards, Ahmad
Hi Ahmad,
Thanks for your update. This error message is strange because the fullyconnect() function is supported in the MATLAB Function block according to the list below. Will need some time to figure this out.
Hello @Sam Chak
Thank you for assistent me.
The problems as follow:
1- the matlab function block does not work with dlarray
2- fullyconnect generate data in dlarray
3- Sigmoid function generate data in dlarray
The solution is to reproduce all (weights and bias) in double array
not using fullyconnect but writing code of matrix insteade.
not using Sigmoid function but writing the equation of it insteade.
Best regards, Ahmad

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Deep Learning Toolbox에 대해 자세히 알아보기

제품

릴리스

R2021b

태그

질문:

2024년 7월 6일

댓글:

2024년 7월 9일

Community Treasure Hunt

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

Start Hunting!

Translated by