Skip to content

Commit

Permalink
fix cross_entropy speed (#64211)
Browse files Browse the repository at this point in the history
  • Loading branch information
wangna11BD committed May 22, 2024
1 parent aef0f56 commit 79bbc2d
Showing 1 changed file with 2 additions and 3 deletions.
5 changes: 2 additions & 3 deletions python/paddle/nn/functional/loss.py
Original file line number Diff line number Diff line change
Expand Up @@ -2921,13 +2921,12 @@ def cross_entropy(
# 2. else
# numerator: loss's weighted sum
# denominator: cal the sum of weight where the sample's class_index!=ignore_index
is_ignore = label == ignore_index
mask = ~is_ignore
if paddle.count_nonzero(is_ignore) > 0: # ignore label
if ignore_index >= 0: # ignore label
out_sum = _C_ops.sum(out, [], None, False)
# for each label[i],set 1 or 0, according to ignore_index
# mask[i]=0, if label[i]==ignore_index
# mask[i]=1, otherwise
mask = label != ignore_index
if weight is None:
mask = paddle.cast(mask, dtype=out_sum.dtype)
count = _C_ops.sum(mask, [], None, False)
Expand Down

0 comments on commit 79bbc2d

Please sign in to comment.