-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmodels.py
140 lines (112 loc) · 3.8 KB
/
models.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import torch
from torch import nn
class base_VAE(nn.Module):
def __init__(self,input_dim, latent_size):
super().__init__()
self.input_dim=input_dim
self.latent_size=latent_size
self.encoder = nn.Sequential(
nn.Linear(input_dim,512),
nn.LayerNorm(512),
nn.SiLU(),
nn.Dropout(p=0.1),
nn.Linear(512,128),
nn.LayerNorm(128),
nn.SiLU(),
nn.Dropout(p=0.1),
nn.Linear(128,latent_size),
)
self.encoder_var = nn.Sequential(
nn.Linear(input_dim,128),
nn.LayerNorm(128),
nn.Tanh(),
nn.Dropout(p=0.1),
nn.Linear(128,latent_size),
)
self.decoder = nn.Sequential(
nn.Linear(latent_size,128),
nn.LayerNorm(128),
nn.SiLU(),
nn.Dropout(p=0.1),
nn.Linear(128,512),
nn.LayerNorm(512),
nn.SiLU(),
nn.Dropout(p=0.1),
nn.Linear(512, input_dim)
)
self.decoder_var = nn.Sequential(
nn.Linear(latent_size,256),
nn.Tanh(),
nn.Linear(256, input_dim),
)
def reparameterize(self,mu,logvar):
mu.to(torch.float32)
logvar.to(torch.float32)
if self.training:
std=logvar.mul(0.5).exp()
eps=std.data.new(std.size()).normal_()
return eps.mul(std).add_(mu)
else:
return mu
def encode(self, x):
x = x.to(torch.float32)
mu = self.encoder(x.view(-1,self.input_dim))
logvar = self.encoder_var(x.view(-1,self.input_dim))
return mu, logvar
def decode(self,z):
mu = self.decoder(z)#.view(-1,1,self.input_dim)#.view(-1,2,self.input_dim)
logvar = self.decoder_var(z)#.view(-1,1,self.input_dim)
#mu=mu_logvar[:,0,:]
#logvar=mu_logvar[:,1,:]
return mu, logvar
class prop_ls_NN(nn.Module):
def __init__(self, latent_size, prop_size, extra_size):
super().__init__()
#define a few variables
self.latent_size=latent_size
self.prop_size=prop_size
self.extra_size=extra_size
#this generates a set of extra_size properties that will be concatenated with the actual properties
self.enhancer=nn.Sequential(
nn.Linear(prop_size, 128),
nn.LayerNorm(128),
nn.SiLU(),
nn.Linear(128,128),
nn.LayerNorm(128),
nn.SiLU(),
nn.Linear(128,extra_size),
nn.LayerNorm(extra_size)
)
#feedforward module that takes in the properties and outputs mean and logvar of the output
self.model=nn.Sequential(
nn.Linear(prop_size+extra_size,512),
nn.LayerNorm(512),
nn.SiLU(),
nn.Dropout(p=0.1),
nn.Linear(512,128),
nn.LayerNorm(128),
nn.SiLU(),
nn.Dropout(p=0.1),
nn.Linear(128,128),
nn.LayerNorm(128),
nn.SiLU(),
nn.Dropout(p=0.1),
nn.Linear(128,latent_size)
)
self.var=nn.Sequential(
nn.Linear(prop_size+extra_size,128),
nn.LayerNorm(128),
nn.Tanh(),
nn.Dropout(p=0.1),
nn.Linear(128,latent_size)
)
def forward(self,x):
x.to(torch.float32)
#compute enhanced set of properties
z=self.enhancer(x)
#concatenate with original properties
y=torch.cat((x,z),1)
#compute output
mu_p=self.model(y)
logvar_p = self.var(y)
return mu_p, logvar_p