npairsloss.py 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. from __future__ import absolute_import
  2. from __future__ import division
  3. from __future__ import print_function
  4. import paddle
  5. class NpairsLoss(paddle.nn.Layer):
  6. def __init__(self, reg_lambda=0.01):
  7. super(NpairsLoss, self).__init__()
  8. self.reg_lambda = reg_lambda
  9. def forward(self, input, target=None):
  10. """
  11. anchor and positive(should include label)
  12. """
  13. features = input["features"]
  14. reg_lambda = self.reg_lambda
  15. batch_size = features.shape[0]
  16. fea_dim = features.shape[1]
  17. num_class = batch_size // 2
  18. #reshape
  19. out_feas = paddle.reshape(features, shape=[-1, 2, fea_dim])
  20. anc_feas, pos_feas = paddle.split(out_feas, num_or_sections=2, axis=1)
  21. anc_feas = paddle.squeeze(anc_feas, axis=1)
  22. pos_feas = paddle.squeeze(pos_feas, axis=1)
  23. #get simi matrix
  24. similarity_matrix = paddle.matmul(
  25. anc_feas, pos_feas, transpose_y=True) #get similarity matrix
  26. sparse_labels = paddle.arange(0, num_class, dtype='int64')
  27. xentloss = paddle.nn.CrossEntropyLoss()(
  28. similarity_matrix, sparse_labels) #by default: mean
  29. #l2 norm
  30. reg = paddle.mean(paddle.sum(paddle.square(features), axis=1))
  31. l2loss = 0.5 * reg_lambda * reg
  32. return {"npairsloss": xentloss + l2loss}