TensorFlow Model Predict Block Input Shape Error for RNN

조회 수: 7 (최근 30일)
Jonathan
Jonathan 2025년 2월 8일
편집: Gayathri 2025년 5월 14일
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