loss.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. # Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved.
  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. import paddle
  15. import paddle.nn.functional as F
  16. __all__ = ['CELoss', 'JSDivLoss']
  17. class Loss(object):
  18. """
  19. Loss
  20. """
  21. def __init__(self, class_dim=1000, epsilon=None):
  22. assert class_dim > 1, "class_dim=%d is not larger than 1" % (class_dim)
  23. self._class_dim = class_dim
  24. if epsilon is not None and epsilon >= 0.0 and epsilon <= 1.0:
  25. self._epsilon = epsilon
  26. self._label_smoothing = True
  27. else:
  28. self._epsilon = None
  29. self._label_smoothing = False
  30. def _labelsmoothing(self, target):
  31. if target.shape[-1] != self._class_dim:
  32. one_hot_target = F.one_hot(target, self._class_dim)
  33. else:
  34. one_hot_target = target
  35. soft_target = F.label_smooth(one_hot_target, epsilon=self._epsilon)
  36. soft_target = paddle.reshape(soft_target, shape=[-1, self._class_dim])
  37. return soft_target
  38. def _crossentropy(self, input, target, use_pure_fp16=False):
  39. if self._label_smoothing:
  40. target = self._labelsmoothing(target)
  41. input = -F.log_softmax(input, axis=-1)
  42. cost = paddle.sum(target * input, axis=-1)
  43. else:
  44. cost = F.cross_entropy(input=input, label=target)
  45. if use_pure_fp16:
  46. avg_cost = paddle.sum(cost)
  47. else:
  48. avg_cost = paddle.mean(cost)
  49. return avg_cost
  50. def _kldiv(self, input, target, name=None):
  51. eps = 1.0e-10
  52. cost = target * paddle.log(
  53. (target + eps) / (input + eps)) * self._class_dim
  54. return cost
  55. def _jsdiv(self, input, target):
  56. input = F.softmax(input)
  57. target = F.softmax(target)
  58. cost = self._kldiv(input, target) + self._kldiv(target, input)
  59. cost = cost / 2
  60. avg_cost = paddle.mean(cost)
  61. return avg_cost
  62. def __call__(self, input, target):
  63. pass
  64. class CELoss(Loss):
  65. """
  66. Cross entropy loss
  67. """
  68. def __init__(self, class_dim=1000, epsilon=None):
  69. super(CELoss, self).__init__(class_dim, epsilon)
  70. def __call__(self, input, target, use_pure_fp16=False):
  71. cost = self._crossentropy(input, target, use_pure_fp16)
  72. return cost
  73. class JSDivLoss(Loss):
  74. """
  75. JSDiv loss
  76. """
  77. def __init__(self, class_dim=1000, epsilon=None):
  78. super(JSDivLoss, self).__init__(class_dim, epsilon)
  79. def __call__(self, input, target):
  80. cost = self._jsdiv(input, target)
  81. return cost