MCPcopy Create free account
hub / github.com/NineAbyss/ZeroG / do_CL

Function do_CL

code/utils.py:407–422  ·  view source on GitHub ↗
(X, Y, labels, args)

Source from the content-addressed store, hash-verified

405
406
407def 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
424def sce_loss(x, y, alpha=3):
425 x = F.normalize(x, p=2, dim=-1)

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected