You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I recently upgraded from Keras 2 to Keras 3 and noticed some strange behavior when using a custom loss function with model.fit(). The model trains without throwing any errors, but the loss values during training stay almost constant across epochs, even when the model’s predictions are changing.
This problem doesn’t happen if I use a built-in loss function like mse, and the same custom loss function worked perfectly fine in Keras 2.
Steps to Reproduce
example that shows the issue:
import tensorflow as tf
from tensorflow import keras
import numpy as np
# Custom loss function
def custom_loss(y_true, y_pred):
diff = y_true - y_pred
return tf.reduce_mean(diff ** 2)
# Dummy dataset
X = np.random.rand(100, 10)
y = np.random.rand(100, 1)
# Simple model
model = keras.Sequential([
keras.layers.Dense(32, activation='relu', input_shape=(10,)),
keras.layers.Dense(1)
])
# Compile with custom loss
model.compile(optimizer='adam', loss=custom_loss)
# Fit the model
history = model.fit(X, y, epochs=10, verbose=1)
When I run this code, the loss value printed during training doesn’t change much:
Hi,
I recently upgraded from Keras 2 to Keras 3 and noticed some strange behavior when using a custom loss function with model.fit(). The model trains without throwing any errors, but the loss values during training stay almost constant across epochs, even when the model’s predictions are changing.
This problem doesn’t happen if I use a built-in loss function like mse, and the same custom loss function worked perfectly fine in Keras 2.
Steps to Reproduce
example that shows the issue:
When I run this code, the loss value printed during training doesn’t change much:
However, if I check the model predictions before and after training, I can see that they’re changing.
Expected Behavior
The loss values should decrease during training as the model learns.
Environment
Using built-in loss functions (e.g., keras.losses.MeanSquaredError()) works fine.
Could this be related to changes in graph execution or how custom loss functions are handled in Keras 3?
Let me know if you need any more details to debug this. Thanks!
The text was updated successfully, but these errors were encountered: