def forward(self, scores):
    diag_idx = [[i, i] for i in range(len(scores))]
    diagonal = paddle.gather_nd(scores, paddle.to_tensor(diag_idx)).unsqueeze(1)
    d1 = diagonal.expand_as(scores)
    d2 = paddle.transpose(d1, (1, 0)).expand_as(scores)
    cost_s = ((self.margin + scores) - d1).clip(min=0)
    cost_im = ((self.margin + scores) - d2).clip(min=0)
    mask = (paddle.eye(scores.shape[0]) < 0.5)
    cost_s = (cost_s * mask)
    cost_im = (cost_im * mask)
    if self.max_violation:
        cost_s = cost_s.max(1)
        cost_im = cost_im.max(0)
    return (cost_s.sum() + cost_im.sum())
def forward(self, scores):
    diag_idx = [[i, i] for i in range(len(scores))]
    diagonal = paddle.gather_nd(scores, paddle.to_tensor(diag_idx)).unsqueeze(1)
    d1 = diagonal.expand_as(scores)
    d2 = paddle.transpose(d1, (1, 0)).expand_as(scores)
    cost_s = ((self.margin + scores) - d1).clip(min=0)
    cost_im = ((self.margin + scores) - d2).clip(min=0)
    mask = (paddle.eye(scores.shape[0]) < 0.5)
    cost_s = (cost_s * mask)
    cost_im = (cost_im * mask)
    if self.max_violation:
        cost_s = cost_s.max(1)
        cost_im = cost_im.max(0)
    return (cost_s.sum() + cost_im.sum())