Update model.py
Browse files
model.py
CHANGED
@@ -1,87 +1,80 @@
|
|
1 |
-
import torch
|
2 |
-
import torch.nn as nn
|
3 |
-
import torch.nn.functional as F
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
nn.
|
14 |
-
nn.
|
15 |
-
nn.ReLU(),
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
nn.
|
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 |
-
class
|
62 |
-
|
63 |
-
|
64 |
-
def __init__(self,
|
65 |
-
super().__init__(
|
66 |
-
self.
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
self.vae
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
def decode(self, z):
|
83 |
-
return self.vae.decode(z)
|
84 |
-
|
85 |
-
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
86 |
-
model = VAEModel.from_pretrained("BioMike/emoji-vae-init").to(device)
|
87 |
-
model.eval()
|
|
|
1 |
+
import torch
|
2 |
+
import torch.nn as nn
|
3 |
+
import torch.nn.functional as F
|
4 |
+
|
5 |
+
class BaseVAE(nn.Module):
|
6 |
+
def __init__(self, latent_dim=16):
|
7 |
+
super().__init__()
|
8 |
+
self.latent_dim = latent_dim
|
9 |
+
input_dim = 3 * 32 * 32
|
10 |
+
|
11 |
+
self.encoder = nn.Sequential(
|
12 |
+
nn.Linear(input_dim, 1024),
|
13 |
+
nn.ReLU(),
|
14 |
+
nn.Linear(1024, 512),
|
15 |
+
nn.ReLU(),
|
16 |
+
)
|
17 |
+
self.fc_mu = nn.Linear(512, latent_dim)
|
18 |
+
self.fc_logvar = nn.Linear(512, latent_dim)
|
19 |
+
|
20 |
+
self.decoder_input = nn.Linear(latent_dim, 512)
|
21 |
+
self.decoder = nn.Sequential(
|
22 |
+
nn.ReLU(),
|
23 |
+
nn.Linear(512, 1024),
|
24 |
+
nn.ReLU(),
|
25 |
+
nn.Linear(1024, input_dim),
|
26 |
+
nn.Sigmoid()
|
27 |
+
)
|
28 |
+
|
29 |
+
def encode(self, x):
|
30 |
+
x = x.view(x.size(0), -1)
|
31 |
+
x = self.encoder(x)
|
32 |
+
mu = self.fc_mu(x)
|
33 |
+
logvar = self.fc_logvar(x)
|
34 |
+
return mu, logvar
|
35 |
+
|
36 |
+
def reparameterize(self, mu, logvar):
|
37 |
+
std = torch.exp(0.5 * logvar)
|
38 |
+
eps = torch.randn_like(std)
|
39 |
+
return mu + eps * std
|
40 |
+
|
41 |
+
def decode(self, z):
|
42 |
+
x = self.decoder_input(z)
|
43 |
+
x = self.decoder(x)
|
44 |
+
x = x.view(-1, 3, 32, 32)
|
45 |
+
return x
|
46 |
+
|
47 |
+
def forward(self, x):
|
48 |
+
mu, logvar = self.encode(x)
|
49 |
+
z = self.reparameterize(mu, logvar)
|
50 |
+
recon = self.decode(z)
|
51 |
+
return recon, mu, logvar
|
52 |
+
|
53 |
+
|
54 |
+
class VAEConfig(PretrainedConfig):
|
55 |
+
model_type = "vae"
|
56 |
+
|
57 |
+
def __init__(self, latent_dim=16, **kwargs):
|
58 |
+
super().__init__(**kwargs)
|
59 |
+
self.latent_dim = latent_dim
|
60 |
+
|
61 |
+
class VAEModel(PreTrainedModel):
|
62 |
+
config_class = VAEConfig
|
63 |
+
|
64 |
+
def __init__(self, config):
|
65 |
+
super().__init__(config)
|
66 |
+
self.vae = BaseVAE(latent_dim=config.latent_dim)
|
67 |
+
self.post_init()
|
68 |
+
|
69 |
+
def forward(self, x):
|
70 |
+
return self.vae(x)
|
71 |
+
|
72 |
+
def encode(self, x):
|
73 |
+
return self.vae.encode(x)
|
74 |
+
|
75 |
+
def decode(self, z):
|
76 |
+
return self.vae.decode(z)
|
77 |
+
|
78 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
79 |
+
model = VAEModel.from_pretrained("BioMike/emoji-vae-init").to(device)
|
80 |
+
model.eval()
|
|
|
|
|
|
|
|
|
|
|
|
|
|