-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain_model516.py
137 lines (116 loc) · 4.48 KB
/
main_model516.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
import torch
import matplotlib.pyplot as plt
import tqdm
from loader import database
import argparse
import evaluate
import numpy as np
import random
from model.new516 import trainer, params_save
import statsmodels.api as sm
import imp
def setup_seed(seed):
torch.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
np.random.seed(seed)
random.seed(seed)
torch.backends.cudnn.deterministic = True
# 设置随机数种子
setup_seed(2)
# 训练或测试模式
mode = 'train'
# 训练参数读取
args = params_save.Params.trainparam()
if __name__ == "__main__":
with torch.cuda.device(args.device):
args.device = torch.device(f'cuda:{args.device}' if torch.cuda.is_available() else 'cpu')
if torch.cuda.is_available():
print(f"GPU{args.device} open")
else:
print("cpu open")
box = trainer.Trainer(args)
d_box = database.Dataloader(
database_wdir="/HDD_data/HYK/bis/database",
time_step=1,
nums=1,
tw=args.tw
)
# 开始训练或测试
if mode == "train":
test_loader, test_label = d_box.test_data_loader(
data="test",
batch=args.test_batch,
batch_size=128
)
d_box.time_step = 10
vaild_loader = d_box.train_data_loader(
data="test",
batch=args.vaild_batch,
batch_size=512
)
d_box.time_step = 13
train_loader = d_box.train_data_loader(
batch=args.train_batch,
batch_size=2048,
shuffle=True,
)
box.train(
X=train_loader,
X2=vaild_loader,
model_file=args.best_file,
best_loss=args.best_loss,
config=args
)
test_out = box.test(
X=test_loader,
epoch_pth=args.best_file,
test_batch=76)
ist, isp = d_box.time_devide(case_nums=76, traindata="test")
access = evaluate.Evalulate(test_label, test_out, ist, isp, case_num=76)
print("MDPE MDAPE RMSE\r")
for i in range(4):
print("%.2f %.2f %.2f" % access.loss(i))
lowess = sm.nonparametric.lowess
test_new = list(range(76))
for i in tqdm.tqdm(range(76)):
axis = list(range(len(test_out[i])))
test_new[i] = lowess(test_out[i], axis, frac=0.03)[:, 1]
access = evaluate.Evalulate(test_new, test_label, ist, isp, case_num=76)
print("MDPE MDAPE RMSE\r")
for i in range(4):
print("%.2f %.2f %.2f" % access.loss(i))
# access = evaluate.Evalulate(test_label, test_new, ist, isp, case_num=76)
# file = {}
# for i in range(4):
# X = np.asarray(access.loss(i))
# name = ["mdpe", "mdape", "rmse",
# "induction_mdpe", "induction_mdape", "induction_rmse",
# "maintence_mdpe", "maintence_mdape", "maintence_rmse",
# "recovery_mdpe", "recovery_mdape", "recovery_rmse"]
# for j in range(3):
# file[f"{name[3*i+j]}"] = X[j, :]
#
# import pandas as pd
# df = pd.DataFrame(dict([(k, pd.Series(v)) for k, v in file.items()]))
#
# df.to_csv('/HDD_data/HYK/bis/database/result.csv')
elif mode == "test":
test_loader, test_label = d_box.test_data_loader(
tw=args.tw,
batch=args.test_batch,
batch_size=100,
timestep=1)
pre_tr_times = 25
pre_file = f'/home/user02/HYK/bis_transformer/output/{args.model_name}/model/epoch{pre_tr_times}.pth'
test_out = box.test(
X=test_loader,
epoch_pth=pre_file,
test_batch=1)
plt.grid(True)
plt.autoscale(axis='x', tight=True)
for i in range(3):
plt.figure()
plt.plot(test_label[i])
plt.plot(test_new[i])
# plt.savefig(f'/home/user02/HYK/bis/attention/output_picture/case{i}.png')
plt.show()