TensorFlow Model Predict Block Input Shape Error for RNN

7 views (last 30 days)
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 Comments
Nithin
Nithin on 11 Feb 2025
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 on 11 Feb 2025
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!

Sign in to comment.

Answers (1)

Gayathri
Gayathri on 14 May 2025
Edited: Gayathri on 14 May 2025
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!

Categories

Find more on Deep Learning with GPU Coder in Help Center and File Exchange

Products


Release

R2024a

Community Treasure Hunt

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

Start Hunting!