TensorFlow Model Predict Block Input Shape Error for RNN

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

Hi Jonathan,
Could you provide some sample code that replicates the error? This would help me understand the data flow and assist you in debugging the issue.
Hi Nithin,
Below is an example of a preprocess.py script which creates an array of shape (1, 2, 4) within Simulink. I have replaced my input data with static values for easier debugging:
import numpy as np
import logging
# Configure the logging
logging.basicConfig(filename='output_log.txt', level=logging.DEBUG)
def preprocess(model, input_data):
# Create an array with shape (1, 2, 4)
array = np.zeros((1, 2, 4))
array[0, :, 0] = [1, 1]
array[0, :, 1] = [1, 1]
array[0, :, 2] = [1, 1]
array[0, :, 3] = [1, 1]
# Check the shape
logging.debug(f" Array shape: {array.shape}")
return array
Which results in the aforementioned error. However, when I change the size to (2, 2, 4), the model seems to work:
array = np.zeros((2, 2, 4))
array[:, :, 0] = np.array([[1, 1], [1, 1]])
array[:, :, 1] = np.array([[1, 1], [1, 1]])
array[:, :, 2] = np.array([[1, 1], [1, 1]])
array[:, :, 3] = np.array([[1, 1], [1, 1]])
Interestingly, I have also discovered that adding a 4th dimension also seems to fix the issue:
array = np.expand_dims(array, axis=0)
Some details of my RNN:
Layer (type) Output Shape Param # Connected to
==================================================================================================
input_12 (InputLayer) [(None, 2, 4)] 0 []
masking_11 (Masking) (None, 2, 4) 0 ['input_12[0][0]']
Thanks once again!
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.
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)

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

Asked:

on 8 Feb 2025

Edited:

on 14 May 2025

Community Treasure Hunt

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

Start Hunting!