-
Notifications
You must be signed in to change notification settings - Fork 29
/
nhits_multivariate.py
156 lines (136 loc) · 6.62 KB
/
nhits_multivariate.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
import os
import pickle
import time
import argparse
import pandas as pd
from hyperopt import fmin, tpe, hp, Trials, STATUS_OK
from src.losses.numpy import mae, mse
from src.experiments.utils import hyperopt_tunning
def get_experiment_space(args):
space= {# Architecture parameters
'model':'nhits',
'mode': 'simple',
'n_time_in': hp.choice('n_time_in', [5*args.horizon]),
'n_time_out': hp.choice('n_time_out', [args.horizon]),
'n_x_hidden': hp.choice('n_x_hidden', [0]),
'n_s_hidden': hp.choice('n_s_hidden', [0]),
'shared_weights': hp.choice('shared_weights', [False]),
'activation': hp.choice('activation', ['ReLU']),
'initialization': hp.choice('initialization', ['lecun_normal']),
'stack_types': hp.choice('stack_types', [ 3*['identity'] ]),
'n_blocks': hp.choice('n_blocks', [ 3*[1]]),
'n_layers': hp.choice('n_layers', [ 9*[2] ]),
'n_hidden': hp.choice('n_hidden', [ 512 ]),
'n_pool_kernel_size': hp.choice('n_pool_kernel_size', [ 3*[1], 3*[2], 3*[4], 3*[8], [8, 4, 1], [16, 8, 1] ]),
'n_freq_downsample': hp.choice('n_freq_downsample', [ [168, 24, 1], [24, 12, 1],
[180, 60, 1], [60, 8, 1],
[40, 20, 1]
]),
'pooling_mode': hp.choice('pooling_mode', [ 'max' ]),
'interpolation_mode': hp.choice('interpolation_mode', ['linear']),
# Regularization and optimization parameters
'batch_normalization': hp.choice('batch_normalization', [False]),
'dropout_prob_theta': hp.choice('dropout_prob_theta', [ 0 ]),
'dropout_prob_exogenous': hp.choice('dropout_prob_exogenous', [0]),
'learning_rate': hp.choice('learning_rate', [0.001]),
'lr_decay': hp.choice('lr_decay', [0.5] ),
'n_lr_decays': hp.choice('n_lr_decays', [3]),
'weight_decay': hp.choice('weight_decay', [0] ),
'max_epochs': hp.choice('max_epochs', [None]),
'max_steps': hp.choice('max_steps', [1_000]),
'early_stop_patience': hp.choice('early_stop_patience', [10]),
'eval_freq': hp.choice('eval_freq', [50]),
'loss_train': hp.choice('loss', ['MAE']),
'loss_hypar': hp.choice('loss_hypar', [0.5]),
'loss_valid': hp.choice('loss_valid', ['MAE']),
'l1_theta': hp.choice('l1_theta', [0]),
# Data parameters
'normalizer_y': hp.choice('normalizer_y', [None]),
'normalizer_x': hp.choice('normalizer_x', [None]),
'complete_windows': hp.choice('complete_windows', [True]),
'frequency': hp.choice('frequency', ['H']),
'seasonality': hp.choice('seasonality', [24]),
'idx_to_sample_freq': hp.choice('idx_to_sample_freq', [1]),
'val_idx_to_sample_freq': hp.choice('val_idx_to_sample_freq', [1]),
'batch_size': hp.choice('batch_size', [1]),
'n_windows': hp.choice('n_windows', [256]),
'random_seed': hp.quniform('random_seed', 1, 10, 1)}
return space
def main(args):
#----------------------------------------------- Load Data -----------------------------------------------#
Y_df = pd.read_csv(f'./data/{args.dataset}/M/df_y.csv')
X_df = None
S_df = None
print('Y_df: ', Y_df.head())
if args.dataset == 'ETTm2':
len_val = 11520
len_test = 11520
if args.dataset == 'Exchange':
len_val = 760
len_test = 1517
if args.dataset == 'ECL':
len_val = 2632
len_test = 5260
if args.dataset == 'traffic':
len_val = 1756
len_test = 3508
if args.dataset == 'weather':
len_val = 5270
len_test = 10539
if args.dataset == 'ili':
len_val = 97
len_test = 193
space = get_experiment_space(args)
#---------------------------------------------- Directories ----------------------------------------------#
output_dir = f'./results/multivariate/{args.dataset}_{args.horizon}/NHITS/'
os.makedirs(output_dir, exist_ok = True)
assert os.path.exists(output_dir), f'Output dir {output_dir} does not exist'
hyperopt_file = output_dir + f'hyperopt_{args.experiment_id}.p'
if not os.path.isfile(hyperopt_file):
print('Hyperparameter optimization')
#----------------------------------------------- Hyperopt -----------------------------------------------#
trials = hyperopt_tunning(space=space, hyperopt_max_evals=args.hyperopt_max_evals, loss_function_val=mae,
loss_functions_test={'mae':mae, 'mse': mse},
Y_df=Y_df, X_df=X_df, S_df=S_df, f_cols=[],
evaluate_train=True,
ds_in_val=len_val, ds_in_test=len_test,
return_forecasts=False,
results_file = hyperopt_file,
save_progress=True,
loss_kwargs={})
with open(hyperopt_file, "wb") as f:
pickle.dump(trials, f)
else:
print('Hyperparameter optimization already done!')
def parse_args():
desc = "Example of hyperparameter tuning"
parser = argparse.ArgumentParser(description=desc)
parser.add_argument('--hyperopt_max_evals', type=int, help='hyperopt_max_evals')
parser.add_argument('--experiment_id', default=None, required=False, type=str, help='string to identify experiment')
return parser.parse_args()
if __name__ == '__main__':
# parse arguments
args = parse_args()
if args is None:
exit()
horizons = [96, 192, 336, 720]
ILI_horizons = [24, 36, 48, 60]
datasets = ['ETTm2', 'Exchange', 'weather', 'ili', 'ECL', 'traffic']
for dataset in datasets:
# Horizon
if dataset == 'ili':
horizons_dataset = ILI_horizons
else:
horizons_dataset = horizons
for horizon in horizons_dataset:
print(50*'-', dataset, 50*'-')
print(50*'-', horizon, 50*'-')
start = time.time()
args.dataset = dataset
args.horizon = horizon
main(args)
print('Time: ', time.time() - start)
main(args)
# source ~/anaconda3/etc/profile.d/conda.sh
# conda activate nixtla
# CUDA_VISIBLE_DEVICES=0 python nhits_multivariate.py --hyperopt_max_evals 10 --experiment_id "eval_train"