- 
                Notifications
    
You must be signed in to change notification settings  - Fork 19.6k
 
Open
Description
I ran a simple lstm code as below on macos with torch backend. It showed error:
RuntimeError: Unable to automatically build the model. Please build it yourself before calling fit/evaluate/predict. A model is 'built' when its variables have been created and its `self.built` attribute is True. Usually, calling the model on a batch of data is the right way to build it.
Exception encountered:
'Exception encountered when calling LSTM.call().
'LSTMCell' object has no attribute 'recurrent_kernel'
Arguments received by LSTM.call():
  • sequences=torch.Tensor(shape=torch.Size([32, 10, 8]), dtype=float32)
  • initial_state=None
  • mask=None
  • training=False'
environment:
Python: 3.11.4
Torch version: 2.8.0
Keras version: 3.11.3
I tried also on macos with tensorflow backend which had no problem.  Also tried on linux with torch backend which was also good.
Is this a bug or my env problem?
Sample code:
import numpy as np
import os 
os.environ['KERAS_BACKEND'] = 'torch'
import keras
from keras import layers
import torch
print("Torch version:", torch.__version__)
print("Keras backend:", keras.backend.backend())
print("Keras version:", keras.__version__)
# Generate dummy sequential data
x_train = np.random.random((1000, 10, 8))  # 1000 samples, 10 timesteps, 8 features
y_train = np.random.randint(2, size=(1000, 1))  # Binary targets
# Build" LSTM model
model = keras.Sequential([
    layers.LSTM(32, input_shape=(10, 8)),
    layers.Dense(1, activation='sigmoid')
])
model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
# Train model
model.fit(x_train, y_train, epochs=5, batch_size=32)
# Evaluate model
x_test = np.random.random((100, 10, 8))
y_test = np.random.randint(2, size=(100, 1))
loss, acc = model.evaluate(x_test, y_test)
print(f"Test loss: {loss:.4f}, Test accuracy: {acc:.4f}")Metadata
Metadata
Assignees
Labels
No labels