-
Notifications
You must be signed in to change notification settings - Fork 0
/
mlp_trainer.py
211 lines (170 loc) · 8.23 KB
/
mlp_trainer.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
import torch
import torch.nn.functional as F
import torch.nn as nn
import numpy as np
import random
import math
from matplotlib import pyplot as plt
from sklearn import metrics
from torch.utils.data import DataLoader
from dataset.torch_dataset import TorchDataset
from dataset.tumor_dataset import TumorDataset
from metrics.evaluate_cls import evaluate_multi_cls
import os.path as osp
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.nn import Parameter
class Model(nn.Module):
def __init__(self, **param_dict):
super(Model, self).__init__()
self.param_dict = param_dict
self.input_dim = self.param_dict['ft_dim']
self.out_dim = self.param_dict['label_num']
self.h_dim = self.param_dict['h_dim']
self.dropout_num = self.param_dict['dropout_num']
self.layer_num = self.param_dict['layer_num']
self.layer_list = nn.ModuleList()
for idx in range(self.layer_num):
in_size = self.h_dim
out_size = self.h_dim
if idx == 0:
in_size = self.input_dim
if idx == self.layer_num - 1:
out_size = self.out_dim
layer = nn.Linear(in_size, out_size)
self.layer_list.append(layer)
self.mlp_activation = nn.ELU()
def forward(self, node_ft):
H = node_ft
for idx in range(self.layer_num):
H = self.layer_list[idx](H)
if idx != self.layer_num - 1:
H = self.mlp_activation(H)
H = F.dropout(H, p=self.dropout_num)
H = F.log_softmax(H, dim=-1)
return H
model_save_dir = 'save_model_param'
current_path = osp.dirname(osp.realpath(__file__))
class Trainer(object):
def __init__(self, **param_dict):
self.param_dict = param_dict
self.setup_seed(self.param_dict['seed'])
self.device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
self.dataset = TumorDataset(
split_param=self.param_dict['split_param'],
ft_stand=self.param_dict['ft_stand']
)
self.dataset.generate_dataset_info()
self.param_dict.update(self.dataset.dataset_info)
# self.dataset.to_tensor(self.device)
self.file_name = __file__.split('/')[-1].replace('.py', '')
self.trainer_info = '{}_seed={}_batch={}'.format(self.file_name, self.param_dict['seed'], self.param_dict['batch_size'])
# self.save_model_path = osp.join(current_path, model_save_dir, self.trainer_info)
self.loss_op = torch.nn.NLLLoss()
self.build_model()
def build_model(self):
self.model = Model(**self.param_dict).to(self.device)
self.optimizer = torch.optim.Adam(self.model.parameters(), lr=self.param_dict['lr'])
self.best_res = None
self.min_dif = -1e10
def setup_seed(self, seed):
torch.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
np.random.seed(seed)
random.seed(seed)
torch.backends.cudnn.deterministic = True
def iteration(self, epoch, dataloader, is_training=False):
if is_training:
self.model.train()
else:
self.model.eval()
all_pred = []
all_label = []
all_loss = []
for ft_mat, label_mat in dataloader:
ft_mat = ft_mat.cuda().float()
label_mat = label_mat.cuda().long()
pred = self.model(ft_mat)
if is_training:
# print(pred.size(), label_mat.size())
c_loss = self.loss_op(pred, label_mat)
param_l2_loss = 0
param_l1_loss = 0
for name, param in self.model.named_parameters():
if 'bias' not in name:
param_l2_loss += torch.norm(param, p=2)
param_l1_loss += torch.norm(param, p=1)
param_l2_loss = self.param_dict['param_l2_coef'] * param_l2_loss
loss = c_loss + param_l2_loss
loss.backward()
self.optimizer.step()
self.optimizer.zero_grad()
all_loss.append(loss.detach().to('cpu').item())
max_value, max_pos = torch.max(pred, dim=1)
pred = max_pos.detach().to('cpu').numpy()
label_mat = label_mat.detach().to('cpu').numpy()
all_pred = np.hstack([all_pred, pred])
all_label = np.hstack([all_label, label_mat])
return all_pred, all_label, all_loss
def print_res(self, res_list, epoch):
train_acc, valid_acc, test_acc, metastatic_acc, train_macro_f1, \
valid_macro_f1, test_macro_f1, metastatic_macro_f1 = res_list
msg_log = 'Epoch: {:03d}, Acc Train: {:.4f}, Val: {:.4f}, Test: {:.4f}, Metastatic: {:.4f} ' \
'Macro F1 Train: {:.4f}, Val: {:.4f}, Test: {:.4f}, Metastatic: {:.4f}'.format(
epoch, train_acc, valid_acc, test_acc, metastatic_acc, train_macro_f1, valid_macro_f1, test_macro_f1,
metastatic_macro_f1)
print(msg_log)
def start(self, display=True):
train_dataset = TorchDataset(dataset=self.dataset, split_type='train')
train_dataloader = DataLoader(train_dataset, batch_size=self.param_dict['batch_size'], shuffle=True)
valid_dataset = TorchDataset(self.dataset, split_type='valid')
valid_dataloader = DataLoader(valid_dataset, batch_size=self.param_dict['batch_size'], shuffle=True)
test_dataset = TorchDataset(self.dataset, split_type='test')
test_dataloader = DataLoader(test_dataset, batch_size=self.param_dict['batch_size'], shuffle=True)
metastatic_dataset = TorchDataset(self.dataset, split_type='metastatic')
metastatic_dataloader = DataLoader(metastatic_dataset, batch_size=self.param_dict['batch_size'], shuffle=True)
for epoch in range(1, self.param_dict['epoch_num'] + 1):
train_pred, train_label, train_loss = self.iteration(epoch=epoch, dataloader=train_dataloader, is_training=True)
train_acc, train_micro_f1, train_macro_f1, train_micro_p, train_micro_r = evaluate_multi_cls(train_pred, train_label)
valid_pred, valid_label, valid_loss = self.iteration(epoch=epoch, dataloader=valid_dataloader, is_training=False)
valid_acc, valid_micro_f1, valid_macro_f1, valid_micro_p, valid_micro_r = evaluate_multi_cls(valid_pred, valid_label)
test_pred, test_label, test_loss = self.iteration(epoch=epoch, dataloader=test_dataloader, is_training=False)
test_acc, test_micro_f1, test_macro_f1, test_micro_p, test_micro_r = evaluate_multi_cls(test_pred, test_label)
metastatic_pred, metastatic_label, metastatic_loss = \
self.iteration(epoch=epoch, dataloader=metastatic_dataloader, is_training=False)
metastatic_acc, metastatic_micro_f1, metastatic_macro_f1, metastatic_micro_p, metastatic_micro_r\
= evaluate_multi_cls(metastatic_pred, metastatic_label)
res_list = [
train_acc, valid_acc, test_acc, metastatic_acc, train_macro_f1, \
valid_macro_f1, test_macro_f1, metastatic_macro_f1
]
if valid_acc > self.min_dif:
self.min_dif = valid_acc
self.best_res = res_list
self.best_epoch = epoch
# save model
# save_complete_model_path = osp.join(current_path, model_save_dir, self.trainer_info + '_complete.pkl')
# torch.save(self.model, save_complete_model_path)
same_model_param_path = osp.join(current_path, model_save_dir, self.trainer_info + '_param.pkl')
torch.save(self.model.state_dict(), same_model_param_path)
if display:
self.print_res(res_list, epoch)
if epoch % 50 == 0 and epoch > 0:
print('Best res')
self.print_res(self.best_res, self.best_epoch)
if __name__ == '__main__':
param_dict = {
'seed': 2,
'split_param': [0.9, 0.05, 0.05],
'ft_stand': True,
'dropout_num': 0.3,
'layer_num': 4,
'epoch_num': 200,
'lr': 1e-4,
'param_l2_coef': 5e-4,
'batch_size': 128,
'h_dim': 512
}
trainer = Trainer(**param_dict)
trainer.start()