[Python] Tensorflow - validation metric does not show up, it gets the same name as the train...

Discussão em 'Python' iniciado por Stack, Junho 4, 2025.

  1. Stack

    Stack Membro Participativo

    I am using the model.compile(metrics=[MyMetric])

    I was wondering why I only see loss and val_loss, but only se my_metric and not val_my_metric after the evaluation at end of each epoch completes.

    I debugged therefore tensorflow code I see in trainer.py:fit() it is ok:

    [​IMG]

    but in CallbackList.on_epoch_end the dict is flattened using logs = python_utils.pythonify_logs(logs), therefore the keys from compile_metrics and val_compile_metrics are saved under the same key: [​IMG]

    Note that I did not change anything in the Trainer logic. I just subclassed the train and test_step, as documented on the web:

    def update_metrics(self, loss, f1, f2):
    # From Cholet's Keras docs:
    for metric in self.metrics:
    if metric.name == "loss":
    metric.update_state(loss)
    else:
    metric.update_state(f1, f2)

    def test_step(self, data):
    """

    Args:
    data (tf.Tensor): Input batch of shape [B, 2, H, W, C]

    Returns:
    dict: Dictionary containing the loss
    """
    f1, f2, p1, p2, z1, z2 = self(data, training=False)
    loss = self.compute_loss(p1, p2, z1, z2)

    self.update_metrics(loss, f1, f2)

    # Return metrics
    return {m.name: m.result() for m in self.metrics}

    Continue reading...

Compartilhe esta Página