Clear Filters
Clear Filters

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

22 views (last 30 days)
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
Umar
Umar on 8 Oct 2024
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.
翼
on 8 Oct 2024
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

Umar
Umar on 8 Oct 2024

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
Umar
Umar on 8 Oct 2024
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)

Products


Release

R2024b

Community Treasure Hunt

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

Start Hunting!