-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
167 lines (146 loc) · 7.74 KB
/
main.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
import argparse
import os
import torch
import numpy as np
import time
from exp.exp_model import Exp_Model
parser = argparse.ArgumentParser(description='')
parser.add_argument('--data', type=str, required=True, default='ETTh1', help='data')
parser.add_argument('--root_path', type=str, default='./data/ETT/', help='root path of the data file')
parser.add_argument('--data_path', type=str, default='ETTh1.csv', help='data file')
parser.add_argument('--features', type=str, default='M',
help='forecasting task, options:[M, S]; M:multivariate predict multivariate, '
'S: univariate predict univariate')
parser.add_argument('--target', type=str, default='OT', help='target feature in S or M task')
parser.add_argument('--checkpoints', type=str, default='./checkpoints/', help='location of model checkpoints')
parser.add_argument('--input_len', type=int, default=96, help='input length')
parser.add_argument('--pred_len', type=int, default=96, help='prediction length')
parser.add_argument('--enc_in', type=int, default=7, help='input variable size')
parser.add_argument('--dec_out', type=int, default=7, help='output variable size')
parser.add_argument('--d_model', type=int, default=28, help='hidden dims of model')
parser.add_argument('--encoder_layer', type=int, default=3)
parser.add_argument('--patch_size', type=int, default=6, help='the size of each patch')
parser.add_argument('--Cross', action='store_true',
help='whether to use cross-variable attention'
, default=False)
parser.add_argument('--dropout', type=float, default=0.05, help='dropout')
parser.add_argument('--num_workers', type=int, default=0, help='data loader num workers')
parser.add_argument('--itr', type=int, default=5, help='experiments times')
parser.add_argument('--train_epochs', type=int, default=20, help='train epochs')
parser.add_argument('--batch_size', type=int, default=16, help='batch size of train input data')
parser.add_argument('--patience', type=int, default=1, help='early stopping patience')
parser.add_argument('--learning_rate', type=float, default=0.0001, help='optimizer learning rate')
parser.add_argument('--lradj', type=str, default='type1', help='adjust learning rate')
parser.add_argument('--save_loss', action='store_true', help='whether saving results and checkpoints', default=False)
parser.add_argument('--gpu', type=int, default=0, help='gpu')
parser.add_argument('--use_multi_gpu', action='store_true', help='use multiple gpus', default=False)
parser.add_argument('--devices', type=str, default='0,1,2,3', help='device ids of multile gpus')
parser.add_argument('--train', action='store_true',
help='whether to train'
, default=False)
parser.add_argument('--reproducible', action='store_true',
help='whether to make results reproducible'
, default=False)
args = parser.parse_args()
args.use_gpu = True if torch.cuda.is_available() else False
if args.reproducible:
np.random.seed(4321) # reproducible
torch.manual_seed(4321)
torch.cuda.manual_seed_all(4321)
torch.backends.cudnn.deterministic = True
else:
torch.backends.cudnn.deterministic = False
torch.backends.cudnn.benchmark = False
torch.backends.cudnn.enabled = True
if args.use_gpu and args.use_multi_gpu:
args.devices = args.devices.replace(' ', '')
device_ids = args.devices.split(',')
args.device_ids = [int(id_) for id_ in device_ids]
args.gpu = args.device_ids[0]
data_parser = {
'ETTh1': {'data': 'ETTh1.csv', 'target': 'OT', 'root_path': './data/ETT/', 'M': [7, 7], 'S': [1, 1]},
'ETTh2': {'data': 'ETTh2.csv', 'target': 'OT', 'root_path': './data/ETT/', 'M': [7, 7], 'S': [1, 1]},
'ETTm1': {'data': 'ETTm1.csv', 'target': 'OT', 'root_path': './data/ETT/', 'M': [7, 7], 'S': [1, 1]},
'ETTm2': {'data': 'ETTm2.csv', 'target': 'OT', 'root_path': './data/ETT/', 'M': [7, 7], 'S': [1, 1]},
'ECL': {'data': 'ECL.csv', 'target': 'MT_321', 'root_path': './data/ECL/', 'M': [321, 321], 'S': [1, 1]},
'Traffic': {'data': 'Traffic.csv', 'target': 'Sensor_861', 'root_path': './data/Traffic/', 'M': [862, 862],
'S': [1, 1]},
'weather': {'data': 'weather.csv', 'target': 'OT', 'root_path': './data/weather/', 'M': [21, 21], 'S': [1, 1]},
'Solar': {'data': 'solar_AL.csv', 'target': '136', 'root_path': './data/Solar/', 'M': [137, 137], 'S': [1, 1]},
}
if args.data in data_parser.keys():
data_info = data_parser[args.data]
args.data_path = data_info['data']
args.target = data_info['target']
args.root_path = data_info['root_path']
args.enc_in, args.dec_out = data_info[args.features]
args.target = args.target.replace('/r', '').replace('/t', '').replace('/n', '')
lr = args.learning_rate
print('Args in experiment:')
print(args)
mse_total = []
mae_total = []
Exp = Exp_Model
for ii in range(args.itr):
# setting record of experiments
exp = Exp(args) # set experiments
if args.train:
setting = '{}_ft{}_ll{}_pl{}_{}'.format(args.data,
args.features, args.input_len,
args.pred_len, ii)
print('>>>>>>>start training| pred_len:{}, settings: {}>>>>>>>>>>>>>>>>>>>>>>>>>>'.
format(args.pred_len, setting))
try:
exp = Exp(args) # set experiments
exp.train(setting)
except KeyboardInterrupt:
print('-' * 99)
print('Exiting from forecasting early')
print('>>>>>>>testing| pred_len:{}: {}<<<<<<<<<<<<<<<<<'.format(args.pred_len, setting))
exp = Exp(args) # set experiments
mse, mae = exp.test(setting, load=True, write_loss=True, save_loss=args.save_loss)
mse_total.append(mse)
mae_total.append(mae)
torch.cuda.empty_cache()
args.learning_rate = lr
else:
setting = '{}_ft{}_ll{}_pl{}_{}'.format(args.data,
args.features, args.input_len,
args.pred_len, ii)
print('>>>>>>>testing| pred_len:{} : {}<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<'.format(args.pred_len, setting))
exp = Exp(args) # set experiments
mse, mae = exp.test(setting, load=True, write_loss=True, save_loss=args.save_loss)
mse_total.append(mse)
mae_total.append(mae)
torch.cuda.empty_cache()
args.learning_rate = lr
path1 = './result.csv'
if not os.path.exists(path1):
with open(path1, "a") as f:
write_csv = ['Time', 'Data', 'input_len', 'pred_len', 'encoder_layer', 'patch_size', 'Mean MSE',
'Mean MAE', 'Std MSE', 'Std MAE']
np.savetxt(f, np.array(write_csv).reshape(1, -1), fmt='%s', delimiter=',')
f.flush()
f.close()
mse = np.asarray(mse_total)
mae = np.asarray(mae_total)
avg_mse = np.mean(mse)
std_mse = np.std(mse)
avg_mae = np.mean(mae)
std_mae = np.std(mae)
print('|Mean|mse:{}, mae:{}|Std|mse:{}, mae:{}'.format(avg_mse, avg_mae, std_mse, std_mae))
path = './result.log'
with open(path, "a") as f:
f.write('|{}_{}|pred_len{}: '.format(
args.data, args.features, args.pred_len) + '\n')
f.write('|Mean|mse:{}, mae:{}|Std|mse:{}, mae:{}'.
format(avg_mse, avg_mae, std_mse, std_mae) + '\n')
f.flush()
f.close()
with open(path1, "a") as f:
f.write(time.strftime("%Y-%m-%d-%H_%M_%S", time.localtime()))
f.write(',{},{},{},{},{},{},{},{},{}'.
format(args.data, args.input_len, args.pred_len, args.encoder_layer,
args.patch_size, avg_mse, avg_mae, std_mse, std_mae) + '\n')
f.flush()
f.close()