-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvae_model_physio.py
210 lines (159 loc) · 7.33 KB
/
vae_model_physio.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
import numpy as np
import torch
import torch.nn as nn
from VAE_physio import VAE
class VAE_base(nn.Module):
def __init__(self, target_dim, config, device):
super().__init__()
self.device = device
self.target_dim = target_dim
self.emb_time_dim = config["model"]["timeemb"]
self.emb_feature_dim = config["model"]["featureemb"]
self.is_unconditional = config["model"]["is_unconditional"]
self.target_strategy = config["model"]["target_strategy"]
self.emb_total_dim = self.emb_time_dim + self.emb_feature_dim
if self.is_unconditional == False:
self.emb_total_dim += 1 # for conditional mask
self.embed_layer = nn.Embedding(
num_embeddings=self.target_dim, embedding_dim=self.emb_feature_dim
)
config_vae = config["vae"]
config_vae["side_dim"] = self.emb_total_dim
input_dim = 1
self.VAE = VAE(config_vae, input_dim)
def time_embedding(self, pos, d_model=128):
pe = torch.zeros(pos.shape[0], pos.shape[1], d_model).to(self.device)
position = pos.unsqueeze(2)
div_term = 1 / torch.pow(
10000.0, torch.arange(0, d_model, 2).to(self.device) / d_model
)
pe[:, :, 0::2] = torch.sin(position * div_term)
pe[:, :, 1::2] = torch.cos(position * div_term)
return pe
def get_randmask(self, observed_mask):
rand_for_mask = torch.rand_like(observed_mask) * observed_mask
rand_for_mask = rand_for_mask.reshape(len(rand_for_mask), -1)
for i in range(len(observed_mask)):
sample_ratio = np.random.rand() # missing ratio
num_observed = observed_mask[i].sum().item()
num_masked = round(num_observed * sample_ratio)
rand_for_mask[i][rand_for_mask[i].topk(num_masked).indices] = -1
cond_mask = (rand_for_mask > 0).reshape(observed_mask.shape).float()
return cond_mask
def get_hist_mask(self, observed_mask, for_pattern_mask=None):
if for_pattern_mask is None:
for_pattern_mask = observed_mask
if self.target_strategy == "mix":
rand_mask = self.get_randmask(observed_mask)
cond_mask = observed_mask.clone()
for i in range(len(cond_mask)):
mask_choice = np.random.rand()
if self.target_strategy == "mix" and mask_choice > 0.5:
cond_mask[i] = rand_mask[i]
else: # draw another sample for histmask (i-1 corresponds to another sample)
cond_mask[i] = cond_mask[i] * for_pattern_mask[i - 1]
return cond_mask
def get_test_pattern_mask(self, observed_mask, test_pattern_mask):
return observed_mask * test_pattern_mask
def get_side_info(self, observed_tp, cond_mask):
B, K, L = cond_mask.shape
time_embed = self.time_embedding(observed_tp, self.emb_time_dim) # (B,L,emb)
time_embed = time_embed.unsqueeze(2).expand(-1, -1, K, -1)
feature_embed = self.embed_layer(
torch.arange(self.target_dim).to(self.device)
) # (K,emb)
feature_embed = feature_embed.unsqueeze(0).unsqueeze(0).expand(B, L, -1, -1)
side_info = torch.cat([time_embed, feature_embed], dim=-1) # (B,L,K,*)
side_info = side_info.permute(0, 3, 2, 1) # (B,*,K,L)
if self.is_unconditional == False:
side_mask = cond_mask.unsqueeze(1) # (B,1,K,L)
side_info = torch.cat([side_info, side_mask], dim=1)
return side_info
def calc_loss_valid(
self, observed_data, observed_data_interpolation,cond_mask, observed_mask, side_info
):
loss_sum = 0
loss = self.calc_loss(
observed_data, observed_data_interpolation, cond_mask, observed_mask, side_info
)
loss_sum += loss.detach()
return loss_sum
def calc_loss(
self, observed_data, observed_data_interpolation, cond_mask, observed_mask, side_info
):
total_input = self.set_input_to_vae(observed_data, observed_data_interpolation, cond_mask)
predicted, KL = self.VAE(total_input, side_info) # (B,K,L)
target_mask = cond_mask
residual = (observed_data_interpolation - predicted) * target_mask
num_eval = target_mask.sum()
loss = (residual ** 2).sum() / (num_eval if num_eval > 0 else 1)
loss_total = loss + KL/num_eval
return loss_total
def set_input_to_vae(self, observed_data, observed_data_interpolation, cond_mask):
total_input = (cond_mask * observed_data_interpolation).unsqueeze(1)
return total_input
def reconstruct(self, observed_data, observed_data_interpolation, cond_mask, side_info):
VAE_input = (cond_mask * observed_data_interpolation).unsqueeze(1)
predicted,_ = self.VAE(VAE_input, side_info)
return predicted
def forward(self, batch, is_train=1):
(
observed_data,
observed_data_interpolation,
observed_mask,
observed_tp,
gt_mask,
for_pattern_mask,
_,
) = self.process_data(batch)
if is_train == 0:
cond_mask = gt_mask
elif self.target_strategy != "random":
cond_mask = self.get_hist_mask(
observed_mask, for_pattern_mask=for_pattern_mask
)
else:
cond_mask = self.get_randmask(observed_mask)
side_info = self.get_side_info(observed_tp, cond_mask)
loss_func = self.calc_loss if is_train == 1 else self.calc_loss_valid
return loss_func(observed_data, observed_data_interpolation, cond_mask, observed_mask, side_info)
def evaluate(self, batch):
(
observed_data,
observed_data_interpolation,
observed_mask,
observed_tp,
gt_mask,
_,
cut_length,
) = self.process_data(batch)
with torch.no_grad():
cond_mask = gt_mask
target_mask = observed_mask - cond_mask
side_info = self.get_side_info(observed_tp, cond_mask)
predicted = self.reconstruct(observed_data, observed_data_interpolation, cond_mask, side_info)
return observed_data, target_mask, observed_mask, observed_tp, predicted
class VAE_Physio(VAE_base):
def __init__(self, config, device, target_dim=35):
super(VAE_Physio, self).__init__(target_dim, config, device)
def process_data(self, batch):
observed_data = batch["observed_data"].to(self.device).float()
observed_data_interpolation = batch["observed_data_interpolation"].to(self.device).float()
observed_mask = batch["observed_mask"].to(self.device).float()
observed_tp = batch["timepoints"].to(self.device).float()
gt_mask = batch["gt_mask"].to(self.device).float()
observed_data = observed_data.permute(0, 2, 1)
observed_data_interpolation = observed_data_interpolation.permute(0, 2, 1)
observed_mask = observed_mask.permute(0, 2, 1)
gt_mask = gt_mask.permute(0, 2, 1)
cut_length = torch.zeros(len(observed_data)).long().to(self.device)
for_pattern_mask = observed_mask
return (
observed_data,
observed_data_interpolation,
observed_mask,
observed_tp,
gt_mask,
for_pattern_mask,
cut_length,
)