MCPcopy Create free account
hub / github.com/TorchSSL/TorchSSL / vat_loss

Function vat_loss

models/vat/vat_utils.py:37–62  ·  view source on GitHub ↗
(model, ul_x, ul_y, xi=1e-6, eps=6, num_iters=1)

Source from the content-addressed store, hash-verified

35
36 return qlogq - qlogp
37def vat_loss(model, ul_x, ul_y, xi=1e-6, eps=6, num_iters=1):
38
39 # find r_adv
40
41 d = torch.Tensor(ul_x.size()).normal_()
42 for i in range(num_iters):
43 d = xi *_l2_normalize(d)
44 d = Variable(d.cuda(), requires_grad=True)
45
46 y_hat = model(ul_x + d)
47
48 delta_kl = kl_div_with_logit(ul_y.detach(), y_hat)
49 delta_kl.backward()
50
51 d = d.grad.data.clone().cpu()
52 model.zero_grad()
53
54 d = _l2_normalize(d)
55 d = Variable(d.cuda())
56 r_adv = eps *d
57 # compute lds
58
59 y_hat = model(ul_x + r_adv.detach())
60
61 delta_kl = kl_div_with_logit(ul_y.detach(), y_hat)
62 return delta_kl
63

Callers 1

trainMethod · 0.85

Calls 2

_l2_normalizeFunction · 0.85
kl_div_with_logitFunction · 0.85

Tested by

no test coverage detected