-
Notifications
You must be signed in to change notification settings - Fork 22
/
CVAE.lua
81 lines (58 loc) · 2.74 KB
/
CVAE.lua
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
require 'torch'
require 'cunn'
require 'cudnn'
local CVAE = {}
function CVAE.get_encoder(nc, ndf, latent_variable_size)
local encoder = nn.Sequential()
encoder:add(nn.SpatialConvolution(nc, ndf, 4, 4, 2, 2, 1, 1))
encoder:add(nn.SpatialBatchNormalization(ndf))
encoder:add(nn.LeakyReLU(0.2, true))
encoder:add(nn.SpatialConvolution(ndf, ndf * 2, 4, 4, 2, 2, 1, 1))
encoder:add(nn.SpatialBatchNormalization(ndf * 2))
encoder:add(nn.LeakyReLU(0.2, true))
encoder:add(nn.SpatialConvolution(ndf * 2, ndf * 4, 4, 4, 2, 2, 1, 1))
encoder:add(nn.SpatialBatchNormalization(ndf * 4))
encoder:add(nn.LeakyReLU(0.2, true))
encoder:add(nn.SpatialConvolution(ndf * 4, ndf * 8, 4, 4, 2, 2, 1, 1))
encoder:add(nn.SpatialBatchNormalization(ndf * 8))
encoder:add(nn.LeakyReLU(0.2, true))
encoder:add(nn.SpatialConvolution(ndf * 8, ndf * 8, 4, 4, 2, 2, 1, 1))
encoder:add(nn.SpatialBatchNormalization(ndf * 8))
encoder:add(nn.View(ndf * 8 * 4 * 4))
mean_logvar = nn.ConcatTable()
mean_logvar:add(nn.Linear(ndf * 8 * 4 * 4, latent_variable_size))
mean_logvar:add(nn.Linear(ndf * 8 * 4 * 4, latent_variable_size))
encoder:add(mean_logvar)
return encoder:cuda()
end
function CVAE.get_decoder(nc, ngf, latent_variable_size)
local decoder = nn.Sequential()
decoder:add(nn.Linear(latent_variable_size, ngf * 8 *2 * 4 * 4))
decoder:add(nn.ReLU(true))
decoder:add(nn.View(ngf * 8 * 2, 4, 4))
decoder:add(nn.SpatialUpSamplingNearest(2))
decoder:add(nn.SpatialReplicationPadding(1, 1, 1, 1))
decoder:add(cudnn.SpatialConvolution(ngf*8*2, ngf*8, 3, 3, 1, 1))
decoder:add(cudnn.SpatialBatchNormalization(ngf*8,1e-3))
decoder:add(nn.LeakyReLU(0.2, true))
decoder:add(nn.SpatialUpSamplingNearest(2))
decoder:add(nn.SpatialReplicationPadding(1, 1, 1, 1))
decoder:add(cudnn.SpatialConvolution(ngf*8, ngf*4, 3, 3, 1, 1))
decoder:add(cudnn.SpatialBatchNormalization(ngf*4,1e-3))
decoder:add(nn.LeakyReLU(0.2, true))
decoder:add(nn.SpatialUpSamplingNearest(2))
decoder:add(nn.SpatialReplicationPadding(1, 1, 1, 1))
decoder:add(cudnn.SpatialConvolution(ngf*4, ngf*2, 3, 3, 1, 1))
decoder:add(cudnn.SpatialBatchNormalization(ngf*2,1e-3))
decoder:add(nn.LeakyReLU(0.2, true))
decoder:add(nn.SpatialUpSamplingNearest(2))
decoder:add(nn.SpatialReplicationPadding(1, 1, 1, 1))
decoder:add(cudnn.SpatialConvolution(ngf*2, ngf*1, 3, 3, 1, 1))
decoder:add(cudnn.SpatialBatchNormalization(ngf*1,1e-3))
decoder:add(nn.LeakyReLU(0.2, true))
decoder:add(nn.SpatialUpSamplingNearest(2))
decoder:add(nn.SpatialReplicationPadding(1, 1, 1, 1))
decoder:add(cudnn.SpatialConvolution(ngf, nc, 3, 3, 1, 1))
return decoder:cuda()
end
return CVAE