MCPcopy Create free account
hub / github.com/KeepTryingTo/Pytorch-GAN / gradient_penalty

Function gradient_penalty

fc-CGANCode/utils.py:29–49  ·  view source on GitHub ↗
(critic, real, fake, device)

Source from the content-addressed store, hash-verified

27
28
29def gradient_penalty(critic, real, fake, device):
30 BATCH_SIZE, C, H, W = real.shape
31 alpha = torch.rand((BATCH_SIZE, 1, 1, 1)).repeat(1, C, H, W).to(device)
32 interpolated_images = real * alpha + fake.detach() * (1 - alpha)
33 interpolated_images.requires_grad_(True)
34
35 # Calculate critic scores
36 mixed_scores = critic(interpolated_images)
37
38 # Take the gradient of the scores with respect to the images
39 gradient = torch.autograd.grad(
40 inputs=interpolated_images,
41 outputs=mixed_scores,
42 grad_outputs=torch.ones_like(mixed_scores),
43 create_graph=True,
44 retain_graph=True,
45 )[0]
46 gradient = gradient.view(gradient.shape[0], -1)
47 gradient_norm = gradient.norm(2, dim=1)
48 gradient_penalty = torch.mean((gradient_norm - 1) ** 2)
49 return gradient_penalty
50
51
52def save_checkpoint(model, optimizer, filename="my_checkpoint.pth.tar"):

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected