-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel.py
149 lines (122 loc) · 4.59 KB
/
model.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import jax.numpy as jnp
# Froms
from equinox.nn import Sequential, Linear, Lambda, Conv2d, ConvTranspose2d
from equinox import Module
from jax.nn import celu
from jax.random import split, normal
from jax import vmap
from einops import rearrange
from functools import partial
# Types
from jax.random import PRNGKeyArray
from jax import Array
from typing import Tuple
# Create layers from activation functions
Celu = Lambda(celu)
Tanh = Lambda(jnp.tanh)
Flatten = Lambda(partial(rearrange, pattern='c h w -> (c h w)'))
Split = Lambda(partial(rearrange, pattern='(p c) -> p c', p=2))
CreateGrid = Lambda(partial(rearrange, pattern='(c h w) -> c h w', c=512, h=2, w=2))
Unflatten = Lambda(partial(rearrange, pattern='(c h w) -> c h w', h=28, w=28))
# Variational autoencoder
# Encoder
class ConvolutionalEncoder(Sequential):
def __init__(self, key: PRNGKeyArray):
keys = split(key, 6)
super().__init__(
[
Conv2d(1, 32, kernel_size=(5, 5), stride=(1, 1), padding=(2, 2), key=keys[0]),
Celu,
Conv2d(32, 64, kernel_size=(5, 5), stride=(2, 2), padding=(2, 2), key=keys[1]),
Celu,
Conv2d(64, 128, kernel_size=(5, 5), stride=(2, 2), padding=(2, 2), key=keys[2]),
Celu,
Conv2d(128, 256, kernel_size=(5, 5), stride=(2, 2), padding=(2, 2), key=keys[3]),
Celu,
Conv2d(256, 512, kernel_size=(5, 5), stride=(2, 2), padding=(2, 2), key=keys[4]),
Celu,
Flatten,
Linear(2048, 4096, key=keys[5]),
Split,
]
)
# Decoder
class ConvolutionalDecoder(Sequential):
def __init__(self, key: PRNGKeyArray):
keys = split(key, 3)
super().__init__(
[
CreateGrid,
ConvTranspose2d(512, 256, kernel_size=(5, 5), stride=(2, 2), padding=(2, 2), output_padding=(1, 1), key=keys[0]),
Celu,
ConvTranspose2d(256, 128, kernel_size=(5, 5), stride=(2, 2), padding=(2, 2), output_padding=(1, 1), key=keys[1]),
Celu,
ConvTranspose2d(128, 64, kernel_size=(5, 5), stride=(2, 2), padding=(2, 2), output_padding=(1, 1), key=keys[2]),
Celu,
ConvTranspose2d(64, 32, kernel_size=(5, 5), stride=(2, 2), padding=(2, 2), output_padding=(1, 1), key=keys[3]),
Celu,
ConvTranspose2d(32, 1, kernel_size=(5, 5), stride=(1, 1), padding=(4, 4), key=keys[4]),
]
)
# VAE
class IWAE(Module):
encoder: ConvolutionalEncoder
decoder: ConvolutionalDecoder
def __init__(self, key: PRNGKeyArray):
enc_key, dec_key = split(key)
self.encoder = ConvolutionalEncoder(enc_key)
self.decoder = ConvolutionalDecoder(dec_key)
def __call__(self, x, K: int, key: PRNGKeyArray):
# Encode
mean, logvar = self.encoder(x)
# Sample
z = self.sample(mean, logvar, (K, *mean.shape), key)
# Decode
x = vmap(self.decoder)(z)
return x, z, mean, logvar
def sample(self, mean: Array, logvar: Array, shape: Tuple, key: PRNGKeyArray):
std = jnp.exp(0.5 * logvar)
eps = normal(key, shape=shape)
# Reparameterization trick
return eps * std + mean
class LinearEncoder(Sequential):
def __init__(self, key: PRNGKeyArray):
keys = split(key, 3)
super().__init__(
[
Flatten,
Linear(784, 200, key=keys[0]),
Tanh,
Linear(200, 200, key=keys[1]),
Tanh,
Linear(200, 100, key=keys[2]),
Split,
]
)
class LinearDecoder(Sequential):
def __init__(self, key: PRNGKeyArray):
keys = split(key, 2)
super().__init__(
[
Linear(50, 200, key=keys[0]),
Tanh,
Linear(200, 200, key=keys[1]),
Tanh,
Linear(200, 784, key=keys[2]),
Unflatten,
]
)
class LinearIWAE(IWAE):
encoder: LinearEncoder
decoder: LinearDecoder
def __init__(self, key: PRNGKeyArray):
enc_key, dec_key = split(key)
self.encoder = LinearEncoder(enc_key)
self.decoder = LinearDecoder(dec_key)
class ConvolutionalIWAE(IWAE):
encoder: ConvolutionalEncoder
decoder: ConvolutionalDecoder
def __init__(self, key: PRNGKeyArray):
enc_key, dec_key = split(key)
self.encoder = ConvolutionalEncoder(enc_key)
self.decoder = ConvolutionalDecoder(dec_key)