-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPhaseReductionNet.py
64 lines (55 loc) · 2.09 KB
/
PhaseReductionNet.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
56
57
58
59
60
61
62
63
64
import torch
class Encoder(torch.nn.Module):
def __init__(self,input_dim=2, hidden_dim=10, output_dim=2, phase=True):
self.e = 1.0e-10
self.phase = phase
super(Encoder, self).__init__()
self.model = torch.nn.Sequential(
torch.nn.Linear(input_dim, hidden_dim),
torch.nn.BatchNorm1d(hidden_dim),
torch.nn.ReLU(),
torch.nn.Linear(hidden_dim, hidden_dim),
torch.nn.BatchNorm1d(hidden_dim),
torch.nn.ReLU(),
torch.nn.Linear(hidden_dim, output_dim)
)
def forward(self, x):
y = self.model(x)
if self.phase:
r = torch.norm(y[:,:2]+self.e,dim=1)
r = r.tile((2,1)).T
z1 = y[:,:2]/r
z = torch.cat((z1,y[:,2:]),dim=1)
return z
else:
return y
class Decoder(torch.nn.Module):
def __init__(self,input_dim=2, hidden_dim=10, output_dim=2):
super(Decoder, self).__init__()
self.model = torch.nn.Sequential(
torch.nn.Linear(input_dim, hidden_dim),
torch.nn.BatchNorm1d(hidden_dim),
torch.nn.ReLU(),
torch.nn.Linear(hidden_dim, hidden_dim),
torch.nn.BatchNorm1d(hidden_dim),
torch.nn.ReLU(),
torch.nn.Linear(hidden_dim, output_dim)
#torch.nn.Linear(hidden_dim, 3),
#torch.nn.BatchNorm1d(3),
#torch.nn.ReLU(),
#torch.nn.Linear(3, output_dim)
)
def forward(self, x):
y = self.model(x)
return y
class LatentSteper(torch.nn.Module):
def __init__(self,zd=1):
super(LatentSteper, self).__init__()
self.theta = torch.nn.Parameter(torch.tensor(0.01))
self.lam = torch.nn.Parameter(torch.tensor([0.99]*zd))
def forward(self, input):
z0 = input[:,[0]] * torch.cos(self.theta) - input[:,[1]] * torch.sin(self.theta)
z1 = input[:,[0]] * torch.sin(self.theta) + input[:,[1]] * torch.cos(self.theta)
z2 = input[:,2:] * self.lam
z = torch.cat((z0,z1,z2),dim=1)
return z