-
Notifications
You must be signed in to change notification settings - Fork 0
/
models.py
111 lines (92 loc) · 3.32 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
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.autograd import Variable
class Encoder(nn.Module):
def __init__(self, args):
super(Encoder, self).__init__()
self.args = args
self.convolution1 = nn.Sequential(nn.Conv2d(in_channels=self.args.channels, out_channels=64, kernel_size=4, stride=2, padding=1),
nn.BatchNorm2d(64),
nn.ReLU())
self.convolution2 = nn.Sequential(nn.Conv2d(in_channels=64, out_channels=128, kernel_size=4, stride=2, padding=1),
nn.BatchNorm2d(128),
nn.ReLU())
def forward(self, x):
x = self.convolution1(x)
x = self.convolution2(x)
return x
class Sampler(nn.Module):
def __init__(self, args):
super(Sampler, self).__init__()
self.args = args
self.z_mean = nn.Linear(128*7*7, self.args.zdim)
self.z_var = nn.Linear(128*7*7, self.args.zdim)
def sample_z(self, mean, logvar):
if self.training:
# the reparameterization trick
std = logvar.mul(0.5).exp_()
eps = torch.empty_like(std).normal_()
return eps.mul(std).add_(mean)
else:
return mean
def forward(self, x):
x = x.view(-1, 128*7*7)
mean = self.z_mean(x)
var = self.z_var(x)
z = self.sample_z(mean, var)
return z, mean, var
class Decoder(nn.Module):
def __init__(self, args):
super(Decoder, self).__init__()
self.args = args
self.expand = nn.Linear(self.args.zdim, 128*7*7)
self.deconv1 = nn.Sequential(nn.ConvTranspose2d(in_channels=128, out_channels=64, kernel_size=4, stride=2, padding=1),
nn.BatchNorm2d(64),
nn.ReLU())
self.deconv2 = nn.Sequential(nn.ConvTranspose2d(in_channels=64, out_channels=self.args.channels, kernel_size=4, stride=2, padding=1))
self.sigmoid = nn.Sigmoid()
def forward(self, z):
out = self.expand(z)
out = out.view(out.size(0), 64*2, 7, 7)
out = self.deconv1(out)
out = self.deconv2(out)
out = torch.sigmoid(out)
return out
class Classifier(nn.Module):
def __init__(self, args, out_c=10):
super(Classifier, self).__init__()
self.args = args
self.fc1 = nn.Linear(128*7*7, 10)
def forward(self, z):
out = self.fc1(z)
return out
class VAE(nn.Module):
def __init__(self, args):
super(VAE, self).__init__()
self.args = args
self.encoder = Encoder(self.args)
self.sampler = Sampler(self.args)
self.decoder = Decoder(self.args)
self.classifier = Classifier(self.args)
def display(self):
print(self)
def forward(self, x):
latent = self.encoder(x)
c = self.classifier(latent.view(-1, 128*7*7))
z, mean, var = self.sampler(latent)
out = self.decoder(z)
return out, c, mean, var
class CNN(nn.Module):
def __init__(self, args, out_c=10):
super(CNN, self).__init__()
self.args = args
self.extractor = Encoder(self.args)
self.classifier = Classifier(self.args)
def forward(self, x):
out = self.extractor(x)
out = out.view(-1, 128*7*7)
out = self.classifier(out)
return out
if __name__ == "__main__":
a = VAE().display()