How to debug CustomPythonModelBlock in Simulink?
37 次查看(过去 30 天)
显示 更早的评论
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:
0 个评论
回答(1 个)
Jaimin
2024-11-29,6:09
Hi @Jinming
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.
Debug Simulation Using Signal Breakpoints: https://www.mathworks.com/help/simulink/ug/conditional-breakpoints.html?lang=en
I hope this will be helpful.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Classification Ensembles 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!