-
Notifications
You must be signed in to change notification settings - Fork 0
/
ecg_transformer.py
208 lines (170 loc) · 8.67 KB
/
ecg_transformer.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Jul 10 14:49:17 2023
@author: hawkiyc
"""
#%%
'Import Libraries'
from params import *
from cnn_embedding import *
from pos_encoder import *
from self_attention_pooling import *
from decoder_masking import *
#%%
'TS-Transformer'
class ECGTransformer(nn.Module):
def __init__(self, d_input: tuple = d_input, emb_dim: int = emb_size,
emb_k_size: int = 3, emb_norm: bool = False,
max_seq_len: int = seq_length - 4, n_encoder_layers: int = 4,
n_decoder_layers: int = 4, n_heads: int = 8,
dropout_encoder: float = .1, dropout_decoder: float = .1,
dropout_pos_encoder: float = .1, dim_feedforward: int = 2048,
use_self_att_pool: bool = True, fc_drop: int = .2,
out_features: int = model_out, act: nn.Module = nn.ReLU()):
super().__init__()
'embedding layer'
ecg_embedding_layer = cnn_embedding(d_input[0],
emb_dim = emb_dim,
kernel_size = emb_k_size,
batch_norm = emb_norm,
act_fun = act)
rr_embedding_layer = cnn_embedding(d_input[1],
emb_dim = emb_dim,
kernel_size = emb_k_size,
batch_norm = emb_norm,
act_fun = act)
'positional embedding'
encoder_pos_emb = pos_encoder(emb_size = emb_dim,
drop_rate = dropout_pos_encoder,
max_seq_len = max_seq_len,)
decoder_pos_emb = pos_encoder(emb_size = emb_dim,
drop_rate = dropout_pos_encoder,
max_seq_len = max_seq_len,
for_decoder_input = True)
'transformer encoder input'
self.ecg_encoder_emb = nn.Sequential(ecg_embedding_layer,
encoder_pos_emb)
self.rr_encoder_emb = nn.Sequential(rr_embedding_layer,
encoder_pos_emb)
'transformer decoder input'
self.ecg_decoder_emb = nn.Sequential(ecg_embedding_layer,
decoder_pos_emb)
self.rr_decoder_emb = nn.Sequential(rr_embedding_layer,
decoder_pos_emb)
'layer norm'
norm = nn.LayerNorm(emb_dim)
'transformer encoder'
encoder_layer = nn.TransformerEncoderLayer(d_model = emb_dim,
nhead = n_heads,
dim_feedforward =
dim_feedforward,
dropout = dropout_encoder,
activation = act,
batch_first = True)
self.ecg_encoder = nn.TransformerEncoder(
encoder_layer, num_layers = n_encoder_layers, norm = norm)
self.rr_encoder = nn.TransformerEncoder(
encoder_layer, num_layers = n_encoder_layers, norm = norm)
'self attention pooling'
self.ecg_self_att_pool = SelfAttentionPooling(
input_dim = emb_dim ) if use_self_att_pool else nn.Identity()
self.rr_self_att_pool = SelfAttentionPooling(
input_dim = emb_dim ) if use_self_att_pool else nn.Identity()
self.use_self_att_pool = use_self_att_pool
'transformer decoder'
decoder_layer = nn.TransformerDecoderLayer(d_model = emb_dim,
nhead = n_heads,
dim_feedforward =
dim_feedforward,
dropout = dropout_decoder,
activation = act,
batch_first = True)
self.ecg_decoder = nn.TransformerDecoder(
decoder_layer, num_layers = n_decoder_layers, norm = norm)
self.rr_decoder = nn.TransformerDecoder(
decoder_layer, num_layers = n_decoder_layers, norm = norm)
'FC'
self.flatten = nn.Flatten()
self.fc_in = self.get_fc_in()
self.fc = nn.Sequential(nn.Linear(self.fc_in, 512),
act,
nn.Dropout(fc_drop),
nn.Linear(512, out_features))
def get_fc_in(self, tgt_ecg_mask = tgt_ecg_mask,
tgt_rr_mask = tgt_rr_mask,
memory_ecg_mask = src_ecg_mask,
memory_rr_mask = src_rr_mask,):
'input/src embedding'
pseudo_ecg = self.ecg_encoder_emb(Variable(
torch.ones(2,d_input[0], seq_length)))
pseudo_rr = self.rr_encoder_emb(Variable(
torch.ones(2,d_input[1], max_rr_seq)))
'get encoder output'
pseudo_ecg = self.ecg_encoder(pseudo_ecg)
pseudo_ecg = self.ecg_self_att_pool(pseudo_ecg)
pseudo_rr = self.rr_encoder(pseudo_rr)
pseudo_rr = self.rr_self_att_pool(pseudo_rr)
'tgt/positional embedding'
pseudo_tgt_ecg = self.ecg_decoder_emb(Variable(
torch.ones(2,d_input[0], seq_length)))
pseudo_tgt_rr = self.rr_decoder_emb(Variable(
torch.ones(2,d_input[1], max_rr_seq)))
'get decoder output'
pseudo_out_ecg = self.ecg_decoder(
tgt = pseudo_tgt_ecg, memory = pseudo_ecg, tgt_mask = tgt_ecg_mask,
memory_mask = None if self.use_self_att_pool else memory_ecg_mask)
pseudo_out_rr = self.rr_decoder(
tgt = pseudo_tgt_rr, memory = pseudo_rr, tgt_mask = tgt_rr_mask,
memory_mask = None if self.use_self_att_pool else memory_rr_mask)
pseudo_outputs = torch.cat([pseudo_out_ecg, pseudo_out_rr], dim = 1)
pseudo_outputs = self.flatten(pseudo_outputs)
return pseudo_outputs.data.view(2, -1).size(1)
def forward(self, ecg, rr, tgt_ecg_mask = tgt_ecg_mask,
tgt_rr_mask = tgt_rr_mask, memory_ecg_mask = src_ecg_mask,
memory_rr_mask = src_rr_mask,):
'input/src embedding'
ecg_src = self.ecg_encoder_emb(ecg)
rr_src = self.rr_encoder_emb(rr)
'get encoder output'
ecg_src = self.ecg_encoder(src = ecg_src)
ecg_src = self.ecg_self_att_pool(ecg_src)
rr_src = self.rr_encoder(src = rr_src)
rr_src = self.rr_self_att_pool(rr_src)
'tgt/positional embedding'
ecg_tgt = self.ecg_decoder_emb(ecg)
rr_tgt = self.rr_decoder_emb(rr)
'get decoder output'
ecg_decoder_output = self.ecg_decoder(
tgt = ecg_tgt, memory = ecg_src,
tgt_mask = tgt_ecg_mask.to(device, torch.float32),
memory_mask = None if self.use_self_att_pool
else memory_ecg_mask.to(device, torch.float32))
rr_decoder_output = self.rr_decoder(
tgt = rr_tgt, memory = rr_src,
tgt_mask = tgt_rr_mask.to(device, torch.float32),
memory_mask = None if self.use_self_att_pool
else memory_rr_mask.to(device, torch.float32))
outputs = torch.cat([ecg_decoder_output, rr_decoder_output], dim = 1)
outputs = self.flatten(outputs)
outputs = self.fc(outputs)
return outputs
#%%
"Check"
if __name__ == '__main__':
in_0 = Variable(torch.randn(batch_size * 2, d_input[0], seq_length))
in_1 = Variable(torch.randn(batch_size * 2, d_input[1], max_rr_seq))
out = Variable(torch.randn(batch_size * 2, model_out))
pseudo_set = TensorDataset(in_0, in_1, out)
pseudo_loader = DataLoader(dataset = pseudo_set,
batch_size = batch_size,
shuffle = False)
model = ECGTransformer(use_self_att_pool=False)
model.to(device)
model.eval()
with torch.no_grad():
for x0,x1,y in pseudo_loader:
x0,x1,y = x0.to(device), x1.to(device), y.to(device)
pseudo_y_hat = model(x0,x1)
pseudo_loss = loss_fn(pseudo_y_hat, y)
print(pseudo_loss.item())