table_att_loss.py 3.5 KB
Newer Older
M
MissPenguin 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
# copyright (c) 2021 PaddlePaddle Authors. All Rights Reserve.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import paddle
from paddle import nn
from paddle.nn import functional as F

文幕地方's avatar
fix bug  
文幕地方 已提交
23

M
MissPenguin 已提交
24
class TableAttentionLoss(nn.Layer):
文幕地方's avatar
文幕地方 已提交
25
    def __init__(self, structure_weight, loc_weight, **kwargs):
M
MissPenguin 已提交
26 27 28 29 30 31 32 33 34
        super(TableAttentionLoss, self).__init__()
        self.loss_func = nn.CrossEntropyLoss(weight=None, reduction='none')
        self.structure_weight = structure_weight
        self.loc_weight = loc_weight

    def forward(self, predicts, batch):
        structure_probs = predicts['structure_probs']
        structure_targets = batch[1].astype("int64")
        structure_targets = structure_targets[:, 1:]
文幕地方's avatar
fix bug  
文幕地方 已提交
35 36
        structure_probs = paddle.reshape(structure_probs,
                                         [-1, structure_probs.shape[-1]])
M
MissPenguin 已提交
37 38
        structure_targets = paddle.reshape(structure_targets, [-1])
        structure_loss = self.loss_func(structure_probs, structure_targets)
文幕地方's avatar
fix bug  
文幕地方 已提交
39

M
MissPenguin 已提交
40
        structure_loss = paddle.mean(structure_loss) * self.structure_weight
文幕地方's avatar
fix bug  
文幕地方 已提交
41

M
MissPenguin 已提交
42 43
        loc_preds = predicts['loc_preds']
        loc_targets = batch[2].astype("float32")
文幕地方's avatar
fix bug  
文幕地方 已提交
44
        loc_targets_mask = batch[3].astype("float32")
M
MissPenguin 已提交
45 46
        loc_targets = loc_targets[:, 1:, :]
        loc_targets_mask = loc_targets_mask[:, 1:, :]
文幕地方's avatar
fix bug  
文幕地方 已提交
47 48
        loc_loss = F.mse_loss(loc_preds * loc_targets_mask,
                              loc_targets) * self.loc_weight
文幕地方's avatar
文幕地方 已提交
49 50 51 52 53 54 55 56 57

        total_loss = structure_loss + loc_loss
        return {
            'loss': total_loss,
            "structure_loss": structure_loss,
            "loc_loss": loc_loss
        }


文幕地方's avatar
文幕地方 已提交
58
class SLALoss(nn.Layer):
文幕地方's avatar
文幕地方 已提交
59
    def __init__(self, structure_weight, loc_weight, loc_loss='mse', **kwargs):
文幕地方's avatar
文幕地方 已提交
60
        super(SLALoss, self).__init__()
文幕地方's avatar
文幕地方 已提交
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
        self.loss_func = nn.CrossEntropyLoss(weight=None, reduction='mean')
        self.structure_weight = structure_weight
        self.loc_weight = loc_weight
        self.loc_loss = loc_loss
        self.eps = 1e-12

    def forward(self, predicts, batch):
        structure_probs = predicts['structure_probs']
        structure_targets = batch[1].astype("int64")
        structure_targets = structure_targets[:, 1:]

        structure_loss = self.loss_func(structure_probs, structure_targets)

        structure_loss = paddle.mean(structure_loss) * self.structure_weight

        loc_preds = predicts['loc_preds']
        loc_targets = batch[2].astype("float32")
        loc_targets_mask = batch[3].astype("float32")
        loc_targets = loc_targets[:, 1:, :]
        loc_targets_mask = loc_targets_mask[:, 1:, :]

        loc_loss = F.smooth_l1_loss(
            loc_preds * loc_targets_mask,
            loc_targets * loc_targets_mask,
            reduction='sum') * self.loc_weight

        loc_loss = loc_loss / (loc_targets_mask.sum() + self.eps)
        total_loss = structure_loss + loc_loss
        return {
            'loss': total_loss,
            "structure_loss": structure_loss,
            "loc_loss": loc_loss
        }