NeuralForecast 超参数优化
flyfish
不使用超参数优化的方式
import numpy as np
import pandas as pd
from IPython.display import display, Markdown
import matplotlib.pyplot as plt
from neuralforecast import NeuralForecast
from neuralforecast.models import NBEATS, NHITS
from neuralforecast.utils import AirPassengersDF
# Split data and declare panel dataset
Y_df = AirPassengersDF
Y_train_df = Y_df[Y_df.ds<='1959-12-31'] # 132 train
Y_test_df = Y_df[Y_df.ds>'1959-12-31'] # 12 test
# Fit and predict with NBEATS and NHITS models
horizon = len(Y_test_df)
models = [NBEATS(input_size=2 * horizon, h=horizon, max_steps=50),
NHITS(input_size=2 * horizon, h=horizon, max_steps=50)]
nf = NeuralForecast(models=models, freq='M')
nf.fit(df=Y_train_df)
Y_hat_df = nf.predict().reset_index()
# Plot predictions
fig, ax = plt.subplots(1, 1, figsize = (20, 7))
Y_hat_df = Y_test_df.merge(Y_hat_df, how='left', on=['unique_id', 'ds'])
plot_df = pd.concat([Y_train_df, Y_hat_df]).set_index('ds')
plot_df[['y', 'NBEATS', 'NHITS']].plot(ax=ax, linewidth=2)
ax.set_title('AirPassengers Forecast', fontsize=22)
ax.set_ylabel('Monthly Passengers', fontsize=20)
ax.set_xlabel('Timestamp [t]', fontsize=20)
ax.legend(prop={'size': 15})
ax.grid()
plt.show()
# # Hyperparameter Optimization
# The main steps of hyperparameter tuning are:
#
# 1. Define training and validation sets.
# 2. Define search space.
# 3. Sample configurations with a search algorithm, train models, and evaluate them on the validation set.
# 4. Select and store the best model.
##超参数优化。
#超参数调优的主要步骤如下:
#。
#1.定义培训和验证集。
#2.定义搜索空间。
#3.使用搜索算法对配置进行采样,训练模型,并在验证集上对其进行评估。
#4.选择并存储最好的型号。
# !pip install neuralforecast hyperopt
from neuralforecast.utils import AirPassengersDF
Y_df = AirPassengersDF
Y_df.head()
from ray import tune
nhits_config = {
"max_steps": 100, # Number of SGD steps
"input_size": 24, # Size of input window
"learning_rate": tune.loguniform(1e-5, 1e-1), # Initial Learning rate
"n_pool_kernel_size": tune.choice([[2, 2, 2], [16, 8, 1]]), # MaxPool's Kernelsize
"n_freq_downsample": tune.choice([[168, 24, 1], [24, 12, 1], [1, 1, 1]]), # Interpolation expressivity ratios
"val_check_steps": 50, # Compute validation every 50 steps
"random_seed": tune.randint(1, 10), # Random seed
}
from ray.tune.search.hyperopt import HyperOptSearch
from neuralforecast.losses.pytorch import MAE
from neuralforecast.auto import AutoNHITS
model = AutoNHITS(h=12,
loss=MAE(),
config=nhits_config,
search_alg=HyperOptSearch(),
backend='ray',
num_samples=10)
from neuralforecast import NeuralForecast
nf = NeuralForecast(models=[model], freq='M')
nf.fit(df=Y_df, val_size=24)
results = nf.models[0].results.get_dataframe()
results.head()
Y_hat_df = nf.predict()
Y_hat_df = Y_hat_df.reset_index()
Y_hat_df.head()
import optuna
optuna.logging.set_verbosity(optuna.logging.WARNING) # Use this to disable training prints from optuna
def config_nhits(trial):
return {
"max_steps": 100, # Number of SGD steps
"input_size": 24, # Size of input window
"learning_rate": trial.suggest_loguniform("learning_rate", 1e-5, 1e-1), # Initial Learning rate
"n_pool_kernel_size": trial.suggest_categorical("n_pool_kernel_size", [[2, 2, 2], [16, 8, 1]]), # MaxPool's Kernelsize
"n_freq_downsample": trial.suggest_categorical("n_freq_downsample", [[168, 24, 1], [24, 12, 1], [1, 1, 1]]), # Interpolation expressivity ratios
"val_check_steps": 50, # Compute validation every 50 steps
"random_seed": trial.suggest_int("random_seed", 1, 10), # Random seed
}
model = AutoNHITS(h=12,
loss=MAE(),
config=config_nhits,
search_alg=optuna.samplers.TPESampler(),
backend='optuna',
num_samples=10)
nf = NeuralForecast(models=[model], freq='M')
nf.fit(df=Y_df, val_size=24)
results = nf.models[0].results.trials_dataframe()
results.drop(columns='user_attrs_ALL_PARAMS')
Y_hat_df_optuna = nf.predict()
print("Y_hat_df_optuna:\n",Y_hat_df_optuna)
Y_hat_df_optuna = Y_hat_df_optuna.reset_index()
Y_hat_df_optuna.head()
print("Y_hat_df_optuna.head():\n",Y_hat_df_optuna.head())
import pandas as pd
import matplotlib.pyplot as plt
fig, ax = plt.subplots(1, 1, figsize = (20, 7))
plot_df = pd.concat([Y_df, Y_hat_df]).reset_index()
plt.plot(plot_df['ds'], plot_df['y'], label='y')
plt.plot(plot_df['ds'], plot_df['AutoNHITS'], label='Ray')
plt.plot(Y_hat_df_optuna['ds'], Y_hat_df_optuna['AutoNHITS'], label='Optuna')
ax.set_title('AirPassengers Forecast', fontsize=22)
ax.set_ylabel('Monthly Passengers', fontsize=20)
ax.set_xlabel('Timestamp [t]', fontsize=20)
ax.legend(prop={'size': 15})
ax.grid()
plt.show()