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

Update MeanAbsoluteError loss reduction #20254

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions keras/src/losses/loss.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ def squeeze_or_expand_to_same_rank(x1, x2, expand_rank_1=True):
return x1, x2


def reduce_values(values, reduction="sum_over_batch_size"):
def reduce_values(values, reduction="sum_over_batch_size", sample_weight=None):
if (
reduction is None
or reduction == "none"
Expand All @@ -132,7 +132,12 @@ def reduce_values(values, reduction="sum_over_batch_size"):
):
return values
loss = ops.sum(values)
if reduction == "sum_over_batch_size":
if sample_weight is not None and reduction == "sum_over_batch_size":
loss /= ops.cast(
ops.sum(sample_weight),
loss.dtype,
)
elif reduction == "sum_over_batch_size":
loss /= ops.cast(
ops.prod(ops.convert_to_tensor(ops.shape(values), dtype="int32")),
loss.dtype,
Expand Down Expand Up @@ -169,7 +174,7 @@ def reduce_weighted_values(
values = values * sample_weight

# Apply reduction function to the individual weighted losses.
loss = reduce_values(values, reduction)
loss = reduce_values(values, reduction, sample_weight)
return loss


Expand Down
Loading