-
Notifications
You must be signed in to change notification settings - Fork 2
/
basehelper.py
55 lines (41 loc) · 1.22 KB
/
basehelper.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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
from misc import *
class Zeronet(nn.Module):
def forward(self, x):
return torch.zeros_like(x)
zeronet = Zeronet()
class TVnorm(nn.Module):
def __init__(self):
super(TVnorm, self).__init__()
self.osize = 1
def forward(self, t, x, v):
return torch.norm(v, 1)
class NormAct(nn.Module):
def __init__(self, bound):
super().__init__()
self.bound = bound
self.relu = nn.ReLU()
self.elu = nn.ELU()
def forward(self, x):
x = x - self.bound + 1
x = self.relu(x) * self.elu(-x) + 1
return x
class Parameter(nn.Module):
def __init__(self, val, frozen=False):
super().__init__()
val = torch.Tensor(val)
self.val = val
self.param = nn.Parameter(val)
self.frozen = frozen
def forward(self):
if self.frozen:
self.val = self.val.to(self.param.device)
return self.val
else:
return self.param
def freeze(self):
self.val = self.param.detach().clone()
self.frozen = True
def unfreeze(self):
self.frozen = False
def __repr__(self):
return "val: {}, param: {}".format(self.val.cpu(), self.param.detach().cpu())