-
Notifications
You must be signed in to change notification settings - Fork 3
/
nbeats-test.sync.py
205 lines (166 loc) · 6.14 KB
/
nbeats-test.sync.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
# %%
import os
def isnotebook():
try:
shell = get_ipython().__class__.__name__
if shell == 'ZMQInteractiveShell':
return True # Jupyter notebook or qtconsole
elif shell == 'TerminalInteractiveShell':
return False # Terminal running IPython
else:
return False # Other type (?)
except NameError:
return False
if isnotebook():
from tqdm.notebook import tqdm
os.environ["CUDA_VISIBLE_DEVICES"]="0"
# os.environ["CUDA_VISIBLE_DEVICES"]="0,1,2,3"
else:
from tqdm import tqdm
os.environ["CUDA_VISIBLE_DEVICES"]="1"
# %%
from itertools import product
from pathlib import Path
import warnings
import keras_tuner as kt
import numpy as np
import pandas as pd
import tensorflow as tf
from collections.abc import Iterable
from hypernbeat import HyperNBeats
from loss import LOSSES
from data import M4Meta,read_data
tf.config.threading.set_inter_op_parallelism_threads(8)
warnings.filterwarnings("ignore")
# %%
strategy = tf.distribute.MirroredStrategy()
print('Number of devices: {}'.format(strategy.num_replicas_in_sync))
# %%
def last_insample_window(timeseries, insample_size):
"""
The last window of insample size of all timeseries.
This function does not support batching and does not reshuffle timeseries.
:return: Last insample window of all timeseries. Shape "timeseries, insample size"
"""
insample = np.zeros((len(timeseries), insample_size))
for i, ts in enumerate(timeseries):
ts_last_window = ts[-insample_size:]
insample[i, -len(ts):] = ts_last_window
return insample
def predict_m4_timeseries(timeseries, targets, model, loss):
if loss=="mase":
insample_size=model.input_shape[0][1]
x_test=last_insample_window(timeseries,insample_size)
y_pred=model.predict((x_test,targets)).squeeze()
else:
insample_size=model.input_shape[1]
x_test=last_insample_window(timeseries,insample_size)
y_pred=model.predict(x_test).squeeze()
return y_pred
# %%
def ensemble_member(freq,lookback,loss,model_type="interpretable",overwrite=False,parallel=False):
timeseries,targets=read_data(freq)
history_size=M4Meta.history_size[freq]
batch_size_power=14
if model_type=="interpretable":
project_name=f"{freq}_{lookback}_{loss}"
else:
project_name=f"{model_type}_{freq}_{lookback}_{loss}"
print(project_name)
hp=kt.HyperParameters()
hp.Fixed("model_type", model_type)
hp.Fixed("lookback",lookback)
hp.Fixed("nb_blocks_per_stack",3)
hp.Fixed("trend_layer_units_power",8)
hp.Fixed("season_layer_units_power",11)
hp.Fixed("degree_of_polynomial",2)
hp.Fixed("loss_fn",loss)
hp.Fixed("batch_size_power",batch_size_power)
hp.Fixed("init_lr",1e-3)
hp.Fixed("history_size",history_size)
hp.Fixed("lr_decay_rate", 0.5)
hp.Fixed("nb_harmonics", 1)
hp.Fixed("epochs", 100)
hp.Fixed("val_size", 0.1)
hp.Fixed("stacks_num", 15)
hp.Fixed("parallel", parallel)
tuner = kt.RandomSearch(
HyperNBeats(freq),
hyperparameters=hp,
objective=kt.Objective("val_loss", direction="min"),
max_trials=15,
overwrite=overwrite,
directory="kt",
project_name=project_name,
seed=64
)
es_callback=tf.keras.callbacks.EarlyStopping(
monitor='val_loss', patience=20,
restore_best_weights=True,
verbose=1
)
logdir = Path("logs")/"keras_tuner_log"
tensorboard_callback = tf.keras.callbacks.TensorBoard(logdir, histogram_freq=1)
tuner.search(timeseries, targets,
# epochs=100,
# steps_per_epoch=M4Meta.iterations[freq] // 100,
verbose=2,
callbacks=[es_callback,tensorboard_callback]
)
evaluate_path=Path('./nbeats_result')/project_name
evaluate_path.mkdir(exist_ok=True,parents=True)
print("Recording pred...")
for i,model in tqdm(enumerate(tuner.get_best_models(num_models=15))):
y_pred=predict_m4_timeseries(timeseries, targets, model, loss)
np.save(evaluate_path/f"{i}.npy",y_pred)
return tuner
# %%
class Evaluater(object):
def __init__(self,freq):
self._freq = freq
self.timeseries,self.targets=read_data(freq)
self.eval_fn=LOSSES['smape']
def ensemble_results(self,results):
if not isinstance(results, Iterable):
results=[results]
preds=[np.load(fp) for fp in results]
print(f"Ensembling from {len(preds)} models:")
# mean_ensemble=np.mean(np.stack(preds),axis=0)
median_ensemble=np.median(np.stack(preds),axis=0)
return median_ensemble
def __call__(self,results):
y_pred=self.ensemble_results(results)
median_score=self.eval_fn(self.targets, y_pred).numpy()
print(f"Median ensemble: {median_score}")
return median_score
# %%
def generic_model(freq,repeat=10):
results=[]
for lookback,loss in product([2,3,4,5,6,7],LOSSES.keys()):
project_name=f"generic_{freq}_{lookback}_{loss}"
member_results=list(Path('./nbeats_result').glob(f"{project_name}/*.npy"))
select_results=np.random.choice(member_results,size=repeat) # For accurate reporduction
results.extend(select_results)
return results
def interpretable_model(freq,repeat=10):
results=[]
for lookback,loss in product([2,3,4,5,6,7],LOSSES.keys()):
project_name=f"{freq}_{lookback}_{loss}"
member_results=list(Path('./nbeats_result').glob(f"{project_name}/*.npy"))
select_results=np.random.choice(member_results,size=repeat) # For accurate reporduction
results.extend(select_results)
return results
# %%
# %%
if __name__ == "__main__":
# freq="Yearly"
# results=Path('./nbeats_result').glob(f"{freq}_[234567]_*ma?e/[0-9].npy")
# evaluate(results,freq)
for freq,lookback,loss in product(["Yearly"],[2,3,4,5,6,7],LOSSES.keys()):
ensemble_member(freq,lookback,loss,model_type="generic")
# ensemble_member(freq,lookback,loss)
# evaluater=Evaluater(freq)
# results=interpretable_model(freq)
#
# evaluater(results)
# %%