How do I solve "pyrunfile is not suppoted" error in MATLAB Function ?

I got these errors in MATLAB Function. I tried everything but I couldn't figure out how to solve. I'm not sure.
Could you give me any advice? I'd be happy if you could tell me why this happens and where I'm missing.
Also, I'm now executing this model on MATLAB Online.
Error1
The function 'pyrunfile' is not supported in code generation.
Function 'MATLAB Function' (#24.95.220), line 2, column 13:.
pyrunfile(scikit_learn_model.py, output_vars,in1=input1,in2=input2,in3=input3")
Error2
Terminal width or dimension error.
'Output terminal 1' in 'MATLAB Function/input7' is a 1-dimensional vector with 1 element
Here is my .slx model.
Also, Here is my MATLAB Function code.
function [trq,brake] = RunPython(input1,input2,input3,input4,input5,input6,input7)
outputs=pyrunfile("scikit_learn_model.py","output_vars",in1=input1,in2=input2,in3=input3,in4=input4,in5=input5,in6=input6,in7=input7);
trq=outputs(1);
brake=outputs(2);
end
scikit_learn_model.py
import pickle
with open('surrogate_model.pkl', 'rb') as f:
model = pickle.load(f)
inputs = [in1,in2,in3,in4,in5,in6,in7]
output1, output2 = model.predict(inputs)
output_vars=[output1,output2]

42 Comments

Can you attach the .slx model please? A screenshot is difficult to run
pyrunfile has no Extended Capabilities section in its documentation, so it is not certified for code generation.
If you are willing to turn rapid acceleration off, then maybe you can get around this by using coder.extrinsic
Sorry. I've attached .slx model here. (matlab_func_howto_ex.slx)
I suppose there is not a problem with my .pkl file....
I wonder if I'm missing MATLAB Function code or python code.
That means I need to use coder.extrinsic instead of pyrunfile to execute my .slx model?
If it's true, It'd be nice if you could give me what the code is.
This error is something to do with using MATLAB Online, isn't it?

Hi @翼 ,

You mentioned, “I got these errors in MATLAB Function. I tried everything but I couldn't figure out how to solve. I'm not sure. Could you give me any advice? I'd be happy if you could tell me why this happens and where I'm missing. Also, I'm now executing this model on MATLAB Online.

Error1

The function 'pyrunfile' is not supported in code generation. Function 'MATLAB Function' (#24.95.220), line 2, column 13:. “pyrunfile(“scikit_learn_model.py”, “output_vars”,in1=input1,in2=input2,in3=input3")

Error2

Terminal width or dimension error. 'Output terminal 1' in 'MATLAB Function/input7' is a 1-dimensional vector with 1 element”

Please see my response to your comments below.

Let me address your query regarding, “Error 1: pyrunfile Not Supported for Code Generation”

The error you are encountering with pyrunfile arises because this function is not compatible with MATLAB's code generation capabilities. In scenarios where code generation is required (like when using Simulink), you should replace calls to pyrunfile with a mechanism that allows MATLAB to call Python functions without generating code for them. So, in your comments, you mentioned,

That means I need to use coder.extrinsic instead of pyrunfile to execute my .slx model? If it's true, It'd be nice if you could give me what the code is.This error is something to do with using MATLAB Online, isn't it?

Yes, you have to utilize coder.extrinsic. Here’s how you can modify your MATLAB Function:

function [trq, brake] = RunPython(input1, input2, input3, input4, 
input5, input6,input7)
coder.extrinsic('pyrunfile'); % Declare pyrunfile as extrinsic
outputs = pyrunfile("scikit_learn_model.py", "output_vars", ...
                       pyName1=input1, pyName2=input2, pyName3=input3, 
                       ...
                       pyName4=input4, pyName5=input5, pyName6=input6,   
                       pyName7=input7);
    trq = outputs(1);
    brake = outputs(2);
  end

So, in this code, the coder.extrinsic (pyrunfile) line tells the code generator to skip generating code for pyrunfile, allowing it to run during simulation instead. Make sure that you pass the inputs correctly as keyword arguments.

Now, let me address your query regarding “Error 2: Terminal Width or Dimension Error”

This error suggests that one of your output terminals in Simulink expects a different dimension than what it is receiving. Specifically, it seems that the output from your MATLAB Function may not match what Simulink expects.

Make sure that trq and brake have the expected dimensions. If they are supposed to be scalars and your Python model returns vectors or arrays, this mismatch will trigger an error. So, what you need to do is reshape or extract the specific values from your output variable. For example:

trq = double(outputs(1)); % Ensure output is a double scalar
brake = double(outputs(2)); % Ensure output is a double scalar

Here are some additions insights that will help you further. If possible, test your MATLAB function locally outside of Simulink to make sure it runs without errors before integrating it back into your Simulink model. Utilize MATLAB's debugging tools (disp, breakpoints) to inspect variable dimensions at runtime. This will help identify mismatches before they become runtime errors in Simulink.

By following these recommendations and making the necessary adjustments in your code and model configurations, you should be able to resolve the errors you are encountering. If issues persist, consider consulting MATLAB documentation or forums by clicking links below for further assistance tailored to your specific use case.

https://www.mathworks.com/help/simulink/ug/overview-of-integrating-python-code-with-simulink.html

https://www.mathworks.com/help/matlab/ref/pyrunfile.html

https://www.mathworks.com/help/matlab/matlab_external/ways-to-call-python-from-matlab.html

Please let us know if you need further assistance or help.

Thank you for your advice!
I have made this change based on your response, but it didn't work.... I wonder where I am missing...
MATLAB Function code
function [trq,brake] = RunPython(input1,input2,input3,input4,input5,input6,input7)
coder.extrinsic("pyrunfile");
outputs=pyrunfile("scikit_learn_model.py","output_vars",pyName1=input1,pyName2=input2,pyName3=input3,pyName4=input4,pyName5=input5,pyName6=input6,pyName7=input7);
trq=double(outputs(1));
brake=double(outputs(2));
end
Python code(scikit_learn_model.py)
import pickle
with open('surrogate_model.pkl', 'rb') as f:
model = pickle.load(f)
inputs = [in1,in2,in3,in4,in5,in6,in7]
# output1, output2 = model.predict(inputs)
# output_vars=[output1,output2]
output_vars=[100,200] # for test
I got these errors in this case.
Code generation does not support mxArray output from this function in this context. Use a known type to initialize the output variable "trq".
Code generation does not support mxArray output from this function in this context. Use a known type to initialize the output variable "brake".
Due to an error in the main unit or due to limitations of the underlying analysis,
Simulink cannot determine the size or type, or both, of the output of that block.
This error may be inaccurate.
Please correct the error indicated or explicitly specify the size, type,
or both for all block outputs.
Terminal width or dimension error.
'Output terminal 1' in 'MATLAB Function/input7' is a 1-dimensional vector with 1 element
Why is this happening? I think the MATLAB function and python code should be fine....
I'd like your expertise. Could you possibly help?

Hi @翼,

After doing analysis of your code and errors, it indicates that the outputs from the Python script are being treated as mxArray, which is not supported in the context of code generation. Specifically, the error messages suggest that MATLAB requires explicit definitions for the output variables. Additionally, the Python code does not properly handle the input variables, as they are referenced without being defined. So, to resolve these issues, you need to make sure that the output variables in the MATLAB function are explicitly defined with known types. Here’s how you can modify the MATLAB function:

function [trq, brake] = RunPython(input1, input2, input3, input4, 
input5, input6,input7)
  coder.extrinsic("pyrunfile");
    % Ensure outputs are initialized with known types
    trq = 0; % Initialize as a double
    brake = 0; % Initialize as a double
    outputs = pyrunfile("scikit_learn_model.py", "output_vars", ...
                        pyName1=input1, pyName2=input2, ...
                        pyName3=input3, pyName4=input4, ...
                        pyName5=input5, pyName6=input6, ...
                        pyName7=input7);
    % Convert outputs to double
    trq = double(outputs(1));
    brake = double(outputs(2));
  end

In the Python code, make sure that the input variables are defined correctly. For example:

import pickle
# Load the model
with open('surrogate_model.pkl', 'rb') as f:
  model = pickle.load(f)
# Define inputs from MATLAB
inputs = [in1, in2, in3, in4, in5, in6, in7] 
# Make predictions
output1, output2 = model.predict([inputs])  # Ensure inputs are passed   
correctly
output_vars = [output1, output2]

Please let me know if this helps resolve your issues.

Hi, @Umar
Appreciated!
I changed my code as you said and I executed my model on MATLAB Online.
But ,on this code here,
outputs=pyrunfile("scikit_learn_model.py","output_vars", ...
pyName1=input1,pyName2=input2,pyName3=input3,pyName4=input4, ...
pyName5=input5,pyName6=input6,pyName7=input7);
I got the following error
Python Error: ModuleNotFoundError: No module named 'sklearn'
Is this because I'm executing it on MATLAB Online not on local?
or
Is this because .pkl file is created by scikit-learn?
1: Install scikit-learn with pip install on my local.
2: Execute my model again.
I wonder if this is the steps I need to do....
Does this look familar to you?
Best,

Hi @翼 ,

You mentioned, _I changed my code as you said and I executed my model on MATLAB Online.But ,on this code here,

   outputs=pyrunfile("scikit_learn_model.py","output_vars", ...
    pyName1=input1,pyName2=input2,pyName3=input3,pyName4=input4, ...
    pyName5=input5,pyName6=input6,pyName7=input7);

I got the following error

Python Error: ModuleNotFoundError: No module named 'sklearn'

Is this because I'm executing it on MATLAB Online not on local?

or

Is this because .pkl file is created by scikit-learn?_

Please see my response to your comments below.

The error message you received, ModuleNotFoundError: No module named sklearn, typically indicates that the Python environment in which your code is running does not have the scikit-learn library installed. This can happen for several reasons:

Execution Environment: MATLAB Online may not have access to all libraries available in your local Python environment. Specifically, it often has a limited set of pre-installed libraries and may not include scikit-learn.

Library Installation: If you are running the code locally and it works there, but fails on MATLAB Online, it's likely that scikit-learn is not available in the MATLAB Online Python environment.

I will suggest following potential solutions to resolve the errors you encountered.

Check Installed Libraries: In MATLAB Online, you can check which Python libraries are available by running:

py.list();

This command will display a list of modules that can be imported.

Use Local Environment: If you need specific libraries like scikit-learn, consider running your model in a local installation of MATLAB where you can control the Python environment better.

MATLAB Online Limitations: If you must use MATLAB Online, check if MathWorks provides a method to request additional packages or consider using a different service that allows more extensive Python support.

Now, let me address your query regarding the Pickle File, the .pkl file format is used for serializing Python objects, and it doesn't inherently relate to your error unless you're trying to load a model saved in this format without ensuring that the appropriate libraries are available in your current environment. If you want to load a model from a .pkl file, make sure that pickle is available (which should be included by default). Load it correctly using:

pickle = py.importlib.import_module('pickle');
fh = py.open('model.pkl', 'rb');
model = pickle.load(fh);
fh.close();

However, ensure that any dependencies required by your model (like scikit-learn) are also present. I would also refer to you to read more about reading pickleball file in matlab by clicking the link below.

Run pickle file in matlab

Now, If you are working locally and want to ensure you have scikit-learn, you can install it via pip:

   pip install scikit-learn

Please let me know if this helps resolve your problems.

Thank you.
I'm trying to do it locally.
I use Windows environment. My python version is 3.12. MATLAB version is R2024b(latest).
I installed pyenv and poetry locally.
Also I installed python 3.10.5 and 3.11.0b4 (I checked this from "pyenv versions")
I set python 3.11.0b4 as local version at a local folder. (.python-version file created )
then , in the folder, I created a poetry virtual environment.I've already installed scikit-learn in that virtual environment.
I want to use my MATLAB Function and my python code like above in my current MATLAB R 2024b.
In this case,which do I have to install scikit-learn in python 3.12 or 3.11.0b4 ?
I suppose my understanding may not be right.....
Do you have any good ideas?
I executed "pyenv" on my MATLAB then it says
PythonEnvironment Properties:
Version: "3.12"
Executable: "C:\Users\ts-yosh\AppData\Local\Programs\Python\Python312\python.EXE"
Library: "C:\Users\ts-yosh\AppData\Local\Programs\Python\Python312\python312.dll"
Home: "C:\Users\ts-yosh\AppData\Local\Programs\Python\Python312"
Status: NotLoaded
ExecutionMode: InProcess

Hi @翼,

Given the compatibility table provided in the link below, MATLAB R2024b supports Python versions 3.9, 3.10, 3.11, and 3.12. This means you can technically use any of these versions to run your MATLAB functions that rely on Python.

https://www.mathworks.com/support/requirements/python-compatibility.html

My recommendation is mentioned below.

Use Python 3.11.0b4: Since you have already set this version as your local version in the specified folder (where your .python-version file is located), it makes sense to continue using it for your project, especially if you have already installed scikit-learn in this environment. If you run into any issues with compatibility or stability while using the beta version (3.11.0b4), consider switching to the stable version (3.10.5 or even 3.12). Also, make sure that you have scikit-learn installed in the active virtual environment linked to Python 3.11.0b4 using bash command.

   poetry add scikit-learn

MATLAB Configuration: Make sure that MATLAB is correctly configured to point to the Python executable from your selected version (3.11.0b4). You can set this in MATLAB using:

   pyenv('Version', 'path_to_python_executable'); % Update with your path

After setting up, you can test whether the integration works by executing a simple command in MATLAB:

   py.importlib.import_module('sklearn');

This should not return any errors if everything is configured correctly. Please bear in mind that while newer versions of Python often come with performance improvements and new features, they may also introduce breaking changes or bugs—especially beta versions like 3.11.0b4.

Hope this helps.

Thank you.
I solved my problem.
Best,
Hi @Umar,
Sorry to ask again after I said "I solved"...
One more thing, it is about this.
pyenv('Version', 'path_to_python_executable'); % Update with your path
I executed "poetry env info" command in the active virtual environment (env name: "caret") linked to Python 3.11.0b4 and
it says like this.
C:\Users\ts-yosh\Desktop\myPython\ver3110b4\caret>poetry env info
Virtualenv
Python: 3.11.0
Implementation: CPython
Path: C:\Users\ts-yosh\Desktop\myPython\ver3110b4\caret\.venv
Executable: C:\Users\ts-yosh\Desktop\myPython\ver3110b4\caret\.venv\Scripts\python.exe
Valid: True
Base
Platform: win32
OS: nt
Python: 3.11.0
Path: C:\Users\ts-yosh\.pyenv\pyenv-win\versions\3.11.0b4
Executable: C:\Users\ts-yosh\.pyenv\pyenv-win\versions\3.11.0b4\python.exe
Which path should I set at
'path_to_python_executable'
here ?
C:\Users\ts-yoshikawa\.pyenv\pyenv-win\versions\3.11.0b4\python.exe
or
C:\Users\ts-yosh\Desktop\myPython\ver3110b4\caret\.venv\Scripts\python.exe
?
I set "C:\Users\ts-yoshikawa\.pyenv\pyenv-win\versions\3.11.0b4\python.exe" and executed below command on my MATLAB,
py.importlib.import_module('sklearn');
but I got this error.
<frozen importlib>_find_and_load_unlocked
ModuleNotFoundError: No module named 'pandas'
In the active virtual environment (env name: "caret"), I did
"poetry run pip list" on my Command promt.
"pandas" and "sklearn" and stuff has been displayed.
Why is this happening? I couldn't figure out what is a problem....

Hi @翼 ,

Let me address your first query regarding, “Which path should I set at ‘path_to_python_executable'here ?C:\Users\ts-yoshikawa\.pyenv\pyenv-win\versions\3.11.0b4\python.exe or C:\Users\ts-yosh\Desktop\myPython\ver3110b4\caret\.venv\Scripts\python.exe?”

In your case, you should set the `path_to_python_executable` to the executable located within your virtual environment, which is:

C:\Users\tsyosh\Desktop\myPython\ver3110b4\caret\.venv\Scripts\python.exe

This is because when you use poetry, it creates a virtual environment that contains all the dependencies (like pandas and sklearn) specifically for that project. Setting the path to the virtual environment's Python executable ensures that when you run commands from MATLAB, they utilize the packages installed in that environment.

Now, let me address your second query regarding, “I set "C:\Users\ts-yoshikawa\.pyenv\pyenv-win\versions\3.11.0b4\python.exe" and executed below command on my MATLAB,py.importlib.import_module('sklearn'); but I got this error. importlib_find_and_load_unlocked ModuleNotFoundError: No module named 'pandas' In the active virtual environment (env name: "caret"), I did “poetry run pip list" on my Command promt. “pandas" and "sklearn" and stuff has been displayed. Why is this happening? I couldn't figure out what is a problem....”

The error you encountered (ModuleNotFoundError: No module named pandas) suggests that MATLAB is not accessing the same Python environment where pandas is installed. Here are steps to troubleshoot:

Verify Active Environment: Make sure that your MATLAB session is indeed using the virtual environment's Python by running: pyenv, this will show you which Python executable is currently being used.

Reconfigure MATLAB's Python Environment: If it's pointing to a different version or installation, reconfigure it: pyenv('Version', 'C:\Users\ts-yosh\Desktop\myPython\ver3110b4\caret\.venv\Scripts\python.exe');

Check Installed Packages: After setting the path, run:

py.importlib.import_module('pandas');

If this still fails, double-check whether you are running MATLAB from an environment that has access to your virtual environment.

MATLAB Command Window Restart: Sometimes changes require restarting the MATLAB command window or even restarting MATLAB itself to take effect.

Here are some additional insights that I would like to share with you.

Virtual Environments: Using virtual environments like those created by poetry helps isolate project dependencies, preventing conflicts with system-wide packages.

Package Management: Always verify that the necessary packages are installed in your virtual environment using poetry run pip list. If a package is missing, install it via bash: poetry add pandas

Hope this helps. Please let me know if you have any further questions.

Thank you for your help. I solved it !
Hi @翼,
It’s my pleasure to help you out and glad to know your problems have been resolved. If you still have any further questions, please let us know. If not then please don’t forget to click “Accept Answer”, and vote for @ Mike Croucher & @Walter Roberson who contributed their efforts on resolving this issue.
Hi @Umar,
I appreciate it. I got it.
So, actually I'm trying to make a surrogate model with MATLAB function and a model by Python.
I'm facing an issue about processing speed of MATLAB Function.
Sorry about additional question but let me ask and lend me your expertise.
I have the existing model that I downloaded from this link here.
In "Vehicle" subsystem of the above Simulink model,
I replaced this VCU subsystem that does some calculation
to MATLAB function block like this.
What I want is to speed up the process of calculation VCU subsystem does .
The MATLAB code is
function [re_brake_N,tgt_MG_trq_Nm] = SurrogateModelPython(open_brake,open_accel,v_VL_PNT,w_MG_PNT,SOC_PNT,HVAC_PNT,DCDC_PNT)
coder.extrinsic("pyrunfile");
tgt_MG_trq_Nm=20;
re_brake_N=30;
outputs=pyrunfile("scikit_learn_model.py","output_vars", ...
pyName1=open_brake,pyName2=open_accel,pyName3=v_VL_PNT, ...
pyName4=w_MG_PNT,pyName5=SOC_PNT,pyName6=HVAC_PNT,pyName7=DCDC_PNT);
re_brake_N=double(outputs(1));
tgt_MG_trq_Nm=double(outputs(2));
end
scikit_learn_model.py is
import pandas as pd
import pickle
def load_model(model_path='surrogate_model_new.pkl'):
model = pickle.load(model_path)
return model
def predict(model, input_data):
if isinstance(input_data, dict):
# If Input is dict type,convert to DataFrame
input_data = pd.DataFrame([input_data])
required_features = ['open_accel_Driver_per', 'open_brake_Driver_per','v_VL_PNT_mps_1',
'w_MG_PNT_radps', 'SOC_BT_Hi_PNT_per','P_HVAC_PNT_W','P_DCDC_PNT_W' ]
if not all(feature in input_data.columns for feature in required_features):
missing_features = [f for f in required_features if f not in input_data.columns]
return None
try:
prediction = model.predict(input_data[required_features])
return prediction
except Exception as e:
return None
model = load_model()
test_input = {
'open_accel_Driver_per': pyName2,
'open_brake_Driver_per': pyName1,
'v_VL_PNT_mps_1': pyName3,
'w_MG_PNT_radps': pyName4,
'SOC_BT_Hi_PNT_per': pyName5,
'P_HVAC_PNT_W': pyName6,
'P_DCDC_PNT_W': pyName7
}
result = predict(model, test_input)
output_vars = [float(result[0][0]), float(result[0][1])]
This MATLAB function and .py file code work without errors. They're the same calculation with VCU.
However,
I tried this MATLAB function and found out the processing speed became slower than VCU.
I looked into it and I recognized pyrunfile method here.
outputs=pyrunfile("scikit_learn_model.py","output_vars"......
I thought it takes a long time to load my python file in particular......
I wonder if using MATLAB function slow down the process due to its specification.
What are your thoughts on this matter?
Do you have any ideas ? It'd be nice if you could give me advice if you know better solution.
Best,

Hi @翼 ,

After going through your comments, now I understand what was the main reason behind all these issues because you are trying to enhance the processing speed of the Vehicle Control Unit (VCU) subsystem by leveraging the capabilities of a pre-trained scikit-learn model in Python. However, you have encountered performance issues, with the MATLAB function being slower than the original VCU calculations. Based on your observations, this slowdown appears to be linked to the use of the pyrunfile function, which loads the Python file each time it is called. My comments regarding to performance analysis are listed below.

Overhead of pyrunfile: The pyrunfile function shared by you introduces overhead because it must load the Python environment and execute the script every time it is called. This can significantly slow down execution, especially if your surrogate model is called frequently within simulations.

MATLAB-Python Integration: The integration between MATLAB and Python can be slow due to data type conversions and the need to pass data back and forth between environments. Each call to pyrunfile entails serializing inputs and deserializing outputs, which adds latency.

Model Loading Time: Loading your model with pickle in each function call can also contribute to delays. Ideally, you want to load the model once and reuse it.

So, my recommendations for improvement would be modifying your Python script so that the model is loaded only once at the start rather than during each function call. You can use global variables or singleton patterns in your Python code:

   import pandas as pd
   import pickle
   model = None  # Global variable
   def load_model(model_path='surrogate_model_new.pkl'):
       global model
       if model is None:
           with open(model_path, 'rb') as f:
               model = pickle.load(f)
       return model

If possible, batch your inputs together and process them in one call instead of individual calls for each input. This reduces the overhead associated with multiple calls. Make sure your calculations can be expressed using MATLAB's built-in functions or if you can convert your scikit-learn model to a format compatible with MATLAB (e.g., using MATLAB's Statistics and Machine Learning Toolbox), this might yield better performance. If possible use MATLAB’s Profiler (profile on;) to identify bottlenecks in your code execution. This will help you understand where time is being spent and guide further optimization efforts. For more information on profiling, please refer to

https://www.mathworks.com/help/matlab/matlab_prog/profiling-for-improving-performance.html

Some additional insights to help you out would be

Integration Alternatives: Consider using MATLAB's built-in support for Python functions more efficiently with py.<module>.<function> syntax instead of pyrunfile. This method may provide better performance.

Data Type Considerations: Make sure that data types being passed between MATLAB and Python are optimized for speed (e.g., using NumPy arrays).

Hopefully, by implementing these strategies, you should see an improvement in the processing speed of your surrogate model integration between MATLAB and Python.

Please let me know if you have any further questions.

Hi @Umar,
Thank you for telling me. I see.
Either way, even if we were to use pyrun, the processing speed would inevitably be slower, as you say.
It is what it is....
Make sure your calculations can be expressed using MATLAB's built-in functions or if you can convert your scikit-learn model to a format compatible with MATLAB (e.g., using MATLAB's Statistics and Machine Learning Toolbox)
I actually tried to use Statistics and Machine Learning Toolbox as well, but my surrogate model is a model that predicts 2 outputs for 7 inputs.
Also, I've heard that this toolbox does not support multiple outputs.
It seems like the Statistics and Machine Learning Toolbox deals with a single output
If we require the output to have two signals, then we will require the Deep Learning Toolbox.
So I am now trying to create a model using the Deep Learning Toolbox.
You mentioned,
Integration Alternatives: Consider using MATLAB's built-in support for Python functions more efficiently with py.<module>.<function> syntax instead of pyrunfile. This method may provide better performance.
Data Type Considerations: Make sure that data types being passed between MATLAB and Python are optimized for speed (e.g., using NumPy arrays).
I'm curious about these points.
Does that mean we could make it better with just Python and MATLAB code without relying on Toolbox?
If so, what would you refine my MATLAB function and python code?
I wonder if it's better and faster to use Julia instead of Python. I imagine it's kind of like you make the same surrogate model with Julia and execute Julia in the Simulink model.
I wish I could get better performance with this idea....
Best,
Hi @ 翼,
I will address your comments in detail right after client’s meeting.

Hi @翼 ,

Let me first address your challenge with predicting multiple outputs in MATLAB, you are correct that the Statistics and Machine Learning Toolbox is primarily designed for single-output models. This limitation can indeed pose difficulties when working with complex datasets that require simultaneous predictions across multiple outputs. Given your situation, here are some tailored solutions and insights:

Deep Learning Toolbox: Since you are considering using the Deep Learning Toolbox, this is a promising direction. The toolbox supports various architectures that can handle multiple outputs effectively. For instance, you can create a feedforward neural network with multiple output layers. Let’s say that your model has seven input features and two output targets, you could define a network structure where the final layer consists of two neurons (one for each output). This allows the network to learn from the inputs while simultaneously predicting both outputs.

Model Structure: Consider using a simple feedforward neural network or a more complex architecture like LSTM if your data has sequential dependencies. You can define your model using the layerGraph function to customize the architecture according to your needs.

Training Data: Make sure that your training data is structured appropriately, with inputs organized in a matrix form (7 columns for 7 inputs) and outputs in another matrix (2 columns for 2 outputs). Use functions like trainNetwork to train your model once it’s defined.

Here’s a simplified example of how you might set up a neural network in MATLAB for multi-output prediction:

% Define input and output sizes
inputSize = 7; % Number of inputs
outputSize = 2; % Number of outputs
% Generate synthetic training data
numSamples = 100; % Number of training samples
XTrain = rand(numSamples, inputSize); % Random input data
YTrain = rand(numSamples, outputSize); % Random output data
% Create layers
layers = [
  featureInputLayer(inputSize)
  fullyConnectedLayer(10) % Hidden layer with 10 neurons
  reluLayer % ReLU activation function
  fullyConnectedLayer(outputSize) % Output layer for two outputs
  regressionLayer]; % Use regression layer for continuous output
% Specify training options
options = trainingOptions('adam', ...
  'MaxEpochs', 100, ... % Maximum number of epochs
  'Plots', 'training-progress', ... % Plot training progress
  'Verbose', 0); % Suppress verbose output
% Train the network
net = trainNetwork(XTrain, YTrain, layers, options);

Now addressing your comments regarding, “I'm curious about these points. Does that mean we could make it better with just Python and MATLAB code without relying on Toolbox?If so, what would you refine my MATLAB function and python code?I wonder if it's better and faster to use Julia instead of Python. I imagine it's kind of like you make the same surrogate model with Julia and execute Julia in the Simulink model.I wish I could get better performance with this idea....”

By utilizing the py.<module>.<function> syntax instead of pyrunfile, you can directly call Python functions, which is generally more efficient. This approach minimizes overhead by avoiding the need to run an entire script each time.

     result = py.module_name.function_name(arg1, arg2);

This method enhances performance by allowing for better data type management, especially when using NumPy arrays, which are optimized for speed.

Data Type Considerations: Make sure that data types passed between MATLAB and Python are compatible and optimized. For instance, using NumPy arrays in Python allows for faster computations compared to standard lists.

     import numpy as np
     arr = np.array([1, 2, 3])

Generally, consider vectorization in MATLAB. Replace loops with vectorized operations where possible to enhance speed. Minimize data conversion overhead by keeping data in compatible formats.

Now finally, addressing your comment, “considering Julia as an Alternative”

Performance Benefits: Julia is designed for high performance, particularly in numerical and scientific computing. Benchmarks show that Julia often outperforms both MATLAB and Python, especially with larger datasets.

Syntax Similarity: The syntax of Julia is similar to that of MATLAB, making the transition smoother for users familiar with MATLAB’s style.Example syntax comparison:

     % MATLAB
     for i = 1:N
         % do something
     end
     # Julia
     for i in 1:N
         # do something
     end

If you decide to explore Julia further, start with its basic constructs and gradually incorporate it into your projects to assess its capabilities. Also, consider using Julia alongside your existing workflows; it can serve as a powerful tool without fully abandoning the tools you currently use.

Please click the following links below to gain more information.

https://discourse.julialang.org/t/including-a-julia-model-in-simulink/80627

https://www.mathworks.com/support/search.html/answers/1582569-matlab-is-significantly-slower-than-julia-on-simple-evaluation.html?fq%5B%5D=asset_type_name:answer&fq%5B%5D=category:parallel-computing/gpu-computing&page=1

If you still have any further questions, please let me know.

Hi @ 翼,
Please let me know if you need any further assistance or feedback, I will be more happy to help you.
Hi,@Umar,
Thank you for your advice!
Unfortunately, I'm currently swampped with work today....
I'll check your answer and try it after I get off work. Sorry for the late reply.
Best,
Hi @ 翼,
No problem. I completely understand.
Hi @Umar,
I went through your comment.
I just thought it is kind of hard to go with the way to use Python....
I feel like we can not avoid bad performance if we use this way...
Actually, I wonder if I should use DeepLearningToolbox or Julia.
Julia is designed for high performance as you mentioned,
but even if you use Julia, you must use MATLAB Function....
I'm not quite sure if Julia may be a better solution and also, there is few information about this matter....
By the way, which way would you try and go with
"Replace VCU subsystem to MATLAB Function with Python"
or
"Replace VCU subsystem to the block exported by DeepLearningToolbox"
?
I just tried DeepNetworkDesigner of DeepLearningToolbox and exported it as Simulink block,
but the block that has single input and single output was created like this.
Here is what I did.
1: open DeepNetworkDesigner and select LSTM
2: change inputsize to 7 on sequenceInputLayer
3: change outputsize to 2 on fullyConnectedLayer
4: export to Simulink as a block
Since I'm DeepLearningToolbox beginner, I may be missing something.....
I'd like you to tell me my mistakes if you don't mind.
When you replace VCU subsystem to this DeepNetworkDesigner block,
how should we connect 7 inputs and 2 outputs to this block?
I'd also like to know how to train CSV data with DeepNetworkDesigner.
Here is my CSV data that I want to use for training in order to create a model. See attached.
Lines have been cut to reduce file size.
Hi @ 翼,
I have gone through your comments. Give me some time to put my thoughts put together to address your issues in detail.
Hi @Umar,
Much appreciated.
I'm so happy to receive your advice.
Best,

Hi @翼,

Thanks for your kind words.

After going through documentation provided at the link below

https://www.mathworks.com/help/deeplearning/ref/deepnetworkdesigner-app.html

and analysis of your comments, please see my response below.

Performance Considerations: Python vs. Julia vs. MATLAB

When considering whether to use Python, Julia, or MATLAB for deep learning applications, each option has distinct advantages:

Python: Widely adopted with extensive libraries (e.g., TensorFlow, PyTorch), rich community support and resources. However, performance can be suboptimal for certain operations due to its interpreted nature.

Julia: Designed for high-performance numerical computing, Offers speed comparable to C in many cases, making it suitable for intensive computations, integrates well with existing libraries but may have a steeper learning curve and less community support compared to Python.

MATLAB (Deep Learning Toolbox)

Provides seamless integration with Simulink, making it ideal for model-based design.The toolbox simplifies the process of designing and exporting neural networks into Simulink.Performance can be highly optimized for specific tasks, especially when leveraging MATLAB’s built-in functions.

In your case, if you are primarily working within the MATLAB environment and need to interface directly with Simulink, continuing with the Deep Learning Toolbox might be more beneficial despite concerns about performance.

Replacing VCU Subsystem

To replace the VCU subsystem with a block exported from Deep Learning Toolbox:

Exporting the Model: After designing your LSTM model in Deep Network Designer, ensure you export it correctly to Simulink. Select "Export > Export to Simulink" to generate a corresponding block.

Connecting Inputs and Outputs

Your LSTM model has 7 inputs and 2 outputs. In Simulink, connect the inputs to your block by linking them directly to the input ports of the LSTM block.Ensure that your input data format matches what the network expects (e.g., [sequence length x number of features]). For outputs, connect the output ports of the block to wherever you need the predicted values in your system.

Input Data Format

The LSTM block expects inputs as an N-by-C matrix (where N is the number of sequences and C is the number of features). You may need to reshape or transpose your input data accordingly.

Training with CSV Data

To train your model using soc_10.csv, follow these steps:

Data Preparation: Load your CSV data into MATLAB using readtable or csvread. Ensure that you clean and preprocess your data (e.g., handling missing values).

    data = readtable('soc_10.csv');

Separating Inputs and Outputs: Identify which columns correspond to inputs (features) and which column is the output label.

Creating Training Data: Convert your table into matrices suitable for training:

    X = data{:, 1:7}; % Assuming first 7 columns are inputs
    Y = data{:, 8}; % Assuming 8th column is output

Using Deep Network Designer: In Deep Network Designer, create a new network or modify an existing one to accept your prepared dataset. Utilize options such as Train Networt after setting up training parameters.

Training: Ensure you define appropriate training options (e.g., learning rate, epochs) before starting the training process.

    options = trainingOptions('adam', ...
        'MaxEpochs', 100, ...
        'MiniBatchSize', 32, ...
        'Verbose', false);
    net = trainNetwork(X, Y, layers, options);

Always refer back to MATLAB's official documentation for detailed examples on configuring blocks and exporting models; this can provide clarity on specific functionalities you may not have explored yet.

By following these structured steps and considerations, you should be able to resolve your current challenges effectively while optimizing performance within your chosen environment.

Hi @Umar,
Thank you for your opinion!
I got it.
Much Appreciated.
I tried Deep Learning Toolbox and
after I created my network model by this line,
net = trainNetwork(X, Y, layers, options);
and I used exportNetworkToSimulink function, however it didn't work..
I executed like this.
mdlInfo = exportNetworkToSimulink(net)
but I got this error....
The argument at position 1 is invalid. Value must be of type dlnetwork or convertible to dlnetwork.
I'm not quite sure what this error says.
Would you happen to know why this error happens?
I wonder if I'm missing something.
I understood the steps I need to follow in order to train my model using soc_10.csv, but I can't figure out how to export the model to Simulink as Block after executing this line.
net = trainNetwork(X, Y, layers, options);
I'm getting there...
Best,

Hi @翼 ,

After going through your comments and reading through documentation provided at the link below,

https://www.mathworks.com/help/deeplearning/ref/trainnetwork.html?searchHighlight=trainNetwork&s_tid=srchtitle_support_results_1_trainNetwork

Here is how to resolve the error you are encountering, let me break down the potential causes and solutions systematically.

Understanding dlnetwork

The function exportNetworkToSimulink requires the input argument to be a dlnetwork object. If you are using trainNetwork, it returns a different type of network object (usually a SeriesNetwork or DAGNetwork). This is likely why you are seeing the error: The argument at position 1 is invalid. Value must be of type dlnetwork or convertible to dlnetwork.

Converting Your Network

To export your trained network to Simulink, you need to convert it into a dlnetwork object. You can do this by using the dag2dlnetwork function if your network is a DAG (Directed Acyclic Graph) network or by creating a new dlnetwork from your existing layers. Here’s how you can convert your trained network:

% Assuming net is your trained SeriesNetwork or DAGNetwork
if isa(net, 'SeriesNetwork') || isa(net, 'DAGNetwork')
  dlnet = dag2dlnetwork(net); % Convert to dlnetwork
else
  error('The trained network must be a SeriesNetwork or DAGNetwork.');
end
mdlInfo = exportNetworkToSimulink(dlnet);

Training with trainnet

As you have noted, MATLAB has recommended using trainnet instead of trainNetwork. If you are starting fresh or want to follow best practices, consider retraining your model with trainnet. Here is a brief example of how you can do this:

% Define your layers and options as before
% Define your data X (features) and Y (targets)
% Using trainnet for training
net = trainnet(X, Y, layers, options);
% Now convert it to dlnetwork before exporting
dlnet = dag2dlnetwork(net);
mdlInfo = exportNetworkToSimulink(dlnet);

Be aware that the exportNetworkToSimulink function has limitations. It only supports networks with one input and one output and does not support certain layer types. Check the documentation for details on supported layers. Also, generated Simulink model references the original network, meaning you can update weights without needing to re-export as long as you keep the same architecture. Now, in case if you encounter further errors related to unsupported layers during export, consult the documentation on supported deep learning layer blocks and consider using placeholder subsystems for unsupported blocks.

Follow up with any additional questions if needed!

Thank you for your advice !
I'll go through your comment.
Actually, I'm now off work and on holiday, so let me get back to you in two days.
I'm going to try that then touch base with you.
Sorry, thanks.
Hi @Umar,
Thank you for telling me "trainnet" .
I tried this code but it didn't work, I took a look at the documentation but I could not figure out.
data = readtable('soc_10.csv');
inputVariables = {'v_VL_PNT_mps_1', 'w_MG_PNT_radps', 'open_brake_Driver_per', ...
'open_accel_Driver_per', 'SOC_BT_Hi_PNT_per', 'P_HVAC_PNT_W', 'P_DCDC_PNT_W'};
XTrain = table2array(data(:, inputVariables));
outputVariables = {'F_VCU_CNT_re_break_N', 'tgt_Trq_VCU_CNT_MG_Nm'};
YTrain = table2array(data(:, outputVariables));
inputSize = numel(inputVariables);
outputSize = numel(outputVariables);
layers = [
featureInputLayer(inputSize)
fullyConnectedLayer(10)
reluLayer
fullyConnectedLayer(outputSize)
regressionLayer];
options = trainingOptions('adam', ...
'MaxEpochs', 100, ...
'Plots', 'training-progress', ...
'Verbose', 0);
net = trainnet(XTrain, YTrain, layers, options);
I got this error at line net = trainnet(XTrain, YTrain, layers, options).
Input arguments are missing.
However, I tried net = trainNetwork(XTrain, YTrain, layers, options)
then it worked.
After that, I did
if isa(net, 'SeriesNetwork') || isa(net, 'DAGNetwork')
dlnet = dag2dlnetwork(net); % Convert to dlnetwork
mdlInfo = exportNetworkToSimulink(dlnet);
else
error('The trained network must be a SeriesNetwork or DAGNetwork.');
end
then this simulink model was created.
I'm aware that I have created the model that has 7 input signals and predicts 2 output signals.
I'd like to know what type of data I should put into "my_model_1".
If you don't mind,
Could you possibly make a simple model that has 7 input signals and predicts 2 output signals on your side then share your .slx and .mlx code ?
I want to make use of it.
I'd appreciate it if you could tell me why the error is happening and give me any good example towards what I want to do.
Best,

Hi @翼 ,

The error you encountered—“Input arguments are missing”—occurs because trainnet is not a recognized function in MATLAB for training deep learning networks. Instead, the correct function is trainNetwork, which is designed for this purpose. The confusion likely arose from referencing an older or incorrect function. When you switched to trainNetwork, it successfully processed your input data, indicating that your setup was otherwise correct.

Input Data for Simulink Model

Regarding the data you should input into "my_model_1" (your Simulink model), here is a breakdown:

Input Signals: You have 7 input signals which correspond to the variables in your dataset: v_VL_PNT_mps_1, w_MG_PNT_radps, open_brake_Driver_per, open_accel_Driver_per, SOC_BT_Hi_PNT_per, P_HVAC_PNT_W, P_DCDC_PNT_W

Each of these signals should be provided as inputs in the same format as used during training (e.g., numeric arrays or appropriate Simulink blocks).

Output Signals: Your model predicts 2 output signals: F_VCU_CNT_re_break_N, tgt_Trq_VCU_CNT_MG_Nm

After processing the inputs, the model will output values corresponding to these predictions.

Here is simplified approach to create a similar model in MATLAB and Simulink:

   MATLAB Code (.mlx)
   % Load Data
   data = readtable('soc_10.csv');
   inputVariables = {'v_VL_PNT_mps_1', 'w_MG_PNT_radps', ...
                     'open_brake_Driver_per', 'open_accel_Driver_per', ...
                     'SOC_BT_Hi_PNT_per', 'P_HVAC_PNT_W', 'P_DCDC_PNT_W'};
   XTrain = table2array(data(:, inputVariables));
   outputVariables = {'F_VCU_CNT_re_break_N', 'tgt_Trq_VCU_CNT_MG_Nm'};
   YTrain = table2array(data(:, outputVariables));
   % Define Neural Network
   layers = [
       featureInputLayer(numel(inputVariables))
       fullyConnectedLayer(10) 
       reluLayer
       fullyConnectedLayer(numel(outputVariables))
       regressionLayer];
   options = trainingOptions('adam', ...
       'MaxEpochs', 100, ...
       'Plots', 'training-progress', ...
       'Verbose', 0);
   % Train Network
   net = trainNetwork(XTrain, YTrain, layers, options);
   % Convert to dlnetwork and export to Simulink
   dlnet = dag2dlnetwork(net);
   mdlInfo = exportNetworkToSimulink(dlnet);

Simulink Model (.slx): Below is a simple example of how to create a Simulink model with 7 input signals and 2 output signals. This example will use a MATLAB script to generate the model programmatically.

% Create a new Simulink model
modelName = 'my_model_1';
open_system(new_system(modelName));
% Add input blocks
for i = 1:7
  add_block('simulink/Sources/Constant', [modelName, '/Input', 
  num2str(i)]);
  set_param([modelName, '/Input', num2str(i)], 'Value', 'rand()'); % 
  Random   
  input for demonstration
  set_param([modelName, '/Input', num2str(i)], 'Position', [100, 100 + 
  (i-1)*50, 
  130, 120 + (i-1)*50]);
end
% Add a block to combine inputs (e.g., a Mux block)
add_block('simulink/Signal Routing/Mux', [modelName, '/Mux']);
set_param([modelName, '/Mux'], 'Inputs', '7', 'Position', [200, 100,   
230, 150]);
% Connect input blocks to the Mux
for i = 1:7
  add_line(modelName, ['Input', num2str(i), '/1'], 'Mux/1');
end
% Add output blocks
for i = 1:2
  add_block('simulink/Sinks/Out1', [modelName, '/Output', num2str(i)]);
  set_param([modelName, '/Output', num2str(i)], 'Position', [400, 100 + 
  (i-1)*50, 430, 120 + (i-1)*50]);
end
% Add a block to split outputs (e.g., a Demux block)
add_block('simulink/Signal Routing/Demux', [modelName, '/Demux']);
set_param([modelName, '/Demux'], 'Outputs', '2', 'Position', [300, 100, 
330,150]);
% Connect Mux to Demux
add_line(modelName, 'Mux/1', 'Demux/1');
% Connect Demux to output blocks
for i = 1:2
  add_line(modelName, 'Demux/1', ['Output', num2str(i), '/1']);
end
% Save the model
save_system(modelName);
close_system(modelName);

Saving and Sharing the Model

After running the above MATLAB code, a Simulink model named my_model_1.slx will be created in your current working directory

Ensure that your input data is normalized or standardized before feeding it into the network. This can significantly improve performance. Familiarize yourself with relevant Simulink blocks such as “From Workspace” for input and “To Workspace” for output to facilitate easy data flow. Also, consider validating your model with unseen data after training to assess its performance and robustness.

Hope this helps.

If you have further questions or need additional examples, feel free to ask!

Hi, @Umar
Thank you for your advice.Sorry for my ambiguous question.
I mean, after I created my model by exportNetworkToSimulink,
I tried to do like this, but I couldn't connect input signals correctly
I just wanted to test with simple input data.
It was impossible to connect 7 input signals.
Is my understanding wrong?
I heard we can make an AI model with 7 input signals and 2 output by DeepLearningToolbox and integrate it into Simulink model....
I wonder if I'm doing wrong.
Do you have any ideas?

Hi @翼,

When exporting a deep learning model to Simulink using exportNetworkToSimulink , it is essential to ensure that the input signals are correctly configured. After reviewing the documentation provided at the link below,

https://www.mathworks.com/help/deeplearning/ref/dlnetwork.exportnetworktosimulink.html

This function generates a model that includes deep learning layer blocks corresponding to the layers in your network. If you have seven input signals, you need to ensure that your model's input layer is designed to accept this configuration. Here is a simple example of how to export a network with multiple inputs:

% Define a simple deep learning network
  layers = [
  featureInputLayer(7) % 7 input features
  fullyConnectedLayer(10)
  reluLayer
  fullyConnectedLayer(2) % 2 outputs
  regressionLayer];
   net = dlnetwork(layers);
% Export to Simulink
mdlInfo = exportNetworkToSimulink(net, ModelName="myExportedModel");

Make sure that the input layer of your network is set to accept the correct number of features (in this case, 7). If you encounter issues, verify that the input data types and dimensions match what the model expects. Additionally, check that the generated model's inport blocks are correctly configured to receive the input signals.

Hope this helps.

Please let me know if you have any further questions.

Hi @Umar,
Much appreciatd.
I'm so sorry but, once I tried your code, I got the error at this line of your suggested code.
net = dlnetwork(layers);
Error says.
Error while using: dlnetwork/initialize (line 600)
Invalid network.
Error: dlnetwork (line 182)
net = initialize(net, dlX{:});
Causes: dlnetwork
Layer 'regressionoutput': output layer detected. The network cannot have an output layer.
Here is my whole code.
% Load data from cSV
data = readtable('soc_10.csv');
inputVariables = {'v_VL_PNT_mps_1', 'w_MG_PNT_radps', 'open_brake_Driver_per', ...
'open_accel_Driver_per', 'SOC_BT_Hi_PNT_per', 'P_HVAC_PNT_W', 'P_DCDC_PNT_W'};
XTrain = table2array(data(:, inputVariables));
outputVariables = {'F_VCU_CNT_re_break_N', 'tgt_Trq_VCU_CNT_MG_Nm'};
YTrain = table2array(data(:, outputVariables));
% Define a simple deep learning network
layers = [
featureInputLayer(7) % 7 input features
fullyConnectedLayer(10)
reluLayer
fullyConnectedLayer(2) % 2 outputs
regressionLayer];
net = dlnetwork(layers);
% Export to Simulink
mdlInfo = exportNetworkToSimulink(net, ModelName="myExportedModel");
I've attached my CSV file for your information. Just take a look. It's like this.
I think input data type is "double".
Did you face the same error with me?
I suppose the cause is not the difference of MATLAB version... I wonder where I'm missing...
If you are on my side, how would you create AI model that has 7 inputs and 2 outputs?
I'm still not quite sure what steps I should do...
Best,

Hi @翼 ,

The error you are experiencing is due to the presence of an output layer in your network configuration that is not compatible with the dlnetwork function. Specifically, the regressionLayer is intended for regression tasks, and it should be the last layer in your network. However, the error message suggests that the network is not being recognized correctly, possibly due to the way the layers are defined or initialized. To resolve this issue, let's go through the steps to create a deep learning model with 7 inputs and 2 outputs, ensuring that the network is correctly configured. Below is the updated code with detailed explanations:

% Load data from CSV
data = readtable('soc_10.csv');
% Define input and output variables
inputVariables = {'v_VL_PNT_mps_1', 'w_MG_PNT_radps', 
'open_brake_Driver_per', ...
  'open_accel_Driver_per', 'SOC_BT_Hi_PNT_per', 'P_HVAC_PNT_W', 
  'P_DCDC_PNT_W'};
XTrain = table2array(data(:, inputVariables)); % Input features
outputVariables = {'F_VCU_CNT_re_break_N', 'tgt_Trq_VCU_CNT_MG_Nm'};
YTrain = table2array(data(:, outputVariables)); % Output targets
% Define a simple deep learning network
layers = [
  featureInputLayer(7) % 7 input features
  fullyConnectedLayer(10) % Hidden layer with 10 neurons
  reluLayer % Activation function
  fullyConnectedLayer(2) % Output layer with 2 outputs
  regressionLayer]; % Regression layer for continuous outputs
% Create the dlnetwork object
net = dlnetwork(layers);
% Check if the network is valid
if ~isvalid(net)
  error('The network is invalid. Please check the layer     configuration.');
end
% Export to Simulink
mdlInfo = exportNetworkToSimulink(net, ModelName="myExportedModel");

So, in the above code snippet, the layers are defined correctly, with the featureInputLayer taking 7 inputs, followed by a fullyConnectedLayer, a reluLayer, another fullyConnectedLayer for the outputs, and finally a regressionLayer. This configuration is appropriate for a regression task with two outputs. The dlnetwork function is called with the defined layers. It is crucial to ensure that the layers are compatible with the intended task. The regressionLayer is correctly placed as the last layer. After creating the dlnetwork, a validation check is performed using isvalid(net). This ensures that the network is properly initialized before proceeding to export it to Simulink. The exportNetworkToSimulink function is called to export the network model, which is essential for integrating the deep learning model into a Simulink environment.

Additional Considerations

  • Data Type: Ensure that the input data type is indeed double, as you mentioned. You can verify this by checking the class of XTrain and YTrain using class(XTrain) and class(YTrain).
  • MATLAB Version: While you suspect that the MATLAB version is not the issue, it is always good practice to ensure that you are using a version that supports the functions and features you are utilizing.
  • Training the Network: After successfully creating the network, you will need to train it using the trainNetwork function, which requires specifying training options. This step is crucial for the model to learn from the data.

Hope this helps.

If you encounter further issues, please feel free to reach out for additional assistance.

Thank you for your help!
I finally solved it.
Hi @翼 ,
Glad to know your problem is resolved. Please don’t forget to click “Accept Answer” and vote for the contributors helped out resolving this problem.
I was trying to do it but ,where is “Accept Answer” button?
I somehow can not find it on this post....

Sign in to comment.

 Accepted Answer

Hi @翼 ,

The error you are experiencing is due to the presence of an output layer in your network configuration that is not compatible with the dlnetwork function. Specifically, the regressionLayer is intended for regression tasks, and it should be the last layer in your network. However, the error message suggests that the network is not being recognized correctly, possibly due to the way the layers are defined or initialized. To resolve this issue, let's go through the steps to create a deep learning model with 7 inputs and 2 outputs, ensuring that the network is correctly configured. Below is the updated code with detailed explanations:

% Load data from CSV
data = readtable('soc_10.csv');
% Define input and output variables
inputVariables = {'v_VL_PNT_mps_1', 'w_MG_PNT_radps', 
'open_brake_Driver_per', ...
  'open_accel_Driver_per', 'SOC_BT_Hi_PNT_per', 'P_HVAC_PNT_W', 
  'P_DCDC_PNT_W'};
XTrain = table2array(data(:, inputVariables)); % Input features
outputVariables = {'F_VCU_CNT_re_break_N', 'tgt_Trq_VCU_CNT_MG_Nm'};
YTrain = table2array(data(:, outputVariables)); % Output targets
% Define a simple deep learning network
layers = [
  featureInputLayer(7) % 7 input features
  fullyConnectedLayer(10) % Hidden layer with 10 neurons
  reluLayer % Activation function
  fullyConnectedLayer(2) % Output layer with 2 outputs
  regressionLayer]; % Regression layer for continuous outputs
% Create the dlnetwork object
net = dlnetwork(layers);
% Check if the network is valid
if ~isvalid(net)
  error('The network is invalid. Please check the layer     configuration.');
end
% Export to Simulink
mdlInfo = exportNetworkToSimulink(net, ModelName="myExportedModel");

So, in the above code snippet, the layers are defined correctly, with the featureInputLayer taking 7 inputs, followed by a fullyConnectedLayer, a reluLayer, another fullyConnectedLayer for the outputs, and finally a regressionLayer. This configuration is appropriate for a regression task with two outputs. The dlnetwork function is called with the defined layers. It is crucial to ensure that the layers are compatible with the intended task. The regressionLayer is correctly placed as the last layer. After creating the dlnetwork, a validation check is performed using isvalid(net). This ensures that the network is properly initialized before proceeding to export it to Simulink. The exportNetworkToSimulink function is called to export the network model, which is essential for integrating the deep learning model into a Simulink environment.

Additional Considerations

  • Data Type: Ensure that the input data type is indeed double, as you mentioned. You can verify this by checking the class of XTrain and YTrain using class(XTrain) and class(YTrain).
  • MATLAB Version: While you suspect that the MATLAB version is not the issue, it is always good practice to ensure that you are using a version that supports the functions and features you are utilizing.
  • Training the Network: After successfully creating the network, you will need to train it using the trainNetwork function, which requires specifying training options. This step is crucial for the model to learn from the data.

Hope this helps.

If you encounter further issues, please feel free to reach out for additional assistance.

1 Comment

Hi @翼 ,
I was trying to do it but ,where is “Accept Answer” button? I somehow can not find it on this post....”
You should be able to see it now.

Sign in to comment.

More Answers (0)

Categories

Products

Release

R2024b

Asked:

翼
on 21 Sep 2024

Commented:

on 8 Oct 2024

Community Treasure Hunt

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

Start Hunting!