how to import parameters (weights and bias) in simulink

6 views (last 30 days)
Hello experts,
I have parameters (weights and bias) saved in mat file.
and I use matlab function block in my simulink model to use this parameters.
the problem that the simulink can not read the parameters and give me this error:
How can I solve this issue??
  2 Comments
Umar
Umar on 7 Jul 2024
Edited: Walter Roberson on 8 Jul 2024
Hi Ahmad,
To resolve this issue and enable Simulink to read the parameters correctly, you need to ensure that the parameters are loaded and passed in a way that provides clear information about their sizes and types. Here's a step-by-step solution to fix this problem:
Load Parameters Correctly: Make sure that the parameters are loaded correctly from the .mat file within the MATLAB function block. Use the load function to load the .mat file and extract the necessary parameters.
% Load parameters from the .mat file
loadedData = load('parameters.mat');
weights = loadedData.weights;
bias = loadedData.bias;
Specify Output Sizes and Types: Explicitly define the sizes and types of the outputs within the MATLAB function block to provide Simulink with the necessary information for data type propagation
function [output1, output2] = myFunction(input)
% Process input using loaded parameters
% Ensure output sizes and types are explicitly defined
output1 = weights * input + bias;
output2 = someOtherOperation(input);
end
Update Simulink Model: After making the necessary changes in the MATLAB function block, update your Simulink model to reflect the modifications. Ensure that the outputs are correctly connected and that the block parameters match the defined sizes and types.
Run Simulation: Run a simulation of the updated model to verify that Simulink can now read the parameters correctly without encountering errors related to data type propagation.
Debugging: Use debugging tools within Simulink to trace the source of the errors. This can help identify where the issue lies and provide insights into potential solutions.
Update MATLAB/Simulink: Make sure that you are using the latest version of MATLAB and Simulink, as newer versions often include bug fixes and improvements that could address compatibility issues.
Consult documentation: Refer to the official documentation for MATLAB and Simulink, as well as any relevant resources or forums, to see if there are known solutions or best practices for handling similar problems.
By following these steps and ensuring that your parameters, data types, and model settings are correctly configured, you should be able to resolve the issue of Simulink not reading the parameters from your mat file successfully. If you encounter any further difficulties, feel free to provide additional details for more targeted assistance.
Ahmad Al-Issa
Ahmad Al-Issa on 7 Jul 2024
Hello @Umar
Thank you for detailed answer.
The point is the simulink model can not access parameters.mat because it is saved as dlarray.
however, you can find in the attached files:
1- the parameters (weights and bias)
2- the model function
3- the matlabe code
you can just run the code very easily.
what I want is how to use these parameters in simulink model using matlab function block.
can you help me with that?
best regards, Ahmad

Sign in to comment.

Accepted Answer

Umar
Umar on 7 Jul 2024
Hi Ahmad,
Now it makes sense. You have the necessary files containing the parameters, model function, and MATLAB code. To address this problem and use the parameters in your Simulink model using a MATLAB Function block, you can follow these steps:
1. Load the parameters from the parameters.mat file in your MATLAB script using the load function. This will give you access to the weights and biases saved in the file.
2. Convert the dlarray data to a format that can be used within the MATLAB Function block in Simulink. You may need to reshape or convert the data to a suitable format depending on your specific requirements.
3. Create a MATLAB Function block in your Simulink model and input the necessary code to use the loaded parameters within this block. This will allow you to incorporate the parameters into your simulation.
4. Connect the MATLAB Function block to other blocks in your Simulink model as needed to utilize the parameters in your simulation.
By following these steps, you should be able to successfully use the parameters saved as dlarray in your Simulink model using a MATLAB Function block.
  1 Comment
Ahmad Al-Issa
Ahmad Al-Issa on 8 Jul 2024
Thank you all 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

Sign in to comment.

More Answers (1)

Paul
Paul on 7 Jul 2024
Hi Ahmad,
To bring fc1, fc2, fc3 and fc4 into the Malab function block as constant parameters, start from Configure MATLAB Function Block Parameter Variables. I'm pretty sure, but not 100% certain, that such parameters can be structs. There may be other issues depending on the data types that are constained within the fields of fc1, etc.
If you get past that point, the next issule will be whether or not that call to fullyconnect works. It does support code generation, so that should be ok. sigmoid should be o.k. as well.
Next, I think Coder does support string scalars, so that should be o.k.
However, I'm not sure that coder will support dynamic field names.
Relevant doc page for Coder support.
If dynamic field names are not supported, you can try making a simple m-function file on your path that takes parameters and name as inputs and returns weights and bias as outputs. Declare that function with coder.extrinsic (search the doc) at the top of the model function.
Finally, I suspect that coder will have problems using the variable name R in three different contexts if any of those changes the type and/or size of R. If that's a problem, seems like they can be renamed. Does is the type/size of R the same as output from both calls to fullyconnect(). Does the type/size of R change on each call to sigmoid()?
Finally, if haven't done already, check the Deep Learning Toolbox to see if already has Simulink block (or blocks) that does what's needed.
  7 Comments
Paul
Paul on 8 Jul 2024
I'm not sure what I'm right about ....
Looks like you have a working approach, but I'll offer one addtional possibility to explore that may be of interest if you want to use the same function in Simulink and in Matlab.
In Simulink, the Matlab function block would look like this, assuming that there are no dlarray types in the parameters struct
function Den_Pred = slModel(T,P,parameters) % parameters base on doc page Configure MATLAB Function Block Parameter Variables.
coder.extrinsic('model')
Den_Pred = model(t,p,parameters)
end
Then, the model.m file would look something like
function Den_Pred = model(T,P,parameters)
% convert inputs in parameters to local dlarray variables if the parameters
% aren't dlarray
% execute the algorithm in the usual way with dlarray using sigmoid,
% fullyconnect, etc.
% convert Den_Pred back to numeric for return to Simulink
end
Ahmad Al-Issa
Ahmad Al-Issa on 8 Jul 2024
you are right about this (Please confirm that the data type of the output of model() should be numeric (like double or single) for use downstream in your simulink model?)
I check the type of all data and then unified them to be double.
thank you for your suggestions.

Sign in to comment.

Categories

Find more on Simulink Functions in Help Center and File Exchange

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!