-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGAN_MNIST.py
More file actions
63 lines (54 loc) · 1.98 KB
/
GAN_MNIST.py
File metadata and controls
63 lines (54 loc) · 1.98 KB
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
from torch import nn
class Generator(nn.Module):
def __init__(self, class_num, z_dim):
super().__init__()
self.input_height = 28
self.input_width = 28
self.input_dim = z_dim + class_num
self.output_dim = 1
#FC_block: input_dim -> 1024 -> 128*7*7
self.fc = nn.Sequential(
nn.Linear(self.input_dim, 1024),
nn.BatchNorm1d(1024),
nn.ReLU(),
nn.Linear(1024, 128 * (self.input_height // 4) * (self.input_width // 4)),
nn.BatchNorm1d(128 * (self.input_height // 4) * (self.input_width // 4)),
nn.ReLU(),
)
#deconv_block: 128*7*7 -> 64*14*14 -> 1*28*28
self.deconv = nn.Sequential(
nn.ConvTranspose2d(128, 64, 4, 2, 1),
nn.BatchNorm2d(64),
nn.ReLU(),
nn.ConvTranspose2d(64, self.output_dim, 4, 2, 1),
#nn.Tanh(),
nn.Sigmoid(),
)
def forward(self, X):
X = self.fc(X).view(-1, 128, (self.input_height // 4), (self.input_width // 4))
return self.deconv(X)
class Discriminator(nn.Module):
def __init__(self):
super().__init__()
#conv_block: 1*28*28 -> 64*14*14 -> 128*7*7 -> 256*3*3
self.conv = nn.Sequential(
nn.Conv2d(1, 64, 4, 2, 1),
nn.BatchNorm2d(64),
nn.LeakyReLU(0.2),
nn.Conv2d(64, 128, 4, 2, 1),
nn.BatchNorm2d(128),
nn.LeakyReLU(0.2),
nn.Conv2d(128, 256, 4, 2, 1),
nn.BatchNorm2d(256),
nn.LeakyReLU(0.2),
)
#FC_block: 256*3*3 -> 1024 -> 1
self.fc = nn.Sequential(
nn.Linear(256 * 3 * 3, 1024),
nn.BatchNorm1d(1024),
nn.LeakyReLU(0.2),
nn.Linear(1024, 1),
)
def forward(self, X):
X = self.conv(X).view(-1, 256 * 3 * 3)
return self.fc(X)