forked from YuntianChen/EnLSTM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
train.py
305 lines (273 loc) · 12.5 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
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
import comet_ml
experiment = comet_ml.Experiment(
api_key = "w9z2jMikkCGUFnqrVmTtWrtZU",
project_name = "EnLSTM",
workspace = "lumisong"
)
import numpy as np
import torch
from torch.autograd import Variable
from torch.utils.data import DataLoader
import time
import os
import matplotlib.pyplot as plt
import pickle
import json
from enn import enn, enrml, lamuda
from net import netLSTM_withbn
from data import TextDataset
from configuration import config
from util import Record, save_var, get_file_list, list_to_csv, shrink, save_txt
# 使用exeriment记录参数
# region
# Set the random seed
LUCKY_NUM = 666666
torch.manual_seed(LUCKY_NUM)
torch.cuda.manual_seed(LUCKY_NUM)
np.random.seed(LUCKY_NUM)
# 记录 random seed
experiment.log_parameter("LUCKY_NUM", 666666)
# initialize matplotlib and CUDA
# plt.ion()
torch.cuda.set_device(config.deviceID)
# 记录GPU使用情况
experiment.log_other("GPU", torch.cuda.get_device_name(config.deviceID))
# set the work path
# endregion
PATH = config.path
# 记录路径
experiment.log_other("PATH", PATH)
# region
if not os.path.isdir(PATH):
os.makedirs(PATH)
experiment.log_parameter("new_directory_created", True)
# Parameters used in the net
ERROR_PER = config.ERROR_PER
experiment.log_parameter("ERROR_PER", ERROR_PER)
NE = config.ne # number of ensemble
experiment.log_parameter("NE", NE)
GAMMA = config.GAMMA
experiment.log_parameter("GAMMA", GAMMA)
T = config.T
experiment.log_parameter("T", T)
# endregion
# Load data and initialize enn net
text = TextDataset()
experiment.log_parameter("TextDataset_class", text.__class__.__name__)
# Assuming TextDataset has an attribute 'data_size' or similar, log it
experiment.log_parameter("TextDataset_data_size", text)
criterion = torch.nn.MSELoss()
experiment.log_parameter("MSELoss_class", criterion.__class__.__name__)
# region
INFO = {
"train len": config.train_len,
"shrink len": config.shrink_len,
"window step": config.window_step,
"Error per": config.ERROR_PER,
"input dim": config.input_dim,
"hid dim": config.hid_dim,
"num layer": config.num_layer,
"number of ensemble": config.ne,
"T": config.T,
"batch size": config.batch_size,
"epoch": config.epoch,
"GAMMA": config.GAMMA,
"test ID": config.test_ID,
"train ID": config.train_ID,
"Weight": enn.ENN.W,
"Lamuda": lamuda.Lamuda.L
}
for key, value in INFO.items():
experiment.log_parameter(key, value)
with open('{}/Info.json'.format(PATH), 'w', encoding='utf-8') as f:
json.dump(INFO, f, ensure_ascii=False)
# endregion
experiment.end()
# train the net, the result will be the net parameters and saved as pickle
def train(net_enn, input_, target, feature_name=''):
dstb_y = lamuda.Lamuda(target, NE, ERROR_PER)
train_losses = Record()
losses = Record()
lamuda_history = Record()
std_history = Record()
pred_history = Record()
initial_parameters = net_enn.initial_parameters
initial_pred = net_enn.output(input_)
train_losses.update(criterion(initial_pred.mean(0), target).tolist())
losses.update(criterion(initial_pred.mean(0), target).tolist())
std_history.update(dstb_y.std(initial_pred))
pred_history.update(initial_pred)
lamuda_history.update(dstb_y.lamuda(initial_pred))
for j in range(T):
torch.cuda.empty_cache()
params = net_enn.get_parameter()
dstb_y.update()
time_ = time.strftime('%Y%m%d_%H_%M_%S')
delta = enrml.EnRML(pred_history.get_latest(mean=False), params, initial_parameters,
lamuda_history.get_latest(mean=False), dstb_y.dstb, ERROR_PER)
params_raw = net_enn.update_parameter(delta)
torch.cuda.empty_cache()
pred = net_enn.output(input_)
loss_new = criterion(pred.mean(0), target).tolist()
bigger = train_losses.check(loss_new)
record_while = 0
while bigger:
record_while += 1
lamuda_history.update(lamuda_history.get_latest(mean=False) * GAMMA)
if lamuda_history.get_latest(mean=False) > GAMMA ** 10:
lamuda_history.update(lamuda_history.data[0])
print('abandon current iteration')
net_enn.set_parameter(params)
loss_new = train_losses.get_latest()
dstb_y.update()
params_raw = params
break
dstb_y.update()
net_enn.set_parameter(params)
delta = enrml.EnRML(pred_history.get_latest(mean=False), params, initial_parameters,
lamuda_history.get_latest(mean=False), dstb_y.dstb, ERROR_PER)
params_raw = net_enn.update_parameter(delta)
torch.cuda.empty_cache()
pred = net_enn.output(input_)
loss_new = criterion(pred.mean(0), target).tolist()
print('update losses, new loss:{}'.format(loss_new))
bigger = train_losses.check(loss_new)
train_losses.update(loss_new)
save_var(params_raw, '{}/{}_{}_params'.format(PATH, time_, feature_name))
print("iteration:{} \t current train losses:{}".format(j, train_losses.get_latest(mean=True)))
save_txt('{}/loss_{}.txt'.format(PATH, feature_name), time.strftime('%Y%m%d_%H_%M_%S')+','+str(train_losses.get_latest(mean=True))+',\n')
pred_history.update(pred)
std_history.update(dstb_y.std(pred))
if std_history.bigger():
lamuda_history.update(lamuda_history.get_latest(mean=False))
else:
lamuda_tmp = lamuda_history.get_latest(mean=False) / GAMMA
if lamuda_tmp < 0.005:
lamuda_tmp = 0.005
lamuda_history.update(lamuda_tmp)
return net_enn, train_losses.get_latest(mean=True), pred_history.get_latest(mean=False)
# predict in one calculation
def predict_full(data, params=None, model_predict=None):
input_ = torch.tensor(data)
input_ = Variable(input_.view(1, len(data), config.input_dim).float()).cuda()
if params is not None:
model_predict.set_parameter(params)
pred = model_predict.output(input_)
return pred
# draw the result based on the latest parameters
def draw_result(enn_net):
# Get the latest parameters, and initialize the enn net
param_list = get_file_list('params', config.path)
params = pickle.load(open(param_list[-1], 'rb'))
print("use parameter file: {}".format(param_list[-1]))
enn_net.set_parameter(params)
# Draw the result of well in test well lists
for well in config.test_ID:
input_, target = text.test_dataset(well)
pred_enn = predict_full(input_, params=params, model_predict=enn_net).cpu()
# output the loss
loss = criterion(pred_enn.mean(0), torch.tensor(target).float())
print("well{}\t test loss: {}".format(well, loss))
# get the real predicted and target data
pred = text.inverse_normalize(pred_enn.mean(0))
target = text.inverse_normalize(target)
std = 3 * np.array(text.inverse_normalize(pred_enn).std(0))
# save the test loss
save_txt('{}/test_loss.txt'.format(PATH), '{}, {}\n'.format(loss, std.mean()))
print('std:', std.mean())
x = np.arange(len(target))
plt.figure(figsize=(60, 5))
plt.plot(target, label='target', color='black', alpha=0.4)
plt.errorbar(x, pred[:, 0], yerr=std[:, 0], color='red', alpha=0.7)
plt.title(config.info)
plt.legend()
ylabel = config.columns[config.input_dim+1]
plt.ylabel(ylabel)
plt.savefig('{}/result.png'.format(PATH))
plt.show()
def test(enn_net, feature_name='', draw_result=False):
# Get the latest parameters, and initialize the enn net
param_list = get_file_list('{}_params'.format(feature_name), config.path)
params = pickle.load(open(param_list[-1], 'rb'))
print("use parameter file: {}".format(param_list[-1]))
enn_net.set_parameter(params)
# Draw the result of well in test well lists
for well in config.test_ID:
input_, target = text.test_dataset(well)
pred_enn = predict_full(input_, params=params, model_predict=enn_net).cpu()
# output the loss
loss = criterion(pred_enn.mean(0), torch.tensor(target).float())
print("well{}\t{}\ttest loss: {}".format(well, feature_name, loss))
# replace the test dataset and reset train dataset
text.df_list[well-1][[feature_name]] = np.array(text.inverse_normalize(pred_enn.mean(0)))
# get the std
std = 3 * np.array(text.inverse_normalize(pred_enn).std(0))
# save the test loss
save_txt('{}/test_loss_{}.txt'.format(PATH, feature_name), '{}, {}, {}\n'.format(feature_name, loss, std.mean()))
print('std:', std.mean())
if draw_result:
# get the real predicted and target data
pred = np.array(text.inverse_normalize(pred_enn.mean(0)))
target = text.inverse_normalize(target)
x = np.arange(len(target))
plt.figure(figsize=(60, 5))
plt.plot(target, label='target', color='black', alpha=0.4)
plt.errorbar(x, pred[:, 0], yerr=std[:, 0], color='red', alpha=0.7)
plt.title(config.info)
plt.legend()
y_label = feature_name
plt.ylabel(y_label)
plt.tight_layout()
plt.savefig('{}/result_{}.png'.format(PATH, feature_name))
# plt.show()
def run():
for epoch in range(config.epoch):
print(epoch)
while config.input_dim+1 <= len(config.columns):
current_feature_name = config.columns[config.input_dim]
textLoader = DataLoader(text, batch_size=config.batch_size, shuffle=True,
num_workers=config.num_workers, drop_last=config.drop_last)
model = netLSTM_withbn()
with torch.no_grad():
model = model.cuda()
net_enn_train = enn.ENN(model, NE)
# If pre_existent epoch found, set net_enn_train parameters with pre_existent epoch record.
# Only processed if current epoch count is 0
epoch_list = [i for i in os.listdir(PATH) if i.startswith(
"parameters_{}_epoch_".format(current_feature_name))]
if len(epoch_list) > 0 and epoch == 0:
print("Pre_existent epoch found: {}".format(sorted(epoch_list)[-1]))
epoch_pre_existent = pickle.load(open(os.path.join(PATH, sorted(epoch_list)[-1]), 'rb'))
net_enn_train.set_parameter(epoch_pre_existent)
if epoch > 0:
parameter_path = os.path.join(PATH, "parameters_{}_epoch_{}".format(current_feature_name, epoch-1))
print("Setting checkpoint {}".format(parameter_path))
parameter_checkpoint = pickle.load(open(parameter_path, 'rb'))
net_enn_train.set_parameter(parameter_checkpoint)
for i, data in enumerate(textLoader):
print('#'*30)
print("{}: batch{}".format(time.strftime('%Y%m%d_%H_%M_%S'), i))
# preparing the train data
input_, target = data
input_ = torch.from_numpy(np.stack(list(shrink(input_, config.shrink_len)), axis=1))
target = torch.from_numpy(np.stack(list(shrink(target, config.shrink_len)), axis=1))
with torch.no_grad():
input_, target = map(Variable, (input_.float(), target.float()))
target = target.reshape(-1, config.output_dim)
input_ = input_.cuda()
target = target.cuda()
# train the model
net_enn_train, loss, pred_data = train(net_enn_train, input_, target, feature_name=current_feature_name)
with torch.no_grad():
params = net_enn_train.get_parameter()
filename = PATH+"/parameters_{}_epoch_{}".format(current_feature_name, epoch)
save_var(params, filename)
del params
test(net_enn_train, feature_name=current_feature_name, draw_result=(epoch == config.epoch-1))
config.input_dim += 1
text.reset_train_dataset()
config.input_dim -= 3
text.reset_train_dataset()
text.reset_test_dataset()
if __name__ == '__main__':
run()