-
Notifications
You must be signed in to change notification settings - Fork 2
/
GeneVAE.py
229 lines (180 loc) · 6.44 KB
/
GeneVAE.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
import torch
import pickle
import numpy as np
import pandas as pd
import torch.nn as nn
import torch.optim as optim
import torch.nn.functional as F
from utils import kld_loss, get_device
# ============================================================================
# Create a VAE encoder (ProfileVAE)
class GeneEncoder(nn.Module):
def __init__(
self,
input_size,
hidden_sizes,
latent_size,
activation_fn,
dropout
):
"""
input_size: number of gene columns (eg. 978)
hidden_sizes: number of neurons of stack dense layers
latent_size: size of the latent vector
activation_fn: activation function
dropout: dropout probabilites
"""
super(GeneEncoder, self).__init__()
self.input_size = input_size
self.hidden_sizes = hidden_sizes
self.latent_size = latent_size
self.activation_fn = activation_fn
self.dropout = [dropout] * len(self.hidden_sizes)
num_units = [self.input_size] + self.hidden_sizes
dense_layers = []
for index in range(1, len(num_units)):
dense_layers.append(nn.Linear(num_units[index-1], num_units[index]))
dense_layers.append(self.activation_fn)
if self.dropout[index-1] > 0.0:
dense_layers.append(nn.Dropout(p=self.dropout[index-1]))
self.encoding = nn.Sequential(*dense_layers)
self.encoding_to_mu = nn.Linear(self.hidden_sizes[-1], self.latent_size)
self.encoding_to_logvar = nn.Linear(self.hidden_sizes[-1], self.latent_size)
def forward(self, inputs):
"""
inputs: [batch_size, input_size]
returns:
mu: [batch_size, latent_size]
logvar: [batch_size, latent_size]
"""
projection = self.encoding(inputs)
mu = self.encoding_to_mu(projection)
logvar = self.encoding_to_logvar(projection)
return (mu, logvar)
# ============================================================================
# Create a VAE decoder (ProfileVAE)
class GeneDecoder(nn.Module):
def __init__(
self,
latent_size,
hidden_sizes,
output_size,
activation_fn,
dropout
):
"""
latent_size: size of the latent vector
hidden_sizes: number of neurons of stack dense layers
output_size: number of gene columns (eg. 978)
activation_fn: activation function
dropout: dropout probabilites
"""
super(GeneDecoder, self).__init__()
self.latent_size = latent_size
# Reverse the number of neurons of dense layers
hidden_sizes.reverse()
self.hidden_sizes = hidden_sizes
self.output_size = output_size
self.activation_fn = activation_fn
self.dropout = [dropout] * len(self.hidden_sizes)
num_units = [self.latent_size] + self.hidden_sizes + [self.output_size]
dense_layers = []
# Last layer does not use dropout but requires a sigmoid function
for index in range(1, len(num_units)-1):
dense_layers.append(nn.Linear(num_units[index-1], num_units[index]))
dense_layers.append(self.activation_fn)
#dense_layers.append(nn.BatchNorm1d(num_units[index]))
if self.dropout[index-1] > 0.0:
dense_layers.append(nn.Dropout(p=self.dropout[index-1]))
# Last layer
dense_layers.append(nn.Linear(num_units[-2], num_units[-1]))
#dense_layers.append(nn.Sigmoid())
self.decoding = nn.Sequential(*dense_layers)
def forward(self, latent_z):
"""
latent_z: [batch_size, latent_size]
returns:
reconstructed inputs: [batch_size, input_size]
"""
outputs = self.decoding(latent_z)
return outputs
# ============================================================================
# Create a VAE to extract features of gene expression
class GeneVAE(nn.Module):
def __init__(
self,
input_size,
hidden_sizes,
latent_size,
output_size,
activation_fn,
dropout
):
"""
input_size: number of gene columns (eg. 978)
latent_size: size of the latent vector
hidden_sizes: number of neurons of stack dense layers
output_size: number of gene columns (eg. 978)
activation_fn: activation function
dropout: dropout probability
"""
super(GeneVAE, self).__init__()
self.encoder = GeneEncoder(
input_size,
hidden_sizes,
latent_size,
activation_fn,
dropout
)
self.decoder = GeneDecoder(
latent_size,
hidden_sizes,
output_size,
activation_fn,
dropout
)
self.reconstruction_loss = nn.MSELoss(reduction='sum')
self.kld_loss = kld_loss
def reparameterize(self, mu, logvar):
"""
Apply reparameterization trick to obtain samples from latent space.
returns:
sampled Z from the latnet distribution
"""
return torch.randn_like(mu).mul_(torch.exp(0.5*logvar)).add_(mu)
def forward(self, inputs):
"""
inputs: [batch_size, input_size]
returns:
output samples: [batch_size, input_size]
"""
self.mu, self.logvar = self.encoder(inputs)
latent_z = self.reparameterize(self.mu, self.logvar)
outputs = self.decoder(latent_z)
return latent_z, outputs
def joint_loss(
self,
outputs,
targets,
alpha=0.5,
beta=1
):
"""
outputs: decoder outputs [batch_size, input_size]
targets: encoder inputs [batch_size, input_size]
alpha: L2 loss
beta: Scaling of the KLD in range [1, 100]
returns:
joint_loss, rec_loss, kld_loss
"""
rec_loss = self.reconstruction_loss(outputs, targets)
rec_loss = rec_loss.double().to(get_device())
kld_loss = self.kld_loss(self.mu, self.logvar)
joint_loss = alpha * rec_loss + (1 - alpha) * beta * kld_loss
return joint_loss, rec_loss, kld_loss
def load_model(self, path):
weights = torch.load(path, map_location=get_device())
#weights = torch.load(path)
self.load_state_dict(weights)
def save_model(self, path):
torch.save(self.state_dict(), path)