keras-team / keras

Deep Learning for humans
http://keras.io/
Apache License 2.0
62.05k stars 19.48k forks source link

keras 3.0.5 - Loss "classes" as strings interpreted incorrectly #19333

Closed lbortolotti closed 7 months ago

lbortolotti commented 8 months ago
import numpy as np
import keras

print(f"keras.__version__: {keras.__version__}"
      )

n_samples = 8

n_features = 3
n_targets = 2

train_x = np.random.random((n_samples, n_features))
train_y = np.random.random((n_samples, n_targets))

inputs = keras.layers.Input(shape=(n_features,))
outputs = keras.layers.Dense(n_targets)(inputs)
model = keras.Model(inputs=inputs, outputs=outputs)
model.summary()

model.compile(loss="MeanSquaredError",
              optimizer='adam')

print('Train...')
model.fit(train_x, train_y, epochs=1)

Throws

Traceback (most recent call last):
  File "repro.py", line 24, in <module>
    model.fit(train_x, train_y, epochs=1)
  File "venv\lib\site-packages\keras\src\utils\traceback_utils.py", line 123, in error_handler
    raise e.with_traceback(filtered_tb) from None
  File "venv\lib\site-packages\keras\src\losses\loss.py", line 28, in __init__
    self.name = name or auto_name(self.__class__.__name__)
tensorflow.python.framework.errors_impl.OperatorNotAllowedInGraphError: Using a symbolic `tf.Tensor` as a Python `bool` is not allowed. You can attempt the following resolutions to the problem: If you are running in Graph mode, use Eager execution mode or decorate this function with @tf.function. If you are using AutoGraph, you can try decorating this function with @tf.function. If that does not work, then you may be using an unsupported feature or your source code may not be visible to AutoGraph. See https://github.com/tensorflow/tensorflow/blob/master/tensorflow/python/autograph/g3doc/reference/limitations.md#access-to-source-code for more information.

If I pass loss = keras.losses.MeanSquaredError() (not as a string) to compile(), it works correctly. If I forget the parentheses, I also get the error above - suggesting that the string is being parsed incorrectly.

sachinprasadhs commented 7 months ago

Hi,

If you're passing a string you should pass name of the loss function, you can try either "mean_squared_error" , "mse" or "MSE"

loss: Loss function. May be a string (name of loss function)

Which is defined here https://github.com/keras-team/keras/blob/42a1535ed7d3d75711a11d295f58a2dc9a59fdae/keras/losses/losses.py#L1129

or any of the values from the exported namespace from below list.

https://github.com/keras-team/keras/blob/42a1535ed7d3d75711a11d295f58a2dc9a59fdae/keras/losses/losses.py#L1118-L1128

lbortolotti commented 7 months ago

That's what I'm doing at the moment, I opened the ticket as passing the class name as a string used to work in tf.keras, But if the new behaviour is expected, you can close the issue.

fchollet commented 7 months ago

Thanks for the report, this is now fixed.

google-ml-butler[bot] commented 7 months ago

Are you satisfied with the resolution of your issue? Yes No

sachinprasadhs commented 7 months ago

Reference commit: https://github.com/keras-team/keras/commit/70054103f88dec3e199a54ddbf27174169c66dff