-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
33 lines (22 loc) · 1.15 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
from typing import Tuple
import torch
def freeze(model: torch.nn.Module):
for param in model.parameters():
param.requires_grad = False
def clamp_weights(model: torch.nn.Module, range: Tuple[float, float] = (-0.01, 0.01)):
assert range[0] < range[1]
for param in model.parameters():
param.data.clamp_(range[0], range[1])
def gradient_penalty(netD: torch.nn.Module, real_data: torch.Tensor, fake_data: torch.Tensor, device: torch.device):
alpha = torch.rand(1, 1)
alpha = alpha.expand(real_data.size())
alpha = alpha.to(device)
interpolates = alpha * real_data + ((1 - alpha) * fake_data)
interpolates = interpolates.to(device)
interpolates = torch.autograd.Variable(interpolates, requires_grad=True)
disc_interpolates = netD(interpolates)
gradients = torch.autograd.grad(outputs=disc_interpolates, inputs=interpolates,
grad_outputs=torch.ones(disc_interpolates.size()).to(device),
create_graph=True, retain_graph=True, only_inputs=True)[0]
gradient_penalty = ((gradients.norm(2, dim=1) - 1) ** 2).mean()
return gradient_penalty