Skip to content

Commit f131cde

Browse files
authored
[FIX] Deprecate the losses.numpy module (#1471)
1 parent 0ab5baf commit f131cde

7 files changed

Lines changed: 47 additions & 327 deletions

File tree

docs/losses.numpy.html.md

Lines changed: 0 additions & 136 deletions
This file was deleted.

docs/mintlify/docs.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,7 @@
128128
{
129129
"group": "Train/Evaluation",
130130
"pages": [
131-
"losses.pytorch.html",
132-
"losses.numpy.html"
131+
"losses.pytorch.html"
133132
]
134133
},
135134
{

experiments/long_horizon/run_nhits.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,14 @@
22
os.environ["PYTORCH_ENABLE_MPS_FALLBACK"] = "1"
33

44
import argparse
5-
import pandas as pd
5+
import numpy as np
66

77
from ray import tune
88

99
from neuralforecast.auto import AutoNHITS
1010
from neuralforecast.core import NeuralForecast
1111

1212
from neuralforecast.losses.pytorch import MAE, HuberLoss
13-
from neuralforecast.losses.numpy import mae, mse
1413
#from datasetsforecast.long_horizon import LongHorizon, LongHorizonInfo
1514
from datasetsforecast.long_horizon2 import LongHorizon2, LongHorizon2Info
1615

@@ -99,8 +98,8 @@
9998
print('y_true.shape (n_series, n_windows, n_time_out):\t', y_true.shape)
10099
print('y_hat.shape (n_series, n_windows, n_time_out):\t', y_hat.shape)
101100

102-
print('MSE: ', mse(y_hat, y_true))
103-
print('MAE: ', mae(y_hat, y_true))
101+
print('MSE: ', np.mean((y_hat - y_true) ** 2))
102+
print('MAE: ', np.mean(np.abs(y_hat - y_true)))
104103

105104
# Save Outputs
106105
if not os.path.exists(f'./data/{dataset}'):

nbs/docs/tutorials/longhorizon_nhits.ipynb

Lines changed: 29 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
"outputs": [],
4848
"source": [
4949
"%%capture\n",
50-
"!pip install neuralforecast datasetsforecast"
50+
"!pip install neuralforecast datasetsforecast utilsforecast"
5151
]
5252
},
5353
{
@@ -433,35 +433,6 @@
433433
"nf.models[0].results.get_best_result().config"
434434
]
435435
},
436-
{
437-
"cell_type": "code",
438-
"execution_count": null,
439-
"metadata": {},
440-
"outputs": [
441-
{
442-
"name": "stdout",
443-
"output_type": "stream",
444-
"text": [
445-
"Parsed results\n",
446-
"2. y_true.shape (n_series, n_windows, n_time_out):\t (7, 11425, 96)\n",
447-
"2. y_hat.shape (n_series, n_windows, n_time_out):\t (7, 11425, 96)\n"
448-
]
449-
}
450-
],
451-
"source": [
452-
"y_true = Y_hat_df.y.values\n",
453-
"y_hat = Y_hat_df['AutoNHITS'].values\n",
454-
"\n",
455-
"n_series = len(Y_df.unique_id.unique())\n",
456-
"\n",
457-
"y_true = y_true.reshape(n_series, -1, horizon)\n",
458-
"y_hat = y_hat.reshape(n_series, -1, horizon)\n",
459-
"\n",
460-
"print('Parsed results')\n",
461-
"print('2. y_true.shape (n_series, n_windows, n_time_out):\\t', y_true.shape)\n",
462-
"print('2. y_hat.shape (n_series, n_windows, n_time_out):\\t', y_hat.shape)"
463-
]
464-
},
465436
{
466437
"cell_type": "code",
467438
"execution_count": null,
@@ -479,23 +450,25 @@
479450
}
480451
],
481452
"source": [
482-
"fig, axs = plt.subplots(nrows=3, ncols=1, figsize=(10, 11))\n",
483-
"fig.tight_layout()\n",
484-
"\n",
453+
"from utilsforecast.plotting import plot_series \n",
454+
" \n",
485455
"series = ['HUFL','HULL','LUFL','LULL','MUFL','MULL','OT']\n",
486-
"series_idx = 3\n",
487-
"\n",
488-
"for idx, w_idx in enumerate([200, 300, 400]):\n",
489-
" axs[idx].plot(y_true[series_idx, w_idx,:],label='True')\n",
490-
" axs[idx].plot(y_hat[series_idx, w_idx,:],label='Forecast')\n",
491-
" axs[idx].grid()\n",
492-
" axs[idx].set_ylabel(series[series_idx]+f' window {w_idx}', \n",
493-
" fontsize=17)\n",
494-
" if idx==2:\n",
495-
" axs[idx].set_xlabel('Forecast Horizon', fontsize=17)\n",
496-
"plt.legend()\n",
497-
"plt.show()\n",
498-
"plt.close()"
456+
"series_id = series[3] # 'LULL'\n",
457+
"\n",
458+
"series_cutoffs = Y_hat_df.loc[Y_hat_df['unique_id'] == \"LULL\", 'cutoff'].unique() \n",
459+
" \n",
460+
"for w_idx in [200, 300, 400]:\n",
461+
" cutoff = series_cutoffs[w_idx]\n",
462+
" plot_df = Y_hat_df.loc[Y_hat_df['cutoff'] == cutoff, ['unique_id', 'ds', 'y', 'AutoNHITS']]\n",
463+
"\n",
464+
" fig = plot_series(\n",
465+
" df=plot_df[['unique_id', 'ds', 'y']],\n",
466+
" forecasts_df=plot_df[['unique_id', 'ds', 'AutoNHITS']],\n",
467+
" ids=[series_id],\n",
468+
" models=['AutoNHITS'],\n",
469+
" max_insample_length=96\n",
470+
" )\n",
471+
" display(fig)"
499472
]
500473
},
501474
{
@@ -523,10 +496,17 @@
523496
}
524497
],
525498
"source": [
526-
"from neuralforecast.losses.numpy import mae, mse\n",
499+
"from utilsforecast.evaluation import evaluate\n",
500+
"from utilsforecast.losses import mae, mse\n",
501+
"\n",
502+
"eval_df = evaluate(\n",
503+
" df=Y_hat_df.drop(columns=[\"cutoff\"]),\n",
504+
" metrics=[mae, mse],\n",
505+
" agg_fn=\"mean\"\n",
506+
")\n",
527507
"\n",
528-
"print('MAE: ', mae(y_hat, y_true))\n",
529-
"print('MSE: ', mse(y_hat, y_true))"
508+
"print('MAE: ', eval_df.iloc[0][\"AutoNHITS\"])\n",
509+
"print('MSE: ', eval_df.iloc[1][\"AutoNHITS\"])"
530510
]
531511
},
532512
{

nbs/docs/tutorials/longhorizon_transformers.ipynb

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@
5555
"outputs": [],
5656
"source": [
5757
"%%capture\n",
58-
"!pip install neuralforecast datasetsforecast"
58+
"!pip install neuralforecast datasetsforecast utilsforecast"
5959
]
6060
},
6161
{
@@ -557,7 +557,8 @@
557557
"metadata": {},
558558
"outputs": [],
559559
"source": [
560-
"from neuralforecast.losses.numpy import mae"
560+
"from utilsforecast.evaluation import evaluate\n",
561+
"from utilsforecast.losses import mae"
561562
]
562563
},
563564
{
@@ -576,13 +577,15 @@
576577
}
577578
],
578579
"source": [
579-
"mae_informer = mae(Y_hat_df['y'], Y_hat_df['Informer'])\n",
580-
"mae_autoformer = mae(Y_hat_df['y'], Y_hat_df['Autoformer'])\n",
581-
"mae_patchtst = mae(Y_hat_df['y'], Y_hat_df['PatchTST'])\n",
580+
"eval_df = evaluate(\n",
581+
" df=Y_hat_df.drop(columns=[\"cutoff\"]),\n",
582+
" metrics=[mae],\n",
583+
" agg_fn=\"mean\"\n",
584+
")\n",
582585
"\n",
583-
"print(f'Informer: {mae_informer:.3f}')\n",
584-
"print(f'Autoformer: {mae_autoformer:.3f}')\n",
585-
"print(f'PatchTST: {mae_patchtst:.3f}')"
586+
"print('Informer: ', eval_df.iloc[0][\"Informer\"])\n",
587+
"print('Autoformer: ', eval_df.iloc[0][\"Autoformer\"])\n",
588+
"print('PatchTST: ', eval_df.iloc[0][\"PatchTST\"])"
586589
]
587590
},
588591
{

tests/test_common/test_base_auto.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
import logging
22
import warnings
33

4+
import numpy as np
45
import optuna
56
import pandas as pd
67
import pytest
7-
import pytorch_lightning as pl
88
from ray import tune
99

1010
from neuralforecast.common._base_auto import BaseAuto
11-
from neuralforecast.losses.numpy import mae
1211
from neuralforecast.losses.pytorch import MAE, MSE
1312
from neuralforecast.models.mlp import MLP
1413
from neuralforecast.tsdataset import TimeSeriesDataset
@@ -58,7 +57,7 @@ def test_ray_tune(setup_module):
5857
)
5958
auto.fit(dataset=dataset)
6059
y_hat = auto.predict(dataset=dataset)
61-
assert mae(Y_test_df["y"].values, y_hat[:, 0]) < 200
60+
assert np.mean(np.abs(Y_test_df["y"].values - y_hat[:, 0])) < 200
6261

6362

6463
def config_f(trial):
@@ -97,7 +96,7 @@ def test_optuna_tune(setup_module):
9796
auto2.fit(dataset=dataset)
9897
assert isinstance(auto2.results, optuna.Study)
9998
y_hat2 = auto2.predict(dataset=dataset)
100-
assert mae(Y_test_df["y"].values, y_hat2[:, 0]) < 200
99+
assert np.mean(np.abs(Y_test_df["y"].values - y_hat2[:, 0])) < 200
101100
Y_test_df["AutoMLP"] = y_hat2
102101

103102
pd.concat([Y_train_df, Y_test_df]).drop("unique_id", axis=1).set_index("ds").plot()

0 commit comments

Comments
 (0)