-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathtrain.py
126 lines (109 loc) · 4.61 KB
/
train.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
import os
import random
import time
import torch
import numpy as np
from models.model import Model
from utils import load_adj, EHRDataset, format_time, MultiStepLRScheduler
from metrics import evaluate_codes, evaluate_hf
def historical_hot(code_x, code_num, lens):
result = np.zeros((len(code_x), code_num), dtype=int)
for i, (x, l) in enumerate(zip(code_x, lens)):
result[i] = x[l - 1]
return result
if __name__ == '__main__':
seed = 6669
dataset = 'mimic4' # 'mimic3' or 'eicu'
task = 'h' # 'm' or 'h'
use_cuda = True
device = torch.device('cuda' if torch.cuda.is_available() and use_cuda else 'cpu')
code_size = 48
graph_size = 32
hidden_size = 150 # rnn hidden size
t_attention_size = 32
t_output_size = hidden_size
batch_size = 32
epochs = 200
random.seed(seed)
np.random.seed(seed)
torch.manual_seed(seed)
torch.cuda.manual_seed(seed)
dataset_path = os.path.join('data', dataset, 'standard')
train_path = os.path.join(dataset_path, 'train')
valid_path = os.path.join(dataset_path, 'valid')
test_path = os.path.join(dataset_path, 'test')
code_adj = load_adj(dataset_path, device=device)
code_num = len(code_adj)
print('loading train data ...')
train_data = EHRDataset(train_path, label=task, batch_size=batch_size, shuffle=True, device=device)
print('loading valid data ...')
valid_data = EHRDataset(valid_path, label=task, batch_size=batch_size, shuffle=False, device=device)
print('loading test data ...')
test_data = EHRDataset(test_path, label=task, batch_size=batch_size, shuffle=False, device=device)
test_historical = historical_hot(valid_data.code_x, code_num, valid_data.visit_lens)
task_conf = {
'm': {
'dropout': 0.45,
'output_size': code_num,
'evaluate_fn': evaluate_codes,
'lr': {
'init_lr': 0.01,
'milestones': [20, 30],
'lrs': [1e-3, 1e-5]
}
},
'h': {
'dropout': 0.0,
'output_size': 1,
'evaluate_fn': evaluate_hf,
'lr': {
'init_lr': 0.01,
'milestones': [2, 3, 20],
'lrs': [1e-3, 1e-4, 1e-5]
}
}
}
output_size = task_conf[task]['output_size']
activation = torch.nn.Sigmoid()
loss_fn = torch.nn.BCELoss()
evaluate_fn = task_conf[task]['evaluate_fn']
dropout_rate = task_conf[task]['dropout']
param_path = os.path.join('data', 'params', dataset, task)
if not os.path.exists(param_path):
os.makedirs(param_path)
model = Model(code_num=code_num, code_size=code_size,
adj=code_adj, graph_size=graph_size, hidden_size=hidden_size, t_attention_size=t_attention_size,
t_output_size=t_output_size,
output_size=output_size, dropout_rate=dropout_rate, activation=activation).to(device)
optimizer = torch.optim.Adam(model.parameters(), lr=0.01)
scheduler = MultiStepLRScheduler(optimizer, epochs, task_conf[task]['lr']['init_lr'],
task_conf[task]['lr']['milestones'], task_conf[task]['lr']['lrs'])
pytorch_total_params = sum(p.numel() for p in model.parameters() if p.requires_grad)
print(pytorch_total_params)
for epoch in range(epochs):
print('Epoch %d / %d:' % (epoch + 1, epochs))
model.train()
total_loss = 0.0
total_num = 0
steps = len(train_data)
st = time.time()
scheduler.step()
for step in range(len(train_data)):
optimizer.zero_grad()
code_x, visit_lens, divided, y, neighbors = train_data[step]
output = model(code_x, divided, neighbors, visit_lens).squeeze()
loss = loss_fn(output, y)
loss.backward()
optimizer.step()
total_loss += loss.item() * output_size * len(code_x)
total_num += len(code_x)
end_time = time.time()
remaining_time = format_time((end_time - st) / (step + 1) * (steps - step - 1))
print('\r Step %d / %d, remaining time: %s, loss: %.4f'
% (step + 1, steps, remaining_time, total_loss / total_num), end='')
train_data.on_epoch_end()
et = time.time()
time_cost = format_time(et - st)
print('\r Step %d / %d, time cost: %s, loss: %.4f' % (steps, steps, time_cost, total_loss / total_num))
valid_loss, f1_score = evaluate_fn(model, valid_data, loss_fn, output_size, test_historical)
torch.save(model.state_dict(), os.path.join(param_path, '%d.pt' % epoch))