Skip to content

add_loss implementation bug? #21289

Closed
Closed
@franklin5

Description

@franklin5

I am reading this line not believing my eyes:

https://github.com/keras-team/keras/blob/master/keras/src/layers/layer.py#L1205-L1207


def add_loss(self, loss):
        ...
        # Eager only.
        losses = tree.flatten(loss)
        ...
        if backend.in_stateless_scope():
            scope = backend.get_stateless_scope()
            if scope.collect_losses:
                for x in losses:
                    scope.add_loss(loss)
                    self._loss_ids.add(id(loss))
        else:
            ...

loss is the input, losses are flattened, x is for looping losses, but scope.add_loss and _loss_ids.add are repeatedly adding loss, not x? it'd be wrong if loss is a list of scalar tensor, no?

unittests are not covering the loss as a list of scalar tensor cases, https://github.com/keras-team/keras/blob/master/keras/src/layers/layer_test.py#L540-L567

Metadata

Metadata

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions