Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

loss tracker for multiple model outputs #18993

Open
james77777778 opened this issue Dec 26, 2023 · 4 comments
Open

loss tracker for multiple model outputs #18993

james77777778 opened this issue Dec 26, 2023 · 4 comments
Assignees
Labels
stat:awaiting keras-eng Awaiting response from Keras engineer type:feature The user is asking for a new feature.

Comments

@james77777778
Copy link
Contributor

Currently, only the total loss has been tracked and displayed.
Can we have more detailed information for each loss?

I have a workaround to recompute the loss using metrics, but it seems very inefficient.

@sachinprasadhs sachinprasadhs added type:feature The user is asking for a new feature. keras-team-review-pending Pending review by a Keras team member. labels Dec 26, 2023
@sampathweb
Copy link
Collaborator

@james77777778 - Just wanted to confirm I captured the issue correctly.

import keras
from keras import layers

inputs = layers.Input(shape=(2,))
out1 = layers.Dense(1, activation=None, name="out1")(inputs)
out2 = layers.Dense(1, activation='sigmoid', name="out2")(inputs)

model = keras.Model(inputs=inputs, outputs=[out1, out2])
model.compile("sgd", ["mse", "binary_crossentropy"])

import numpy as np

x = np.random.random((10, 2))
y1 = np.random.random((10, 1))
y2 = np.random.randint(0, 2, (10, 1))

model.fit(x, [y1, y2], epochs=2)

Keras 2 Output:

Epoch 1/2
1/1 [==============================] - 1s 720ms/step - loss: 1.7548 - out1_loss: 0.9954 - out2_loss: 0.7594
Epoch 2/2
1/1 [==============================] - 0s 9ms/step - loss: 1.6991 - out1_loss: 0.9401 - out2_loss: 0.7590

Keras 3 Output:

Epoch 1/2
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 277ms/step - loss: 1.7056
Epoch 2/2
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 58ms/step - loss: 1.6477

Unlike Keras2, Keras 3 doesn't output the individual losses and will need to be added via metrics.
@fchollet - WDYT? Should Keras3 also output individual losses?

@sampathweb sampathweb removed the keras-team-review-pending Pending review by a Keras team member. label Dec 31, 2023
@sampathweb
Copy link
Collaborator

sampathweb commented Dec 31, 2023

This is documented as one of the differences in Keras 2 vs Keras 3 compatibility issues doc #18467 -

  • When having multiple named outputs (for example named output_a and output_b, old tf.keras adds <output_a>_loss, <output_b>_loss and so on to metrics. Keras 3 doesn't add them to metrics and needs to be done them to the output metrics by explicitly providing them in metrics list of individual outputs.

@sampathweb
Copy link
Collaborator

So, the way forward is the explicitly add them to metrics unless @fchollet suggests otherwise -

model = keras.Model(inputs=inputs, outputs=[out1, out2])
model.compile("sgd", ["mse", "binary_crossentropy"], metrics=["mse", "binary_crossentropy"])

Epoch 1/2
1/1 ━━━━━━━━━━━━━━━━━━━━ 1s 752ms/step - loss: 1.6307 - out1_mse: 0.7819 - out2_binary_crossentropy: 0.8488
Epoch 2/2
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 41ms/step - loss: 1.5852 - out1_mse: 0.7376 - out2_binary_crossentropy: 0.8476

@james77777778
Copy link
Contributor Author

Hi @sampathweb

Yes, we can explicitly add them using metrics. However, this approach might be less efficient when the loss computation is expensive. It is also inconvenient to require the user to implement custom metrics.

For example, IoU loss and classification loss in an object detection model must be implemented both in Loss and Metric. The same computation occurs during training and when computing metrics.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stat:awaiting keras-eng Awaiting response from Keras engineer type:feature The user is asking for a new feature.
Projects
None yet
Development

No branches or pull requests

3 participants