提交 e30150dd 编写于 作者: H HydrogenSulfate 提交者: chajchaj

replace where with min and max

上级 3ab9ace5
...@@ -1665,27 +1665,17 @@ def cross_entropy(input, ...@@ -1665,27 +1665,17 @@ def cross_entropy(input,
if input_dims - 1 == label_dims: if input_dims - 1 == label_dims:
label = paddle.unsqueeze(label, axis=axis) label = paddle.unsqueeze(label, axis=axis)
if in_dygraph_mode(): if in_dygraph_mode():
if not soft_label: if soft_label == False:
valid_label = paddle.cast( valid_label = paddle.cast(
label != ignore_index, dtype=label.dtype) * label label != ignore_index, dtype=label.dtype) * label
# TODO: Temporarily use paddle.nonzero instead of paddle.max label_min = paddle.min(valid_label)
# to detect and find out possible illegal label values label_max = paddle.max(valid_label)
if len(paddle.nonzero(valid_label < 0)) > 0: if label_min < 0:
invalid_label = paddle.gather_nd( raise ValueError("label should not out of bound, but got{}".
valid_label, paddle.nonzero(valid_label < 0)) format(label_min))
raise ValueError( if label_max >= input.shape[axis]:
"Target({}) is out of class_dimension's lower bound({})". raise ValueError("label should not out of bound, but got{}".
format(invalid_label[0], 0)) format(label_max))
# TODO: Temporarily use paddle.nonzero instead of paddle.max
# to detect and find out possible illegal label values
if len(paddle.nonzero(valid_label >= input.shape[axis])) > 0:
invalid_label = paddle.gather_nd(
valid_label,
paddle.nonzero(valid_label >= input.shape[axis]))
raise ValueError(
"Target({}) is out of class_dimension's upper bound({})".
format(invalid_label[0], input.shape[axis] - 1))
if core.is_compiled_with_npu(): if core.is_compiled_with_npu():
_, _, out = _C_ops.softmax_with_cross_entropy( _, _, out = _C_ops.softmax_with_cross_entropy(
input, label, 'soft_label', soft_label, 'ignore_index', input, label, 'soft_label', soft_label, 'ignore_index',
...@@ -1842,7 +1832,6 @@ def cross_entropy(input, ...@@ -1842,7 +1832,6 @@ def cross_entropy(input,
valid_label = paddle.multiply( valid_label = paddle.multiply(
paddle.cast( paddle.cast(
label != ignore_index, dtype=label.dtype), label) label != ignore_index, dtype=label.dtype), label)
ignore_weight_mask = paddle.cast((label != ignore_index), ignore_weight_mask = paddle.cast((label != ignore_index),
input.dtype) input.dtype)
if ignore_weight_mask.ndim > 1 and ignore_weight_mask.shape[ if ignore_weight_mask.ndim > 1 and ignore_weight_mask.shape[
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册