forked from neale/Adversarial-Autoencoder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathencoders.py
91 lines (82 loc) · 3.11 KB
/
encoders.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
import numpy as np
from torch import nn
from torch.nn import functional as F
class CELEBAencoder(nn.Module):
def __init__(self, args):
super(CELEBAencoder, self).__init__()
self._name = 'celebaE'
self.shape = (64, 64, 3)
self.dim = args.dim
convblock = nn.Sequential(
nn.Conv2d(3, self.dim, 3, 2, padding=1),
nn.Dropout(p=0.3),
nn.LeakyReLU(),
nn.Conv2d(self.dim, 2 * self.dim, 3, 2, padding=1),
nn.Dropout(p=0.3),
nn.LeakyReLU(),
nn.Conv2d(2 * self.dim, 4 * self.dim, 3, 2, padding=1),
nn.Dropout(p=0.3),
nn.LeakyReLU(),
nn.Conv2d(4 * self.dim, 8 * self.dim, 3, 2, padding=1),
nn.Dropout(p=0.3),
nn.LeakyReLU(),
nn.Conv2d(8 * self.dim, 16 * self.dim, 3, 2, padding=1),
nn.Dropout(p=0.3),
nn.LeakyReLU(),
)
self.main = convblock
self.linear = nn.Linear(4*4*4*self.dim, self.dim)
def forward(self, input):
output = self.main(input)
output = output.view(-1, 4*4*4*self.dim)
output = self.linear(output)
return output.view(-1, self.dim)
class CIFARencoder(nn.Module):
def __init__(self, args):
super(CIFARencoder, self).__init__()
self._name = 'cifarE'
self.shape = (32, 32, 3)
self.dim = args.dim
convblock = nn.Sequential(
nn.Conv2d(3, self.dim, 3, 2, padding=1),
nn.Dropout(p=0.3),
nn.LeakyReLU(),
nn.Conv2d(self.dim, 2 * self.dim, 3, 2, padding=1),
nn.Dropout(p=0.3),
nn.LeakyReLU(),
nn.Conv2d(2 * self.dim, 4 * self.dim, 3, 2, padding=1),
nn.Dropout(p=0.3),
nn.LeakyReLU(),
)
self.main = convblock
self.linear = nn.Linear(4*4*4*self.dim, self.dim)
def forward(self, input):
output = self.main(input)
output = output.view(-1, 4*4*4*self.dim)
output = self.linear(output)
return output.view(-1, self.dim)
class MNISTencoder(nn.Module):
def __init__(self, args):
super(MNISTencoder, self).__init__()
self._name = 'mnistE'
self.shape = (1, 28, 28)
self.dim = args.dim
convblock = nn.Sequential(
nn.Conv2d(1, self.dim, 5, stride=2, padding=2),
nn.Dropout(p=0.3),
nn.ReLU(True),
nn.Conv2d(self.dim, 2*self.dim, 5, stride=2, padding=2),
nn.Dropout(p=0.3),
nn.ReLU(True),
nn.Conv2d(2*self.dim, 4*self.dim, 5, stride=2, padding=2),
nn.Dropout(p=0.3),
nn.ReLU(True),
)
self.main = convblock
self.output = nn.Linear(4*4*4*self.dim, self.dim)
def forward(self, input):
input = input.view(-1, 1, 28, 28)
out = self.main(input)
out = out.view(-1, 4*4*4*self.dim)
out = self.output(out)
return out.view(-1, self.dim)