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

Update loss.py

上级 ee070fbd
...@@ -1804,8 +1804,15 @@ def cross_entropy(input, ...@@ -1804,8 +1804,15 @@ def cross_entropy(input,
"when weight is provided" "when weight is provided"
.format(input.shape[-1], weight.shape[-1])) .format(input.shape[-1], weight.shape[-1]))
weight_gather = paddle.gather_nd( valid_label = paddle.where(label == ignore_index,
weight, label) #trans weight from class to sample, shape:N paddle.zeros_like(label), label)
ignore_weight_mask = paddle.cast((label != ignore_index), input.dtype)
if ignore_weight_mask.ndim > 1 and ignore_weight_mask.shape[
-1] == 1:
ignore_weight_mask = paddle.squeeze(ignore_weight_mask, -1)
weight_gather = paddle.gather_nd(weight, valid_label)
weight_gather = paddle.multiply(weight_gather, ignore_weight_mask)
input_shape = list(label.shape) input_shape = list(label.shape)
weight_gather_reshape = reshape(weight_gather, shape=input_shape) weight_gather_reshape = reshape(weight_gather, shape=input_shape)
out = paddle.multiply(out, weight_gather_reshape, name=weight_name) out = paddle.multiply(out, weight_gather_reshape, name=weight_name)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册