-
Notifications
You must be signed in to change notification settings - Fork 483
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Minor| Fix neural nets regressor shape (#1589)
* fixed tensor shapes in regressor * add test * add shared NN test and add note to forecaster * add tests for shared and coef NN and fix implementations * move future regressor test with NN to separate file and separate test into smaller tests * cleanup * fix SharedNeuralNetsCoefFutureRegressors * isolate issue --------- Co-authored-by: Oskar Triebe <ourownstory@users.noreply.github.com>
- Loading branch information
1 parent
f1a3820
commit 0aafec9
Showing
6 changed files
with
211 additions
and
107 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,195 @@ | ||
#!/usr/bin/env python3 | ||
|
||
import logging | ||
import os | ||
import pathlib | ||
|
||
import pandas as pd | ||
|
||
from neuralprophet import NeuralProphet | ||
|
||
log = logging.getLogger("NP.test") | ||
log.setLevel("DEBUG") | ||
log.parent.setLevel("WARNING") | ||
|
||
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") | ||
|
||
TUTORIAL_FILE = "https://github.com/ourownstory/neuralprophet-data/raw/main/kaggle-energy/datasets/tutorial04.csv" | ||
NROWS = 1028 | ||
EPOCHS = 2 | ||
BATCH_SIZE = 128 | ||
LR = 1.0 | ||
|
||
PLOT = False | ||
|
||
|
||
def test_future_reg_nn(): | ||
log.info("testing: Future Regressors modelled with NNs") | ||
df = pd.read_csv(PEYTON_FILE, nrows=NROWS + 50) | ||
m = NeuralProphet(epochs=EPOCHS, batch_size=BATCH_SIZE, learning_rate=LR, future_regressors_model="neural_nets") | ||
df["A"] = df["y"].rolling(7, min_periods=1).mean() | ||
df["B"] = df["y"].rolling(30, min_periods=1).mean() | ||
df["C"] = df["y"].rolling(7, min_periods=1).mean() | ||
df["D"] = df["y"].rolling(30, min_periods=1).mean() | ||
|
||
regressors_df_future = pd.DataFrame( | ||
data={"A": df["A"][-50:], "B": df["B"][-50:], "C": df["C"][-50:], "D": df["D"][-50:]} | ||
) | ||
df = df[:-50] | ||
m = m.add_future_regressor(name="A") | ||
m = m.add_future_regressor(name="B", mode="additive") | ||
m = m.add_future_regressor(name="C", mode="multiplicative") | ||
m = m.add_future_regressor(name="D", mode="multiplicative") | ||
m.fit(df, freq="D") | ||
future = m.make_future_dataframe(df=df, regressors_df=regressors_df_future, n_historic_predictions=10, periods=50) | ||
forecast = m.predict(df=future) | ||
if PLOT: | ||
m.plot(forecast) | ||
m.plot_components(forecast) | ||
m.plot_parameters() | ||
plt.show() | ||
|
||
|
||
def test_future_reg_nn_shared(): | ||
log.info("testing: Future Regressors modelled with NNs shared") | ||
df = pd.read_csv(PEYTON_FILE, nrows=NROWS + 50) | ||
m = NeuralProphet( | ||
epochs=EPOCHS, batch_size=BATCH_SIZE, learning_rate=LR, future_regressors_model="shared_neural_nets" | ||
) | ||
df["A"] = df["y"].rolling(7, min_periods=1).mean() | ||
df["B"] = df["y"].rolling(30, min_periods=1).mean() | ||
df["C"] = df["y"].rolling(7, min_periods=1).mean() | ||
df["D"] = df["y"].rolling(30, min_periods=1).mean() | ||
|
||
regressors_df_future = pd.DataFrame( | ||
data={"A": df["A"][-50:], "B": df["B"][-50:], "C": df["C"][-50:], "D": df["D"][-50:]} | ||
) | ||
df = df[:-50] | ||
m = m.add_future_regressor(name="A") | ||
m = m.add_future_regressor(name="B", mode="additive") | ||
m = m.add_future_regressor(name="C", mode="multiplicative") | ||
m = m.add_future_regressor(name="D", mode="multiplicative") | ||
m.fit(df, freq="D") | ||
future = m.make_future_dataframe(df=df, regressors_df=regressors_df_future, n_historic_predictions=10, periods=50) | ||
forecast = m.predict(df=future) | ||
if PLOT: | ||
m.plot(forecast) | ||
m.plot_components(forecast) | ||
m.plot_parameters() | ||
plt.show() | ||
|
||
|
||
def test_future_reg_nn_shared_coef(): | ||
log.info("testing: Future Regressors modelled with NNs shared coef") | ||
df = pd.read_csv(PEYTON_FILE, nrows=NROWS + 50) | ||
m = NeuralProphet( | ||
epochs=EPOCHS, batch_size=BATCH_SIZE, learning_rate=LR, future_regressors_model="shared_neural_nets_coef" | ||
) | ||
df["A"] = df["y"].rolling(7, min_periods=1).mean() | ||
df["B"] = df["y"].rolling(30, min_periods=1).mean() | ||
df["C"] = df["y"].rolling(7, min_periods=1).mean() | ||
df["D"] = df["y"].rolling(30, min_periods=1).mean() | ||
|
||
regressors_df_future = pd.DataFrame( | ||
data={"A": df["A"][-50:], "B": df["B"][-50:], "C": df["C"][-50:], "D": df["D"][-50:]} | ||
) | ||
df = df[:-50] | ||
m = m.add_future_regressor(name="A") | ||
m = m.add_future_regressor(name="B", mode="additive") | ||
m = m.add_future_regressor(name="C", mode="multiplicative") | ||
m = m.add_future_regressor(name="D", mode="multiplicative") | ||
m.fit(df, freq="D") | ||
future = m.make_future_dataframe(df=df, regressors_df=regressors_df_future, n_historic_predictions=10, periods=50) | ||
forecast = m.predict(df=future) | ||
if PLOT: | ||
m.plot(forecast) | ||
m.plot_components(forecast) | ||
m.plot_parameters() | ||
plt.show() | ||
|
||
|
||
def test_future_regressor_nn_2(): | ||
log.info("future regressor with NN") | ||
|
||
df = pd.read_csv(TUTORIAL_FILE, nrows=NROWS) | ||
|
||
m = NeuralProphet( | ||
yearly_seasonality=False, | ||
weekly_seasonality=False, | ||
daily_seasonality=True, | ||
future_regressors_model="neural_nets", # 'linear' default or 'neural_nets' | ||
future_regressors_d_hidden=4, # (int) | ||
future_regressors_num_hidden_layers=2, # (int) | ||
n_forecasts=3, | ||
n_lags=5, | ||
drop_missing=True, | ||
# trainer_config={"accelerator": "gpu"}, | ||
) | ||
df_train, df_val = m.split_df(df, freq="H", valid_p=0.2) | ||
|
||
# Use static plotly in notebooks | ||
# m.set_plotting_backend("plotly") | ||
|
||
# Add the new future regressor | ||
m.add_future_regressor("temperature") | ||
|
||
# Add counrty holidays | ||
m.add_country_holidays("IT", mode="additive", lower_window=-1, upper_window=1) | ||
|
||
metrics = m.fit( | ||
df_train, validation_df=df_val, freq="H", epochs=EPOCHS, learning_rate=LR, early_stopping=True, progress=False | ||
) | ||
|
||
|
||
def test_future_regressor_nn_shared_2(): | ||
log.info("future regressor with NN shared 2") | ||
|
||
df = pd.read_csv(TUTORIAL_FILE, nrows=NROWS) | ||
|
||
m = NeuralProphet( | ||
yearly_seasonality=False, | ||
weekly_seasonality=False, | ||
daily_seasonality=True, | ||
future_regressors_model="shared_neural_nets", | ||
future_regressors_d_hidden=4, | ||
future_regressors_num_hidden_layers=2, | ||
n_forecasts=3, | ||
n_lags=5, | ||
drop_missing=True, | ||
) | ||
df_train, df_val = m.split_df(df, freq="H", valid_p=0.2) | ||
|
||
# Add the new future regressor | ||
m.add_future_regressor("temperature") | ||
|
||
metrics = m.fit( | ||
df_train, validation_df=df_val, freq="H", epochs=EPOCHS, learning_rate=LR, early_stopping=True, progress=False | ||
) | ||
|
||
|
||
# def test_future_regressor_nn_shared_coef_2(): | ||
# log.info("future regressor with NN shared coef 2") | ||
|
||
# df = pd.read_csv(TUTORIAL_FILE, nrows=NROWS) | ||
|
||
# m = NeuralProphet( | ||
# yearly_seasonality=False, | ||
# weekly_seasonality=False, | ||
# daily_seasonality=True, | ||
# future_regressors_model="shared_neural_nets_coef", | ||
# future_regressors_d_hidden=4, | ||
# future_regressors_num_hidden_layers=2, | ||
# n_forecasts=3, | ||
# n_lags=5, | ||
# drop_missing=True, | ||
# ) | ||
# df_train, df_val = m.split_df(df, freq="H", valid_p=0.2) | ||
|
||
# # Add the new future regressor | ||
# m.add_future_regressor("temperature") | ||
|
||
# metrics = m.fit( | ||
# df_train, validation_df=df_val, freq="H", epochs=EPOCHS, learning_rate=LR, early_stopping=True, progress=False | ||
# ) |
Oops, something went wrong.