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

173 次查看(过去 30 天)
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]
  33 个评论
Umar
Umar 2024-10-4,14:17

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!

翼
2024-10-4,15:17
编辑: 2024-10-4,15:17
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.

请先登录,再进行评论。

回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Call Python from MATLAB 的更多信息

产品


版本

R2024b

Community Treasure Hunt

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

Start Hunting!

Translated by