TensorFlow Model Predict Block Input Shape Error for RNN

2 次查看(过去 30 天)
Hi all,
I am attempting to use a trained RNN model within Simulink using a TensorFlow Model Predict block for time series forecasting.
My model was trained using 2 timesteps and 4 features, therefore when I pass a numpy array of shape [1,2,4] to the following code I get a prediction for a single 'batch' within a standalone MATLAB script without error:
sequencesPy = py.numpy.array(sequences);
predictionsPy = model.predict(sequencesPy);
When referencing the same model within Simulink, I convert the model input signals to a numpy array of shape [1,2,4] using a preprocess.py script specified within the Model Predict block parameters, however when I attempt to run my model I get the following error:
== START OF PYTHON ERROR MESSAGE ==
Python Error: ValueError: Input 0 of layer "model_8" is incompatible with the layer: expected shape=(None, 2, 4), found shape=(2, 4)
== END OF PYTHON ERROR MESSAGE ==
However, my model appears to work when an array of [2,2,4] is passed from the preprocessing function.
Is there therefore a solution to stop my 0th dimension from being 'squeezed' out when being passed from my preprocess script to my TensorFlow model when it is equal to 1?
Thank you for your help!
  4 个评论
Nithin
Nithin 2025-2-11
Hi Jonathan,
It seems like the issue might not lie within the preprocess function itself. Instead, there could be a problem with the configuration of your Simulink model or the RNN model you're using. Given the information provided, it's challenging to pinpoint the exact cause.
P.S.: MATLAB itself isn't removing the batch dimension; the issue might be occurring elsewhere in the process.
Jonathan
Jonathan 2025-2-11
Hi Nithin,
Thank you for your response. Are there any other settings I should look into with regards to the Simulink model? When the rest of my model is removed, I still appear to get the same error message:
Also, when running the preprocess function & RNN Model in isolation within a MATLAB script (to proudce an array of shape (1,2,4)) it appears to work:
Please let me know if I can provide any more details to help assist!

请先登录,再进行评论。

回答(1 个)

Gayathri
Gayathri 2025-5-14
编辑:Gayathri 2025-5-14
The error you are encountering is due to the fact that the "preprocess" function is returning the output as an array. But it is expected to output a python list containing the output array. The python list should be a list of length '1', because there is only one input to the tensorflow model.
Please return the array in the below format.
return [array]
Please refer to the below link for more information on the same:
I hope this helps resolve the issue!

类别

Help CenterFile Exchange 中查找有关 Deep Learning with GPU Coder 的更多信息

产品


版本

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by