-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdecoder.py
More file actions
47 lines (35 loc) · 1.28 KB
/
decoder.py
File metadata and controls
47 lines (35 loc) · 1.28 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
"""
References codes: https://github.com/MishaLaskin/vqvae
"""
import torch
import torch.nn as nn
import torch.nn.functional as F
import numpy as np
from .residual import ResidualStack
class Decoder(nn.Module):
"""
This is the p_phi (x|z) network. Given a latent sample z p_phi
maps back to the original space z -> x.
Inputs:
- in_dim : the input dimension
- h_dim : the hidden layer dimension
- res_h_dim : the hidden dimension of the residual block
- n_res_layers : number of layers to stack
"""
def __init__(self, in_dim, h_dim, n_res_layers, res_h_dim):
super(Decoder, self).__init__()
kernel = 4
stride = 2
self.inverse_conv_stack = nn.Sequential(
nn.ConvTranspose2d(
in_dim, h_dim, kernel_size=kernel-1, stride=stride-1, padding=1),
ResidualStack(h_dim, h_dim, res_h_dim, n_res_layers),
nn.ConvTranspose2d(h_dim, h_dim // 2,
kernel_size=kernel, stride=stride, padding=1),
nn.ReLU(),
nn.ConvTranspose2d(h_dim//2, 3, kernel_size=kernel,
stride=stride, padding=1),
# nn.Sigmoid() # 9.17
)
def forward(self, x):
return self.inverse_conv_stack(x)