Loss name not using names from the output dictionary

I am not sure if this is expected behavior or not, but when I have custom layer that returns dict of tensors then I would like to loss use those names because order could change if outputs change.

For example if I set my_layer_1_mse as a monitor variable in early stopping and change other outputs this will no longer work if out2 changes from my_layer_1_mse to e.g. my_layer_2_mse

I would like to know if it is possible to have loss and metrics be named out1_loss and out2_mse in this specific case. Or at least my_layer_out1_loss and my_layer_out2_loss

Another problem is that I would like for model outputs to follow same naming convention if possible when exporting model to, for example, to tfjs. Currently outputs will be named my_layer and my_layer_1 instead of out1 and out2 respectively.

Thank you for your help.

import tensorflow as tf


class Layer(tf.keras.layers.Layer):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)

    def call(self, inputs):
        batch_size = tf.shape(inputs)[0]
        return {"out1": tf.repeat(1, batch_size), "out2": tf.repeat(2, batch_size)}


def build_model():

    input = tf.keras.layers.Input(shape=(1,))
    out = Layer(name="my_layer")(input)

    return tf.keras.Model(
        inputs=input,
        outputs=out,
    )


model = build_model()

model.compile(
    optimizer=tf.keras.optimizers.Adam(),
    loss={"out1": "mse"},
    metrics={"out2": "mse"},
)

data = (
    # x
    [5, 5, 5, 5, 5, 5, 5, 5],
    # y
    {
        "out1": [5, 5, 5, 5, 5, 5, 5, 5],
        "out2": [5, 5, 5, 5, 5, 5, 5, 5],
    },
)


ds = tf.data.Dataset.from_tensor_slices(data)
ds = ds.batch(2)

model.fit(ds)
4/4 [==============================] - 0s 2ms/step - loss: 16.0000 - my_layer_loss: 16.0000 - my_layer_1_mse: 9.0000

Hi @anon78355200,

Sorry for the delayed response. As you defined a custom class that returns a dictionary, to match the dictionary keys with the models output, you need to set output names for the model explicitly.
So i have modified the build_model() function by setting output names for the model and specifying the loss and metric for each output explicitly in model.compile(.). The code outputs the loss name with user defined name:

Epoch 1/5 1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 419ms/step - loss: 0.0000e+00 - out1_mse: 0.0000e+00 - out2_mse: 0.0000e+00.

Please refer to the gist. Hope you are expecting the same output.

Thank You