|
1 | 1 | import torch |
| 2 | +from torch.nn.parameter import Parameter |
2 | 3 | from torch import nn |
3 | 4 | from torch.nn import functional as F |
4 | 5 | from torch.autograd import Variable |
5 | | - |
| 6 | +import pdb |
| 7 | +from functional import reset_normal_param, LinearWeightNorm |
6 | 8 | # class Discriminator(nn.Module): |
7 | 9 | # def __init__(self, output_units = 10): |
8 | 10 | # super(Discriminator, self).__init__() |
|
22 | 24 | # x = self.fc2(x) |
23 | 25 | # return x if not feature else x_f |
24 | 26 |
|
25 | | -#class Discriminator(nn.Module): |
26 | | -# def __init__(self, input_dim = 28 ** 2, output_dim = 10): |
27 | | -# super(Discriminator, self).__init__() |
28 | | -# self.input_dim = input_dim |
29 | | -# self.layers = torch.nn.ModuleList([ |
30 | | -# nn.Linear(input_dim, 1000), |
31 | | -# nn.Linear(1000, 500), |
32 | | -# nn.Linear(500, 250)] |
33 | | -# ) |
34 | | -# self.bns = torch.nn.ModuleList([ |
35 | | -# nn.BatchNorm1d(1000, affine=False), |
36 | | -# nn.BatchNorm1d(500, affine=False), |
37 | | -# nn.BatchNorm1d(250, affine=True)] |
38 | | -# ) |
39 | | -# self.final = nn.Linear(250, output_dim) |
40 | | -# def forward(self, x, feature = False, cuda = False): |
41 | | -# x = x.view(-1, self.input_dim) |
42 | | -# noise = torch.randn(x.size()) * 0.3 if self.training else torch.Tensor([0]) |
43 | | -# if cuda: |
44 | | -# noise = noise.cuda() |
45 | | -# x = x + Variable(noise, requires_grad = False) |
46 | | -# for i in range(len(self.layers)): |
47 | | -# m = self.layers[i] |
48 | | -# bn = self.bns[i] |
49 | | -# x_f = F.leaky_relu(m(x)) |
50 | | -# noise = torch.randn(x_f.size()) * 0.5 if self.training else torch.Tensor([0]) |
51 | | -# if cuda: |
52 | | -# noise = noise.cuda() |
53 | | -# x = (x_f + Variable(noise, requires_grad = False)) |
54 | | -# if feature: |
55 | | -# return x_f |
56 | | -# return self.final(x) |
57 | | - |
58 | | - |
59 | | -# class Generator(nn.Module): |
60 | | -# def __init__(self, z_dim, output_dim = 28 ** 2): |
61 | | -# super(Generator, self).__init__() |
62 | | -# self.z_dim = z_dim |
63 | | -# self.fc1 = nn.Linear(z_dim, 500) |
64 | | -# self.bn1 = nn.BatchNorm1d(500, affine = False) |
65 | | -# self.fc2 = nn.Linear(500, 500) |
66 | | -# self.bn2 = nn.BatchNorm1d(500, affine = False) |
67 | | -# self.fc3 = nn.Linear(500, output_dim) |
68 | | -# def forward(self, batch_size, cuda = False): |
69 | | -# x = Variable(torch.rand(batch_size, self.z_dim), requires_grad = False) |
70 | | -# if cuda: |
71 | | -# x = x.cuda() |
72 | | -# x = F.softplus(self.bn1(self.fc1(x))) |
73 | | -# #x = F.softplus(self.bn2(self.fc2(x))) |
74 | | -# x = F.sigmoid(self.fc3(x)) |
75 | | -# return x |
76 | | - |
77 | 27 | class Discriminator(nn.Module): |
78 | | - def __init__(self, nc = 1, ndf = 64, output_units = 10): |
| 28 | + def __init__(self, input_dim = 28 ** 2, output_dim = 10): |
79 | 29 | super(Discriminator, self).__init__() |
80 | | - self.ndf = ndf |
81 | | - self.main = nn.Sequential( |
82 | | - # state size. (nc) x 28 x 28 |
83 | | - nn.Conv2d(nc, ndf, 4, 2, 3, bias=False), |
84 | | - nn.BatchNorm2d(ndf), |
85 | | - nn.LeakyReLU(0.2, inplace=True), |
86 | | - # state size. (ndf) x 16 x 16 |
87 | | - nn.Conv2d(ndf, ndf * 4, 4, 2, 1, bias=False), |
88 | | - nn.BatchNorm2d(ndf * 4), |
89 | | - nn.LeakyReLU(0.2, inplace=True), |
90 | | - # state size. (ndf*2) x 8 x 8 |
91 | | - nn.Conv2d(ndf * 4, ndf * 4, 4, 2, 1, bias=False), |
92 | | - nn.BatchNorm2d(ndf * 4), |
93 | | - nn.LeakyReLU(0.2, inplace=True), |
94 | | - # state size. (ndf*4) x 4 x 4 |
95 | | - nn.Conv2d(ndf * 4, ndf * 4, 4, 1, 0, bias=False), |
| 30 | + self.input_dim = input_dim |
| 31 | + self.layers = torch.nn.ModuleList([ |
| 32 | + LinearWeightNorm(input_dim, 1000), |
| 33 | + LinearWeightNorm(1000, 500), |
| 34 | + LinearWeightNorm(500, 250), |
| 35 | + LinearWeightNorm(250, 250), |
| 36 | + LinearWeightNorm(250, 250)] |
96 | 37 | ) |
97 | | - self.final = nn.Linear(ndf * 4, output_units, bias=False) |
| 38 | + self.final = LinearWeightNorm(250, output_dim, weight_scale=1) |
| 39 | + #for layer in self.layers: |
| 40 | + # reset_normal_param(layer, 0.1) |
| 41 | + #reset_normal_param(self.final, 0.1, 5) |
98 | 42 | def forward(self, x, feature = False, cuda = False): |
99 | | - x_f = self.main(x).view(-1, self.ndf * 4) |
100 | | - return x_f if feature else self.final(x_f) |
| 43 | + x = x.view(-1, self.input_dim) |
| 44 | + noise = torch.randn(x.size()) * 0.3 if self.training else torch.Tensor([0]) |
| 45 | + if cuda: |
| 46 | + noise = noise.cuda() |
| 47 | + x = x + Variable(noise, requires_grad = False) |
| 48 | + for i in range(len(self.layers)): |
| 49 | + m = self.layers[i] |
| 50 | + x_f = F.relu(m(x)) |
| 51 | + noise = torch.randn(x_f.size()) * 0.5 if self.training else torch.Tensor([0]) |
| 52 | + if cuda: |
| 53 | + noise = noise.cuda() |
| 54 | + x = (x_f + Variable(noise, requires_grad = False)) |
| 55 | + if feature: |
| 56 | + return x_f, self.final(x) |
| 57 | + return self.final(x) |
| 58 | + |
101 | 59 |
|
102 | 60 | class Generator(nn.Module): |
103 | | - def __init__(self, z_dim, ngf = 64, output_dim = 28 ** 2): |
| 61 | + def __init__(self, z_dim, output_dim = 28 ** 2): |
104 | 62 | super(Generator, self).__init__() |
105 | 63 | self.z_dim = z_dim |
106 | | - self.main = nn.Sequential( |
107 | | - # input is Z, going into a convolution |
108 | | - nn.ConvTranspose2d(z_dim, ngf * 4, 4, 1, 0, bias=False), |
109 | | - nn.BatchNorm2d(ngf * 4), |
110 | | - nn.ReLU(True), |
111 | | - # state size. (ngf*8) x 4 x 4 |
112 | | - nn.ConvTranspose2d(ngf * 4, ngf * 2, 4, 2, 1, bias=False), |
113 | | - nn.BatchNorm2d(ngf * 2), |
114 | | - nn.ReLU(True), |
115 | | - # state size. (ngf*4) x 8 x 8 |
116 | | - nn.ConvTranspose2d(ngf * 2, ngf, 4, 2, 1, bias=False), |
117 | | - nn.BatchNorm2d(ngf), |
118 | | - nn.ReLU(True), |
119 | | - # state size. (ngf*2) x 16 x 16 |
120 | | - nn.ConvTranspose2d(ngf, 1, 4, 2, 3, bias=False), |
121 | | - # state size. (ngf) x 32 x 32 |
122 | | - nn.Sigmoid() |
123 | | - ) |
| 64 | + self.fc1 = nn.Linear(z_dim, 500, bias = False) |
| 65 | + self.bn1 = nn.BatchNorm1d(500, affine = False, eps=1e-6, momentum = 0.5) |
| 66 | + self.fc2 = nn.Linear(500, 500, bias = False) |
| 67 | + self.bn2 = nn.BatchNorm1d(500, affine = False, eps=1e-6, momentum = 0.5) |
| 68 | + self.fc3 = LinearWeightNorm(500, output_dim, weight_scale = 1) |
| 69 | + self.bn1_b = Parameter(torch.zeros(500)) |
| 70 | + self.bn2_b = Parameter(torch.zeros(500)) |
| 71 | + nn.init.xavier_uniform(self.fc1.weight) |
| 72 | + nn.init.xavier_uniform(self.fc2.weight) |
| 73 | + #reset_normal_param(self.fc1, 0.1) |
| 74 | + #reset_normal_param(self.fc2, 0.1) |
| 75 | + #reset_normal_param(self.fc3, 0.1) |
124 | 76 | def forward(self, batch_size, cuda = False): |
125 | | - x = Variable(torch.rand(batch_size, self.z_dim, 1, 1), requires_grad = False, volatile = self.training) |
| 77 | + x = Variable(torch.rand(batch_size, self.z_dim), requires_grad = False, volatile = not self.training) |
126 | 78 | if cuda: |
127 | 79 | x = x.cuda() |
128 | | - return self.main(x) |
| 80 | + x = F.softplus(self.bn1(self.fc1(x)) + self.bn1_b) |
| 81 | + x = F.softplus(self.bn2(self.fc2(x)) + self.bn2_b) |
| 82 | + x = F.softplus(self.fc3(x)) |
| 83 | + return x |
| 84 | + |
| 85 | +#class Discriminator(nn.Module): |
| 86 | +# def __init__(self, nc = 1, ndf = 64, output_units = 10): |
| 87 | +# super(Discriminator, self).__init__() |
| 88 | +# self.ndf = ndf |
| 89 | +# self.main = nn.Sequential( |
| 90 | +# # state size. (nc) x 28 x 28 |
| 91 | +# nn.Conv2d(nc, ndf, 4, 2, 3, bias=False), |
| 92 | +# nn.BatchNorm2d(ndf), |
| 93 | +# nn.LeakyReLU(0.2, inplace=True), |
| 94 | +# # state size. (ndf) x 16 x 16 |
| 95 | +# nn.Conv2d(ndf, ndf * 4, 4, 2, 1, bias=False), |
| 96 | +# nn.BatchNorm2d(ndf * 4), |
| 97 | +# nn.LeakyReLU(0.2, inplace=True), |
| 98 | +# # state size. (ndf*2) x 8 x 8 |
| 99 | +# nn.Conv2d(ndf * 4, ndf * 4, 4, 2, 1, bias=False), |
| 100 | +# nn.BatchNorm2d(ndf * 4), |
| 101 | +# nn.LeakyReLU(0.2, inplace=True), |
| 102 | +# # state size. (ndf*4) x 4 x 4 |
| 103 | +# nn.Conv2d(ndf * 4, ndf * 4, 4, 1, 0, bias=False), |
| 104 | +# ) |
| 105 | +# self.final = nn.Linear(ndf * 4, output_units, bias=False) |
| 106 | +# def forward(self, x, feature = False, cuda = False): |
| 107 | +# x_f = self.main(x).view(-1, self.ndf * 4) |
| 108 | +# return x_f if feature else self.final(x_f) |
| 109 | + |
| 110 | +#class Generator(nn.Module): |
| 111 | +# def __init__(self, z_dim, ngf = 64, output_dim = 28 ** 2): |
| 112 | +# super(Generator, self).__init__() |
| 113 | +# self.z_dim = z_dim |
| 114 | +# self.main = nn.Sequential( |
| 115 | +# # input is Z, going into a convolution |
| 116 | +# nn.ConvTranspose2d(z_dim, ngf * 4, 4, 1, 0, bias=False), |
| 117 | +# nn.BatchNorm2d(ngf * 4), |
| 118 | +# nn.ReLU(True), |
| 119 | +# # state size. (ngf*8) x 4 x 4 |
| 120 | +# nn.ConvTranspose2d(ngf * 4, ngf * 2, 4, 2, 1, bias=False), |
| 121 | +# nn.BatchNorm2d(ngf * 2), |
| 122 | +# nn.ReLU(True), |
| 123 | +# # state size. (ngf*4) x 8 x 8 |
| 124 | +# nn.ConvTranspose2d(ngf * 2, ngf, 4, 2, 1, bias=False), |
| 125 | +# nn.BatchNorm2d(ngf), |
| 126 | +# nn.ReLU(True), |
| 127 | +# # state size. (ngf*2) x 16 x 16 |
| 128 | +# nn.ConvTranspose2d(ngf, 1, 4, 2, 3, bias=False), |
| 129 | +# # state size. (ngf) x 32 x 32 |
| 130 | +# nn.Sigmoid() |
| 131 | +# ) |
| 132 | +# def forward(self, batch_size, cuda = False): |
| 133 | +# x = Variable(torch.rand(batch_size, self.z_dim, 1, 1), requires_grad = False, volatile = not self.training) |
| 134 | +# if cuda: |
| 135 | +# x = x.cuda() |
| 136 | +# return self.main(x) |
0 commit comments