(X, Y, labels, args)
| 405 | |
| 406 | |
| 407 | def do_CL(X, Y, labels, args): |
| 408 | if args.CL_normalize: |
| 409 | X = F.normalize(X, dim=-1) |
| 410 | Y = F.normalize(Y, dim=-1) |
| 411 | |
| 412 | criterion = nn.CrossEntropyLoss() |
| 413 | B = X.size()[0] |
| 414 | logits = torch.mm(X, Y.transpose(1, 0)) # B*B |
| 415 | logits = torch.div(logits, 1) |
| 416 | # labels = torch.arange(B).long().to(logits.device) # B*1 |
| 417 | labels = labels.to(args.device) |
| 418 | CL_loss = criterion(logits, labels) |
| 419 | pred = logits.argmax(dim=1, keepdim=False) |
| 420 | CL_acc = pred.eq(labels).sum().detach().cpu().item() * 1. / B |
| 421 | |
| 422 | return CL_loss, CL_acc |
| 423 | |
| 424 | def sce_loss(x, y, alpha=3): |
| 425 | x = F.normalize(x, p=2, dim=-1) |
nothing calls this directly
no outgoing calls
no test coverage detected