table_att_loss.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. # copyright (c) 2021 PaddlePaddle Authors. All Rights Reserve.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. from __future__ import absolute_import
  15. from __future__ import division
  16. from __future__ import print_function
  17. import paddle
  18. from paddle import nn
  19. from paddle.nn import functional as F
  20. class TableAttentionLoss(nn.Layer):
  21. def __init__(self, structure_weight, loc_weight, **kwargs):
  22. super(TableAttentionLoss, self).__init__()
  23. self.loss_func = nn.CrossEntropyLoss(weight=None, reduction='none')
  24. self.structure_weight = structure_weight
  25. self.loc_weight = loc_weight
  26. def forward(self, predicts, batch):
  27. structure_probs = predicts['structure_probs']
  28. structure_targets = batch[1].astype("int64")
  29. structure_targets = structure_targets[:, 1:]
  30. structure_probs = paddle.reshape(structure_probs,
  31. [-1, structure_probs.shape[-1]])
  32. structure_targets = paddle.reshape(structure_targets, [-1])
  33. structure_loss = self.loss_func(structure_probs, structure_targets)
  34. structure_loss = paddle.mean(structure_loss) * self.structure_weight
  35. loc_preds = predicts['loc_preds']
  36. loc_targets = batch[2].astype("float32")
  37. loc_targets_mask = batch[3].astype("float32")
  38. loc_targets = loc_targets[:, 1:, :]
  39. loc_targets_mask = loc_targets_mask[:, 1:, :]
  40. loc_loss = F.mse_loss(loc_preds * loc_targets_mask,
  41. loc_targets) * self.loc_weight
  42. total_loss = structure_loss + loc_loss
  43. return {
  44. 'loss': total_loss,
  45. "structure_loss": structure_loss,
  46. "loc_loss": loc_loss
  47. }
  48. class SLALoss(nn.Layer):
  49. def __init__(self, structure_weight, loc_weight, loc_loss='mse', **kwargs):
  50. super(SLALoss, self).__init__()
  51. self.loss_func = nn.CrossEntropyLoss(weight=None, reduction='mean')
  52. self.structure_weight = structure_weight
  53. self.loc_weight = loc_weight
  54. self.loc_loss = loc_loss
  55. self.eps = 1e-12
  56. def forward(self, predicts, batch):
  57. structure_probs = predicts['structure_probs']
  58. structure_targets = batch[1].astype("int64")
  59. structure_targets = structure_targets[:, 1:]
  60. structure_loss = self.loss_func(structure_probs, structure_targets)
  61. structure_loss = paddle.mean(structure_loss) * self.structure_weight
  62. loc_preds = predicts['loc_preds']
  63. loc_targets = batch[2].astype("float32")
  64. loc_targets_mask = batch[3].astype("float32")
  65. loc_targets = loc_targets[:, 1:, :]
  66. loc_targets_mask = loc_targets_mask[:, 1:, :]
  67. loc_loss = F.smooth_l1_loss(
  68. loc_preds * loc_targets_mask,
  69. loc_targets * loc_targets_mask,
  70. reduction='sum') * self.loc_weight
  71. loc_loss = loc_loss / (loc_targets_mask.sum() + self.eps)
  72. total_loss = structure_loss + loc_loss
  73. return {
  74. 'loss': total_loss,
  75. "structure_loss": structure_loss,
  76. "loc_loss": loc_loss
  77. }