https://github.com/Roelof1986/Keras4Delphi---New-FULL-LSTM-Fix-Delphi-10.3-Rio-
Hello everyone,
I've been working extensively with Keras4Delphi and wanted to share a solid fix for using stateful=True in LSTM layers — something that normally leads to frustrating errors like:
ValueError: When using 'stateful=True' in a RNN, the batch size must be static. Found: (None, 4, 2)
After reverse-engineering how Keras handles batch_input_shape, I've built a working solution that lets you:
Use stateful LSTM models in Delphi without runtime shape errors
Define a static batch shape via class vars instead of fragile .SetItem(...) calls
Cleanly set stateful, return_sequences, and batch_input_shape using hardcoded defaults
Stack multiple LSTM layers (with only the first one being stateful, as recommended)
✅ Highlights:
Fully functional fix for stateful=True
Tested with TensorFlow 2.19 and Delphi 10.4
Shape is passed cleanly to Python using PyDict_SetItemString(...)
Uses class var overrides, e.g.:
TBase.UseStateful := True;
TBase.UseReturnSequences := True;
TBase.UseBatchInputShape := [2, 4, 2];
🔧 Example setup:
lstm1 := TLSTM.Create(32); // parameters pulled from class vars
model.Add(lstm1);
lstm2 := TLSTM.Create(64); // stateful := False, return_sequences := False
model.Add(lstm2);
🧠 Why this helps:
Keras4Delphi is a great bridge between Pascal and TensorFlow, but its current design struggles with RNN state handling. This fix brings full stability, predictable behavior, and makes LSTM-based time-series modeling in Delphi a reality.
🗂 Repo (MIT-licensed):
GitHub: keras4delphi-stateful-fix
---
Let me know if you're also working with RNNs or time-sequence data in Delphi — I'd love to share ideas.
Kind regards,
Roelof Emmerink
---