How to debug CustomPythonModelBlock in Simulink?

37 次查看(过去 30 天)
Jinming
Jinming 2024-11-25,8:03
评论: Jinming 2024-12-8,7:59
I use CustomPythonModelBlock to implement my trained Reinforcemen Learning model in simulink.
I have test `load_TD3AQ.py` in PyCharm with:
if __name__ == '__main__':
model_dir = 'TD3AQ-{COWHVC-v7}/seed-001-2024-11-17-10-20-52'
model_name = 'epoch-2900.pt'
test_model = load_model(model_dir, model_name)
_, _ = test_model.env.reset()
observation = test_model.env.reset_zero(soc_init=0.6)
obs_list = []
act_list = []
for _ in range(100):
action = predict(test_model, observation)
obs_list.append(observation)
act_list.append(action)
observation, r, c, done, truncated, info = test_model.env.step(action)
if done:
break
print(obs_list)
print(act_list)
And everything goes fine, but when I test it in Simulink, there was an error:
Error:MATLAB System block 'sub_slx_RLAgent/Custom Python Model Predict/Custom Python Model Block' error occurred when invoking 'getOutputSizeImpl' method of 'internal.pycoexblks.CustomPythonModelBlock'. The error was thrown from '
'C:\Program Files\MATLAB\R2024b\toolbox\shared\pycoexblks\+internal\+pycoexblks\PythonModelBlockBase.p' at line 0
'C:\Program Files\MATLAB\R2024b\toolbox\shared\pycoexblks\+internal\+pycoexblks\PythonModelBlockBase.p' at line 0
'C:\Program Files\MATLAB\R2024b\toolbox\shared\pycoexblks\+internal\+pycoexblks\PythonModelBlockBase.p' at line 0
'C:\Program Files\MATLAB\R2024b\toolbox\shared\pycoexblks\+internal\+pycoexblks\PythonModelBlockBase.p' at line 0
'C:\Program Files\MATLAB\R2024b\toolbox\simulink\ui\studio\config\m\+SLStudio\SimulationForwardWithDebuggerCB.p' at line 0
'C:\Program Files\MATLAB\R2024b\toolbox\simulink\ui\studio\config\m\+SLStudio\ToolBars.p' at line 0'.
Caused by:
Call to the Python model predict() function 'py.load_TD3AQ.predict(...)' failed. The Python error message is: == START OF PYTHON ERROR MESSAGE ==
Python Error: RuntimeError: Tensors must have same number of dimensions: got 1 and 2
== END OF PYTHON ERROR MESSAGE ==.
I have no idea about this error, is there any way to debug the code to check which line is wrong?
I place the slx and py file in the attachment.
-------------------------------------------------------------------
Update:
I try to return a specific array directly in predict():
still error:

回答(1 个)

Jaimin
Jaimin 2024-11-29,6:09
The error message you are encountering is a RuntimeError from Python, specifically related to tensor operations in a library like PyTorch or TensorFlow.
The error message indicates that an operation in your predict() function is trying to combine or compare two tensors that have different numbers of dimensions. 
To debug this, set a breakpoint at the predict() function call and analyze the dimensions of the input arguments.
For more information kindly refer following MathWorks Documentation.
I hope this will be helpful.
  1 个评论
Jinming
Jinming 2024-12-8,7:59
Thank you for your comment. I understand the meaning of the error message, but I am returning a specific array directly in the predict() method, and I'm still encountering the error. Unfortunately, signal breakpoints are not applicable in this case.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Classification Ensembles 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by