-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
24 lines (19 loc) · 800 Bytes
/
utils.py
File metadata and controls
24 lines (19 loc) · 800 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import torch
import torch.nn as nn
def gradient_penalty(critic, real, fake, device="cpu"):
BATCH_SIZE, C, H, W = real.shape
epsilon = torch.rand((BATCH_SIZE, 1, 1, 1)).repeat(1, C, H, W).to(device)
interpolated_images = real * epsilon + fake * (1 - epsilon)
critic_output = critic(interpolated_images)
# gradients = ∂C(interpolated_images) / ∂(interpolated_images)
gradient = torch.autograd.grad(
inputs=interpolated_images,
outputs=critic_output,
grad_outputs=torch.ones_like(critic_output),
create_graph=True,
retain_graph=True,
)[0]
gradient = gradient.view(gradient.shape[0], -1)
gradient_norm = gradient.norm(2, dim=1)
gradient_penalty = torch.mean((gradient_norm - 1) ** 2)
return gradient_penalty