-
Notifications
You must be signed in to change notification settings - Fork 495
/
Copy pathtest_model_performance.py
336 lines (281 loc) · 10.5 KB
/
test_model_performance.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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
#!/usr/bin/env python3
import json
import logging
import os
import pathlib
import time
import numpy as np
import pandas as pd
import plotly.graph_objects as go
import torch
from plotly.subplots import make_subplots
from neuralprophet import NeuralProphet, set_random_seed
log = logging.getLogger("NP.test")
log.setLevel("INFO")
log.parent.setLevel("INFO")
try:
from plotly_resampler import unregister_plotly_resampler
plotly_resampler_installed = True
except ImportError:
plotly_resampler_installed = False
log.error("Importing plotly failed. Interactive plots will not work.")
DIR = pathlib.Path(__file__).parent.parent.absolute()
DATA_DIR = os.path.join(DIR, "tests", "test-data")
PEYTON_FILE = os.path.join(DATA_DIR, "wp_log_peyton_manning.csv")
AIR_FILE = os.path.join(DATA_DIR, "air_passengers.csv")
YOS_FILE = os.path.join(DATA_DIR, "yosemite_temps.csv")
ENERGY_PRICE_DAILY_FILE = os.path.join(DATA_DIR, "tutorial04_kaggle_energy_daily_temperature.csv")
# Important to set seed for reproducibility
set_random_seed(42)
def get_system_speed():
repeats = 5
benchmarks = np.array([])
for a in range(0, repeats):
start = time.time()
for i in range(0, 1000):
for x in range(1, 1000):
3.141592 * 2**x
for x in range(1, 1000):
float(x) / 3.141592
for x in range(1, 1000):
float(3.141592) / x
end = time.time()
duration = end - start
duration = round(duration, 3)
benchmarks = np.append(benchmarks, duration)
log.info(f"System speed: {round(np.mean(benchmarks), 5)}s")
log.info(f"Standart deviation: {round(np.std(benchmarks), 5)}s")
return benchmarks.mean(), benchmarks.std()
def create_metrics_plot(metrics):
# Deactivate the resampler since it is not compatible with kaleido (image export)
if plotly_resampler_installed:
unregister_plotly_resampler()
# Plotly params
prediction_color = "#2d92ff"
actual_color = "black"
line_width = 2
xaxis_args = {"showline": True, "mirror": True, "linewidth": 1.5, "showgrid": False}
yaxis_args = {
"showline": True,
"mirror": True,
"linewidth": 1.5,
"showgrid": False,
"rangemode": "tozero",
"type": "log",
}
layout_args = {
"autosize": True,
"template": "plotly_white",
"margin": go.layout.Margin(l=0, r=10, b=0, t=30, pad=0),
"font": dict(size=10),
"title": dict(font=dict(size=10)),
"width": 1000,
"height": 200,
}
metric_cols = [col for col in metrics.columns if not ("_val" in col or col == "RegLoss" or col == "epoch")]
fig = make_subplots(rows=1, cols=len(metric_cols), subplot_titles=metric_cols)
for i, metric in enumerate(metric_cols):
fig.add_trace(
go.Scatter(
y=metrics[metric],
name=metric,
mode="lines",
line=dict(color=prediction_color, width=line_width),
legendgroup=metric,
),
row=1,
col=i + 1,
)
if f"{metric}_val" in metrics.columns:
fig.add_trace(
go.Scatter(
y=metrics[f"{metric}_val"],
name=f"{metric}_val",
mode="lines",
line=dict(color=actual_color, width=line_width),
legendgroup=metric,
),
row=1,
col=i + 1,
)
if metric == "Loss":
fig.add_trace(
go.Scatter(
y=metrics["RegLoss"],
name="RegLoss",
mode="lines",
line=dict(color=actual_color, width=line_width),
legendgroup=metric,
),
row=1,
col=i + 1,
)
fig.update_xaxes(xaxis_args)
fig.update_yaxes(yaxis_args)
fig.update_layout(layout_args)
return fig
def test_PeytonManning():
df = pd.read_csv(PEYTON_FILE)
m = NeuralProphet()
df_train, df_test = m.split_df(df=df, freq="D", valid_p=0.1)
system_speed, std = get_system_speed()
start = time.time()
metrics = m.fit(df_train, validation_df=df_test, freq="D", deterministic=True) # , early_stopping=True)
end = time.time()
accuracy_metrics = metrics.to_dict("records")[-1]
accuracy_metrics["time"] = round(end - start, 2)
accuracy_metrics["system_performance"] = round(system_speed, 5)
accuracy_metrics["system_std"] = round(std, 5)
with open(os.path.join(DIR, "tests", "metrics", "PeytonManning.json"), "w") as outfile:
json.dump(accuracy_metrics, outfile)
create_metrics_plot(metrics).write_image(os.path.join(DIR, "tests", "metrics", "PeytonManning.svg"))
def test_YosemiteTemps():
df = pd.read_csv(YOS_FILE)
m = NeuralProphet(
# learning_rate=0.01,
# epochs=3,
n_lags=36,
n_forecasts=12,
changepoints_range=0.9,
n_changepoints=30,
weekly_seasonality=False,
)
df_train, df_test = m.split_df(df=df, freq="5min", valid_p=0.1)
system_speed, std = get_system_speed()
start = time.time()
metrics = m.fit(
df_train,
validation_df=df_test,
freq="5min",
deterministic=True,
) # , early_stopping=True)
end = time.time()
accuracy_metrics = metrics.to_dict("records")[-1]
accuracy_metrics["time"] = round(end - start, 2)
accuracy_metrics["system_performance"] = round(system_speed, 5)
accuracy_metrics["system_std"] = round(std, 5)
with open(os.path.join(DIR, "tests", "metrics", "YosemiteTemps.json"), "w") as outfile:
json.dump(accuracy_metrics, outfile)
create_metrics_plot(metrics).write_image(os.path.join(DIR, "tests", "metrics", "YosemiteTemps.svg"))
def test_AirPassengers():
df = pd.read_csv(AIR_FILE)
m = NeuralProphet(seasonality_mode="multiplicative")
df_train, df_test = m.split_df(df=df, freq="MS", valid_p=0.1)
system_speed, std = get_system_speed()
start = time.time()
metrics = m.fit(df_train, validation_df=df_test, freq="MS", deterministic=True) # , early_stopping=True)
end = time.time()
accuracy_metrics = metrics.to_dict("records")[-1]
accuracy_metrics["time"] = round(end - start, 2)
accuracy_metrics["system_performance"] = round(system_speed, 5)
accuracy_metrics["system_std"] = round(std, 5)
with open(os.path.join(DIR, "tests", "metrics", "AirPassengers.json"), "w") as outfile:
json.dump(accuracy_metrics, outfile)
create_metrics_plot(metrics).write_image(os.path.join(DIR, "tests", "metrics", "AirPassengers.svg"))
def test_EnergyPriceDaily():
df = pd.read_csv(ENERGY_PRICE_DAILY_FILE)
df["temp"] = df["temperature"]
m = NeuralProphet(
# learning_rate=0.01,
# epochs=3,
n_forecasts=7,
n_changepoints=0,
yearly_seasonality=True,
weekly_seasonality=True,
daily_seasonality=False,
n_lags=14,
)
m.add_lagged_regressor("temp", n_lags=3)
m.add_future_regressor("temperature")
df_train, df_test = m.split_df(df=df, freq="D", valid_p=0.1)
system_speed, std = get_system_speed()
start = time.time()
metrics = m.fit(
df_train,
validation_df=df_test,
freq="D",
deterministic=True,
) # , early_stopping=True)
end = time.time()
accuracy_metrics = metrics.to_dict("records")[-1]
accuracy_metrics["time"] = round(end - start, 2)
accuracy_metrics["system_performance"] = round(system_speed, 5)
accuracy_metrics["system_std"] = round(std, 5)
with open(os.path.join(DIR, "tests", "metrics", "EnergyPriceDaily.json"), "w") as outfile:
json.dump(accuracy_metrics, outfile)
create_metrics_plot(metrics).write_image(os.path.join(DIR, "tests", "metrics", "EnergyPriceDaily.svg"))
def test_EnergyDailyDeep():
# Temporary Test for on-the-fly sampling - very time consuming!
df = pd.read_csv(ENERGY_PRICE_DAILY_FILE)
df = df[df["ds"] < "2018-01-01"]
df["temp"] = df["temperature"]
df["ds"] = pd.to_datetime(df["ds"])
df["y"] = pd.to_numeric(df["y"], errors="coerce")
df["ID"] = "test"
# Conditional Seasonality
df["winter"] = np.where(
df["ds"].dt.month.isin(
[
10,
11,
12,
1,
2,
3,
]
),
1,
0,
)
df["summer"] = np.where(df["ds"].dt.month.isin([4, 5, 6, 7, 8, 9]), 1, 0)
df["winter"] = pd.to_numeric(df["winter"], errors="coerce")
df["summer"] = pd.to_numeric(df["summer"], errors="coerce")
# Normalize Temperature
df["temp"] = (df["temp"] - 65.0) / 50.0
# df
df = df[["ID", "ds", "y", "temp", "winter", "summer"]]
# Hyperparameter
tuned_params = {
"n_lags": 15,
"newer_samples_weight": 2.0,
"n_changepoints": 0,
"yearly_seasonality": 10,
"weekly_seasonality": False, # due to conditional daily seasonality
"daily_seasonality": False, # due to data freq
"batch_size": 64,
"ar_layers": [16, 32, 16, 8],
"lagged_reg_layers": [32, 16],
# not tuned
"n_forecasts": 7,
"learning_rate": 0.001,
"epochs": 30,
"trend_global_local": "global",
"season_global_local": "global",
"drop_missing": True,
"normalize": "standardize",
}
# Uncertainty Quantification
confidence_lv = 0.98
quantile_list = [round(((1 - confidence_lv) / 2), 2), round((confidence_lv + (1 - confidence_lv) / 2), 2)]
# Check if GPU is available
use_gpu = torch.cuda.is_available()
# Set trainer configuration
trainer_configs = {
"accelerator": "gpu" if use_gpu else "cpu",
}
print(f"Using {'GPU' if use_gpu else 'CPU'}")
# Model
m = NeuralProphet(**tuned_params, **trainer_configs, quantiles=quantile_list)
# Lagged Regressor
m.add_lagged_regressor(names="temp", n_lags=7, normalize="standardize")
# Conditional Seasonality
m.add_seasonality(name="winter", period=7, fourier_order=6, condition_name="winter")
m.add_seasonality(name="summer", period=7, fourier_order=6, condition_name="summer")
# Holidays
m.add_country_holidays(country_name="US", lower_window=-1, upper_window=1)
# Split
df_train = df[df["ds"] < "2016-05-01"]
df_test = df[df["ds"] >= "2016-05-01"]
# Training & Predict
_ = m.fit(df=df_train, freq="D", num_workers=4)
_ = m.predict(df_test)