-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel.py
47 lines (38 loc) · 1.25 KB
/
model.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
import torch.nn as nn
import torch.nn.functional as F
class Encoder(nn.Module):
def __init__(self, z=16):
super(Encoder, self).__init__()
self.z = z
self.encode = nn.Sequential(
nn.Conv2d(1, 16, 4, 1),
nn.LeakyReLU(0.1, inplace=True),
nn.Conv2d(16, 32, 4, 2),
nn.LeakyReLU(0.1, inplace=True),
nn.Conv2d(32, 64, 4, 2),
nn.LeakyReLU(0.1, inplace=True),
nn.Conv2d(64, z * 2, 4, 1)
)
def forward(self, inp):
inp = self.encode(inp)
inp = inp.view(-1, 2, self.z)
return inp
class Decoder(nn.Module):
def __init__(self, z=16):
super(Decoder, self).__init__()
self.z = z
self.decode = nn.Sequential(
nn.ConvTranspose2d(1, 64, 4, 1),
nn.LeakyReLU(0.1, inplace=True),
nn.ConvTranspose2d(64, 32, 4, 1),
nn.LeakyReLU(0.1, inplace=True),
nn.ConvTranspose2d(32, 16, 4, 1),
nn.LeakyReLU(0.1, inplace=True),
nn.ConvTranspose2d(16, 1, 4, 2),
nn.Sigmoid()
)
def forward(self, inp):
inp = inp.view(-1, 1, 4, 4)
inp = self.decode(inp)
inp = inp.view(-1, 1, 28, 28)
return inp