Spaces:
Sleeping
Sleeping
File size: 4,581 Bytes
e59f78e | 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 | import torch
import torch.nn as nn
import torch.nn.functional as F
# define a mlp encoder
# inputs: batch x num_genes (2446)
# outputs: batch x ecoding_dim
class Encoder(nn.Module):
def __init__(self, latent_dim, hidden_dims, num_genes=2446):
super().__init__()
layers = []
prev_dim = num_genes
for h_dim in hidden_dims:
layers.append(nn.Linear(prev_dim, h_dim))
layers.append(nn.BatchNorm1d(h_dim))
layers.append(nn.ELU())
layers.append(nn.Dropout(0.4))
prev_dim = h_dim
self.enc_net = nn.Sequential(*layers)
self.fc_mean = nn.Linear(prev_dim, latent_dim)
self.fc_std = nn.Linear(prev_dim, latent_dim)
def forward(self, x_t):
h = self.enc_net(x_t)
mean = self.fc_mean(h)
# Ensure minimum std to prevent posterior collapse
# Higher minimum (1e-3) prevents std from collapsing to near-zero
std = F.softplus(self.fc_std(h)) + 1e-3
return mean, std
# define a corresponding mlp decoder
# input: batch x ecoding_dim + rnn_hidden_dim
class Decoder(nn.Module):
def __init__(self, latent_dim, rnn_hidden_dim, hidden_dims, num_genes=2446):
super().__init__()
layers = []
prev_dim = latent_dim + rnn_hidden_dim
for h_dim in hidden_dims:
layers.append(nn.Linear(prev_dim, h_dim))
layers.append(nn.BatchNorm1d(h_dim))
layers.append(nn.ELU())
layers.append(nn.Dropout(0.4))
prev_dim = h_dim
layers.append(nn.Linear(prev_dim, num_genes))
self.dec_net = nn.Sequential(*layers)
def forward(self, z, h):
inps = torch.cat([z, h], dim=1)
return self.dec_net(inps)
# define a gru-based rssm
# input: batch x ecoding_dim at t=0
# output: batch x 2*encoding_dim at t = 1 to get the mean and standard deviation
class RSSM(nn.Module):
def __init__(self, latent_dim, rnn_hidden_dim):
super().__init__()
self.latent_dim = latent_dim
self.hidden_dim = rnn_hidden_dim
self.gru = nn.GRUCell(latent_dim, rnn_hidden_dim)
self.mlp = nn.Sequential(
nn.Linear(rnn_hidden_dim, rnn_hidden_dim),
nn.LayerNorm(rnn_hidden_dim),
nn.ELU(),
nn.Linear(rnn_hidden_dim, 2 * latent_dim)
)
# Better initialization: larger std prevents weak prior
# Use Xavier/Glorot initialization for better gradient flow
nn.init.xavier_uniform_(self.mlp[3].weight, gain=0.1)
nn.init.zeros_(self.mlp[3].bias)
def forward(self, prev_r, prev_h):
h_t_1 = self.gru(prev_r, prev_h)
prev_stats = self.mlp(h_t_1)
prev_mean, prev_std = torch.chunk(prev_stats, 2, dim=1)
prev_std = F.softplus(prev_std) + 1e-3
return h_t_1, prev_mean, prev_std
# create joint training architecture for dreamer
class CellDreamer(nn.Module):
def __init__(
self,
device,
latent_dim = 20,
rnn_dim = 64,
enc_hidden_dims = [128, 64, 32],
dec_hidden_dims = [32, 64, 128],
num_genes = 2446
):
super().__init__()
self.encoder = Encoder(latent_dim, enc_hidden_dims, num_genes)
self.decoder = Decoder(latent_dim, rnn_dim, dec_hidden_dims, num_genes)
self.rssm = RSSM(latent_dim, rnn_dim)
self.rnn_dim = rnn_dim
self.latent_dim = latent_dim
self.input_dim = num_genes
self.device = device
def reparametrize(self, mean, std):
eps = torch.randn_like(std)
return mean + eps * std
def forward(self, x_t):
post_mean, post_std = self.encoder(x_t)
z_t = self.reparametrize(post_mean, post_std)
h_prev = torch.zeros(x_t.size(0), self.rnn_dim).to(self.device)
h_next, velocity_mean, velocity_std = self.rssm(z_t, h_prev)
prior_next_mean = z_t + velocity_mean
prior_next_std = velocity_std
rec_x = self.decoder(z_t, h_next)
return {
"recon_x": rec_x,
"post_mean": post_mean,
"post_std": post_std,
"prior_next_mean": prior_next_mean,
"prior_next_std": prior_next_std,
"z_t": z_t,
"h_next": h_next
}
|