-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathge_train.py
340 lines (330 loc) · 14.1 KB
/
ge_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
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
import torch
import torch.nn.functional as F
import torch.nn as nn
import torch.optim as optim
import numpy as np
from torch.utils.data import DataLoader
from torch.utils.data.dataset import Subset
from sklearn.model_selection import KFold
import h5py
import time
import sys
from tqdm import tqdm
import os
import csv
import matplotlib.pyplot as plt
import ge_data
import ge_loss
import ge_nn
class EarlyStopping:
def __init__(self, patience=7, verbose=False, delta=0, trace_func=print):
self.patience = patience
self.verbose = verbose
self.counter = 0
self.best_score = None
self.early_stop = False
self.val_loss_min = np.Inf
self.delta = delta
self.trace_func = trace_func
def __call__(self, val_loss, model, path):
score = -val_loss
if self.best_score is None:
self.best_score = score
self.save_checkpoint(val_loss, model, path)
elif score < self.best_score + self.delta:
self.counter += 1
self.trace_func(f'EarlyStopping counter: {self.counter} out of {self.patience}')
if self.counter >= self.patience:
self.early_stop = True
else:
self.best_score = score
self.save_checkpoint(val_loss, model, path)
self.counter = 0
def save_checkpoint(self, val_loss, model, path):
if self.verbose:
self.trace_func(f'Validation loss decreased ({self.val_loss_min:.6f} --> {val_loss:.6f}). Saving model ...')
torch.save(model.state_dict(), path)
self.val_loss_min = val_loss
with open('train_log.txt', 'a') as f:
f.write('save model\n')
def ge_train_fun_kfold(data, n_device, n_epochs, batchsize, n_targets, k_fold):
print('calling dataloader ...')
with open('train_log.txt', 'a') as f:
f.write('calling dataloader ...\n')
#モデル読み込み
train_set = ge_data.ge_train_dataset(data)
#train_iter = DataLoader(train_set, batchsize)
device_str = 'cuda:{}'.format(n_device)
device = torch.device(device_str if torch.cuda.is_available() else "cpu")
print('used device : ', device)
#損失関数
loss_fun = nn.PoissonNLLLoss()
#foldごとの学習の損失
avg_kfold_train_loss = []
#foldごとの検証の損失
avg_kfold_valid_loss = []
#交差検証用
kf = KFold(n_splits = k_fold)
for _fold, (train_index, val_index) in enumerate(kf.split(train_set)):
with open('train_log.txt', 'a') as f:
f.write('fold{} start\n'.format(_fold))
#交差検証用のtrain, validデータをロード
train_loader = DataLoader(Subset(train_set,train_index), batch_size=batchsize, shuffle=True, num_workers=16)
val_loader = DataLoader(Subset(train_set,val_index), batch_size=batchsize, shuffle=True, num_workers=16)
#バッチごと学習の損失を追う
train_losses = []
#バッチごと検証の損失を追う
valid_losses = []
#epochごとの学習の損失
avg_train_losses = []
#epochごとの検証の損失
avg_valid_losses = []
#モデル定義
ge_model = ge_nn.Net(n_targets=n_targets)
ge_model.to(device)
#最適化手法の選択
optimizer = optim.Adam(ge_model.parameters())
# initialize the early_stopping object
early_stopping = EarlyStopping(verbose=True)
for epoch in range(n_epochs):
#学習
ge_model.train()
for train_in, train_out in tqdm(train_loader):
#モデル入力
train_in, train_out = train_in.to(device), train_out.to(device)
out = ge_model(train_in)
#損失計算
loss = loss_fun(out, train_out)
ge_model.zero_grad()
loss.backward()
optimizer.step()
#損失記録
train_losses.append(loss.item())
#検証
ge_model.eval()
for valid_in, valid_out in tqdm(val_loader):
#モデル入力
valid_in = valid_in.to(device)
valid_out = valid_out.to(device)
out = ge_model(valid_in)
#損失計算
loss = loss_fun(out, valid_out)
#損失記録
valid_losses.append(loss.item())
#損失の平均をとる
train_loss = np.average(train_losses)
valid_loss = np.average(valid_losses)
#エポックごとの損失を保存していく
avg_train_losses.append(train_loss)
avg_valid_losses.append(valid_loss)
print('fold: {}/{} epoch: {}/{} train_loss: {:.6f} valid_loss: {:.6f}'.format(_fold+1, k_fold, epoch+1, n_epochs, train_loss, valid_loss))
with open('train_log.txt', 'a') as f:
f.write('fold: {}/{} epoch: {}/{} train_loss: {:.6f} valid_loss: {:.6f}\n'.format(_fold+1, k_fold, epoch+1, n_epochs, train_loss, valid_loss))
#次のエポックのためにリセットする
train_losses = []
valid_losses = []
#earlystopping
early_stopping(valid_loss, ge_model, path='./model_checkpoint/checkpoint_fold{}.pth'.format(_fold))
if early_stopping.early_stop:
print('Early stopping')
with open('train_log.txt', 'a') as f:
f.write('Early stopping\n')
break
#損失の平均をとる
kfold_train_loss = np.average(avg_train_losses)
kfold_valid_loss = np.average(avg_valid_losses)
#foldごとの損失を保存していく
avg_kfold_train_loss.append(kfold_train_loss)
avg_kfold_valid_loss.append(kfold_valid_loss)
#一番いいモデルをロードする。
#ge_model.load_state_dict(torch.load('./model_checkpoint/checkpoint_fold{}.pth'.format(_fold)))
#学習状況を可視化
fig = plt.figure(figsize=(10,8))
plt.plot(range(1, len(avg_train_losses)+1), avg_train_losses, label='Training Loss')
plt.plot(range(1, len(avg_valid_losses)+1), avg_valid_losses, label='Validation Loss')
#validlossの最低を検索する->earlystoppingに利用する
minposs = avg_valid_losses.index(min(avg_valid_losses)) + 1
plt.axvline(minposs, linestyle='--', color='r',label='Early Stopping Checkpoint')
plt.xlabel('epochs')
plt.ylabel('loss')
plt.ylim(0, 0.5) # consistent scale
plt.xlim(0, len(avg_train_losses)+1) # consistent scale
plt.grid(True)
plt.legend()
plt.tight_layout()
plt.show()
fig.savefig('./loss_plot/loss_plot_fold{}.png'.format(_fold), bbox_inches='tight')
with open('train_log.txt', 'a') as f:
f.write('fold{}, graph ploted\n'.format(_fold))
print(avg_kfold_train_loss)
print(avg_kfold_valid_loss)
with open('./kfold_loss/kfold_train_loss.csv', 'w') as ft :
writer = csv.writer(ft)
writer.writerows(enumerate(avg_kfold_train_loss))
with open('./kfold_loss/kfold_valid_loss.csv', 'w') as fv :
writer = csv.writer(fv)
writer.writerows(enumerate(avg_kfold_valid_loss))
n_bestmodel_fold = avg_kfold_valid_loss.index(min(avg_kfold_valid_loss))
return n_bestmodel_fold
def ge_train_fun_mse(data, n_device, n_epochs, batchsize, n_targets):
print('calling dataloader ...')
with open('train_log.txt', 'a') as f:
f.write('calling dataloader ...\n')
#モデル読み込み
train_set = ge_data.ge_train_dataset(data)
val_set = ge_data.ge_valid_dataset(data)
device_str = 'cuda:{}'.format(n_device)
device = torch.device(device_str if torch.cuda.is_available() else "cpu")
print('used device : ', device)
#損失関数
loss_fun = nn.MSELoss()
#train, validデータをロード
train_loader = DataLoader(train_set, batch_size=batchsize, shuffle=True, num_workers=80)
val_loader = DataLoader(val_set, batch_size=batchsize, shuffle=True, num_workers=80)
#バッチごと学習の損失を追う
train_losses = []
#バッチごと検証の損失を追う
valid_losses = []
#epochごとの学習の損失
avg_train_losses = []
#epochごとの検証の損失
avg_valid_losses = []
#モデル定義
ge_model = ge_nn.Net(n_targets=n_targets)
ge_model.to(device)
#最適化手法の選択
optimizer = optim.Adam(ge_model.parameters())
# initialize the early_stopping object
early_stopping = EarlyStopping(verbose=True)
for epoch in range(n_epochs):
#学習
ge_model.train()
for train_in, train_out in tqdm(train_loader):
#モデル入力
train_in, train_out = train_in.to(device), train_out.to(device)
out = ge_model(train_in)
#損失計算
loss = loss_fun(out, train_out)
ge_model.zero_grad()
loss.backward()
optimizer.step()
#損失記録
train_losses.append(loss.item())
#検証
ge_model.eval()
for valid_in, valid_out in tqdm(val_loader):
#モデル入力
valid_in = valid_in.to(device)
valid_out = valid_out.to(device)
out = ge_model(valid_in)
#損失計算
loss = loss_fun(out, valid_out)
#損失記録
valid_losses.append(loss.item())
#損失の平均をとる
train_loss = np.average(train_losses)
valid_loss = np.average(valid_losses)
#エポックごとの損失を保存していく
avg_train_losses.append(train_loss)
avg_valid_losses.append(valid_loss)
print('epoch: {}/{} train_loss: {:.6f} valid_loss: {:.6f}'.format(epoch+1, n_epochs, train_loss, valid_loss))
with open('train_log.txt', 'a') as f:
f.write('epoch: {}/{} train_loss: {:.6f} valid_loss: {:.6f}\n'.format(epoch+1, n_epochs, train_loss, valid_loss))
#次のエポックのためにリセットする
train_losses = []
valid_losses = []
#earlystopping
early_stopping(valid_loss, ge_model, path='./mse/checkpoint_fold_mse.pth')
if early_stopping.early_stop:
print('Early stopping')
with open('train_log.txt', 'a') as f:
f.write('Early stopping\n')
break
#一番いいモデルをロードする。
#ge_model.load_state_dict(torch.load('./model_checkpoint/checkpoint_fold{}.pth'.format(_fold)))
#学習状況を可視化
fig = plt.figure(figsize=(10,8))
plt.plot(range(1, len(avg_train_losses)+1), avg_train_losses, label='Training Loss')
plt.plot(range(1, len(avg_valid_losses)+1), avg_valid_losses, label='Validation Loss')
#validlossの最低を検索する->earlystoppingに利用する
minposs = avg_valid_losses.index(min(avg_valid_losses)) + 1
plt.axvline(minposs, linestyle='--', color='r',label='Early Stopping Checkpoint')
plt.xlabel('epochs')
plt.ylabel('loss')
plt.ylim(0, 0.5) # consistent scale
plt.xlim(0, len(avg_train_losses)+1) # consistent scale
plt.grid(True)
plt.legend()
plt.tight_layout()
fig.savefig('./mse/loss_plot_mse.png', bbox_inches='tight')
with open('train_log.txt', 'a') as f:
f.write('graph ploted\n')
def ge_train_fun_optim(data, n_device, n_epochs, batchsize, n_optim, model_dir):
#print('calling dataloader ...')
#モデル読み込み
train_set = ge_data.ge_train_dataset(data)
#test = ge_data.hogehoge
train_iter = DataLoader(train_set, batchsize)
#モデル定義
train_model = ge_nn.Net()
device_str = "cuda:{}".format(n_device)
device = torch.device(device_str if torch.cuda.is_available() else "cpu")
#print("used device : ", device)
train_model.to(device)
#最適化手法
if n_optim == 0:
optimizer = optim.Adam(train_model.parameters())
elif n_optim == 1:
optimizer = optim.SGD(train_model.parameters(), 0.01)
elif n_optim == 2:
optimizer = optim.RMSprop(train_model.parameters())
elif n_optim == 3:
optimizer = optim.Adadelta(train_model.parameters())
elif n_optim == 4:
optimizer = optim.AdamW(train_model.parameters())
elif n_optim == 5:
optimizer = optim.Adagrad(train_model.parameters())
elif n_optim == 6:
optimizer = optim.ASGD(train_model.parameters())
elif n_optim == 7:
optimizer = optim.Adamax(train_model.parameters())
else :
print('please input optimizer')
sys.exit(1)
loss_fun = nn.PoissonNLLLoss()
loss_fun2 = nn.MSELoss()
train_model.train()
print('epoch poissonLoss mseLoss Acc')
for epoch in range(n_epochs):
counter = 0
batch_loss = 0.0
batch_acc = 0.0
#print('Epoch {}/{}'.format(epoch+1, n_epochs))
#print('------------------------------------------------')
for train_in, train_out in tqdm(train_iter):
t1 = time.time()
counter = counter + 1
#変数定義
#モデル入力
train_in = train_in.to(device)
train_out = train_out.to(device)
out = train_model(train_in)
#損失計算
loss = loss_fun(out, train_out)
mse_loss = loss_fun2(out, train_out)
#acc = ge_loss.log_r2_score(out, train_out)
acc = 0.0
train_model.zero_grad()
loss.backward()
optimizer.step()
batch_loss += loss
batch_acc += acc
t2 = time.time()
#print('{} batch{} poissonLoss: {:.4f} mseLoss: {:.4f} Acc: {:.4f} time {}'.format(epoch+1, counter, loss, mse_loss, acc, t2-t1))
#print('------------------------------------------------')
epoch_loss = batch_loss / batchsize
epoch_acc = batch_acc / batchsize
print('{} {:.4f} {:.4f} {:.4f}'.format(epoch+1, epoch_loss, mse_loss, epoch_acc))
#print('------------------------------------------------')
#print('------------------------------------------------')
torch.save(train_model.state_dict(), "./" + model_dir + "/model_optim{}_epoch{}.pth".format(n_optim, epoch))