-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathencoder.py
More file actions
50 lines (38 loc) · 1.33 KB
/
encoder.py
File metadata and controls
50 lines (38 loc) · 1.33 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
47
48
"""
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 Encoder(nn.Module):
"""
This is the q_theta (z|x) network. Given a data sample x q_theta
maps to the latent space x -> z.
For a VQ VAE, q_theta outputs parameters of a categorical distribution.
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(Encoder, self).__init__()
kernel = 4
stride = 2
self.conv_stack = nn.Sequential(
nn.Conv2d(in_dim, h_dim // 2, kernel_size=kernel,
stride=stride, padding=1),
nn.ReLU(),
nn.Conv2d(h_dim // 2, h_dim, kernel_size=kernel,
stride=stride, padding=1),
nn.ReLU(),
nn.Conv2d(h_dim, h_dim, kernel_size=kernel-1,
stride=stride-1, padding=1),
ResidualStack(
h_dim, h_dim, res_h_dim, n_res_layers),
# nn.Sigmoid() # 9.18
)
def forward(self, x):
return self.conv_stack(x)