-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbetaVAE.py
348 lines (271 loc) · 11.1 KB
/
betaVAE.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
import time
import copy
import gc
import os
from tqdm import tqdm
import numpy as np
import torch
import torch.nn as nn
from torch.nn import functional as F
from torch.cuda.amp import GradScaler, autocast
from sklearn.metrics import mean_squared_error
from types_ import *
from utils import *
class RNAEncoder(nn.Module):
def __init__(self,
in_channels: int,
hidden_dims: List):
super(RNAEncoder, self).__init__()
self.in_channels = in_channels
modules = [
nn.Sequential(nn.Dropout())]
# Build encoder
for h_dim in hidden_dims:
modules.append(
nn.Sequential(
nn.Linear(in_channels, h_dim),
nn.BatchNorm1d(h_dim),
nn.LeakyReLU()
)
)
in_channels = h_dim
self.encoder = nn.Sequential(*modules)
def forward(self, x):
return self.encoder(x)
class Decoder(nn.Module):
def __init__(self,
in_channels: int,
hidden_dims: int,
out_dims: int):
super(Decoder, self).__init__()
self.in_h = nn.Linear(in_channels, hidden_dims)
self.bn = nn.BatchNorm1d(hidden_dims)
self.relu = nn.LeakyReLU()
self.fc = nn.Linear(hidden_dims, out_dims)
def forward(self, x):
x = self.in_h(x)
x = self.bn(x)
x = self.relu(x)
x = self.fc(x)
return torch.tanh(x)
class betaVAE(nn.Module):
def __init__(self,
in_channels: int,
z_dim: int,
encoder_dims: List,
hidden_dims_decoder: List,
beta:int = 2,
encoder_checkpoint=None):
super(betaVAE, self).__init__()
self.encoder = RNAEncoder(in_channels, encoder_dims)
# if there are weights for the encoder
if encoder_checkpoint:
self.encoder.load_state_dict(torch.load(encoder_checkpoint))
self.z_mu = nn.Linear(z_dim, z_dim)
self.z_logvar = nn.Linear(z_dim, z_dim)
self.beta = beta
self.training = True
modules = []
in_ch = z_dim
for h_dim in hidden_dims_decoder:
modules.append(
nn.Sequential(
nn.Linear(in_ch, h_dim),
nn.BatchNorm1d(h_dim),
nn.LeakyReLU())
)
in_ch = h_dim
modules.append(nn.Sequential(nn.Linear(in_ch, in_channels), nn.Tanh()))
self.decoder = nn.Sequential(*modules)
#self.decoder = Decoder(z_dim, hidden_dims_decoder[0], in_channels)
self.z_dim = z_dim
def reparametrize(self, z_mean, z_log_var):
#z_log_var = torch.clip(z_log_var, min=-20, max=20)
std = torch.exp(0.5*z_log_var)
eps = torch.randn_like(std)
return z_mean + eps*std
def encode(self, x):
x_encoded = self.encoder(x)
z_mean = self.z_mu(x_encoded)
z_log_var = self.z_logvar(x_encoded)
return z_mean, z_log_var, x_encoded
def forward(self, x):
z_mean, z_log_var, x_encoded = self.encode(x)
z = self.reparametrize(z_mean, z_log_var)
out = self.decoder(z)
return out, z_mean, z_log_var
def sample(self,
num_samples:int,
current_device: int,
interpolation: Tensor = None,
alpha: float = 1.0) -> Tensor:
"""
Samples from the latent space and return the corresponding
gene expression.
:param num_samples: (Int) Number of samples
:param current_device: (Int) Device to run the model
:param interpolation: (Tensor) Difference to move samples in the latent space
:param alpha: (Tensor) Weight for moving in the latent space
:return: (Tensor)
"""
z = torch.randn(num_samples,
self.z_dim)
z = z.to(current_device)
if interpolation is not None:
z = z + torch.from_numpy(alpha * interpolation).float().to(current_device)
samples = self.decoder(z)
return samples
def decode(self, x):
return self.decoder(x)
def betaVAEloss(x, x_recons, z_mean, z_logvar, beta, kld_weight=0.005, training=True):
recons_loss =F.mse_loss(x_recons, x)
kld_loss = torch.mean(-0.5 * torch.sum(1 + z_logvar - z_mean ** 2 - z_logvar.exp(), dim = 1), dim = 0)
# applying the beta correction
if training:
# https://openreview.net/forum?id=Sy2fzU9gl
total_loss = recons_loss + beta * kld_loss
else:
total_loss = recons_loss
losses = {
'total_loss': total_loss,
'reconstruction_loss': recons_loss,
'kl_loss': kld_loss
}
return losses
def train_betaVAE(model, optimizer, dataloader,
save_dir='checkpoints/models/', device=None,
log_interval=100, summary_writer=None, num_epochs=100, scheduler=None,
verbose=True):
best_model_wts = copy.deepcopy(model.state_dict())
best_epoch = 0
best_loss = {}
best_loss['total_loss'] = np.inf
losses_array = {
'train': {'total_loss': [],
'reconstruction_loss': [],
'kl_loss': []},
'val': {'total_loss': [],
'reconstruction_loss': [],
'kl_loss': []}
}
#scaler = GradScaler()
global_summary_step = {'train': 0, 'val': 0}
for epoch in range(num_epochs):
print('Epoch {}/{}'.format(epoch, num_epochs - 1))
print('-' * 10)
sizes = {'train': 0, 'val': 0}
inputs_seen = {'train': 0, 'val': 0}
for phase in ['train', 'val']:
if phase == 'train':
model.train()
model.training = True
else:
model.eval()
model.training = False
running_loss = {
'total_loss': [],
'reconstruction_loss': [],
'kl_loss': []
}
summary_step = global_summary_step[phase]
# for logging tensorboard
last_running_loss = {
'total_loss': 0.0,
'reconstruction_loss': 0.0,
'kl_loss': 0.0
}
for b_idx, batch in tqdm(enumerate(dataloader[phase])):
if torch.cuda.is_available():
batch['rna_data'] = batch['rna_data'].cuda()
optimizer.zero_grad(set_to_none=True)
#with autocast():
with torch.set_grad_enabled(phase=='train'):
outputs, z_mean, z_log_var = model(batch['rna_data'])
losses = betaVAEloss(batch['rna_data'], outputs, z_mean, z_log_var, model.beta, training=model.training)
if phase == 'train':
#scaler.scale(losses['total_loss']).backward()
losses['total_loss'].backward()
#scaler.step(optimizer)
optimizer.step()
if scheduler:
scheduler.step()
#scaler.update()
summary_step += 1
for key in ['total_loss', 'reconstruction_loss', 'kl_loss']:
running_loss[key].append(losses[key].detach().item())
sizes[phase] += 1
inputs_seen[phase] += batch['rna_data'].size(0)
# Emptying memory
del outputs, z_mean, z_log_var, losses
torch.cuda.empty_cache()
if (summary_step % log_interval == 0 and summary_writer is not None):
for key in ['total_loss', 'reconstruction_loss', 'kl_loss']:
loss_to_log = (np.mean(running_loss[key]) - last_running_loss[key])
summary_writer.add_scalar("{}/{}".format(phase, key), loss_to_log, summary_step)
last_running_loss[key] = np.mean(running_loss[key])
inputs_seen[phase] = 0.0
global_summary_step[phase] = summary_step
epoch_loss = {}
for key in ['total_loss', 'reconstruction_loss', 'kl_loss']:
epoch_loss[key] = np.mean(running_loss[key])
losses_array[phase][key].append(epoch_loss[key])
if verbose:
print('{} Total Loss: {:.4f} | Reconstruction Loss: {:.4f} | KL Loss: {:.4f}'.format(
phase, epoch_loss['total_loss'], epoch_loss['reconstruction_loss'],
epoch_loss['kl_loss']))
if phase == 'val' and epoch_loss['total_loss'] < best_loss['total_loss']:
best_loss['total_loss'] = epoch_loss['total_loss']
torch.save(model.state_dict(), os.path.join(save_dir, 'model_dict_best.pt'))
best_epoch = epoch
torch.save(model.state_dict(), os.path.join(save_dir, 'model_last.pt'))
model.load_state_dict(torch.load(os.path.join(save_dir, 'model_dict_best.pt')))
results = {
'best_epoch': best_epoch,
'best_loss': best_loss
}
return model, results
def evaluate_betaVAE(model, dataloader, verbose=True):
model.eval()
model.training = False
sizes = 0
inputs_seen = 0
running_loss = {
'total_loss': [],
'reconstruction_loss': [],
'kl_loss': []
}
predictions = []
real = []
for b_idx, batch in tqdm(enumerate(dataloader)):
if torch.cuda.is_available():
batch['rna_data'] = batch['rna_data'].cuda()
#with autocast():
with torch.set_grad_enabled(False):
outputs, z_mean, z_log_var = model(batch['rna_data'])
losses = betaVAEloss(batch['rna_data'], outputs, z_mean, z_log_var, model.beta, training=model.training)
predictions.append(outputs.detach().cpu().numpy().tolist())
real.append(batch['rna_data'].detach().cpu().numpy().tolist())
for key in ['total_loss', 'reconstruction_loss', 'kl_loss']:
running_loss[key].append(losses[key].detach().item())
sizes += batch['rna_data'].size(0)
inputs_seen += batch['rna_data'].size(0)
test_loss = {
'total_loss': np.mean(running_loss['total_loss']),
'reconstruction_loss': np.mean(running_loss['reconstruction_loss']),
'kl_loss': np.mean(running_loss['kl_loss'])
}
if verbose:
print('Total Loss: {:.4f} | Reconstruction Loss: {:.4f} | KL Loss: {:.4f}'.format(
test_loss['total_loss'], test_loss['reconstruction_loss'],
test_loss['kl_loss']))
return test_loss, predictions, real
if __name__ == "__main__":
rna_encoder = RNAEncoder(56200,[4056,2048])
x = torch.rand((64,56200))
out = rna_encoder(x)
print(out.shape)
betavae = betaVAE(56200, 2048, [4056, 2048], [4056, 56200])
x = torch.rand((64, 56200))
out, z_mean, z_log_var = betavae(x)
losses = betavae.loss(x, out, z_mean, z_log_var)
print(losses)