How to debug CustomPythonModelBlock in Simulink?

조회 수: 2 (최근 30일)
Jinming
Jinming 2024년 11월 25일
댓글: Jinming 2024년 12월 8일
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일
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일
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에서 Get Started with Statistics and Machine Learning Toolbox에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by