Exemplo n.º 1
0
    hidden_size=25,
    n_rnn_layers=1,
    dropout=0.2,
    batch_size=16,
    n_epochs=20,
    optimizer_kwargs={'lr': 1e-3},
    model_name='Forecast_LSTM_next_hour',
    log_tensorboard=True,
    random_state=42
)

# Either train a new model or load best model from checkpoint
if train_new_model==True:
    my_model.fit(train_transformed, val_series=val_transformed, verbose=True)
else:
    my_model = RNNModel.load_from_checkpoint(model_name='Forecast_LSTM_next_hour', best=True)

# Evaluate Predictions 
def eval_model(model):
    pred_series = model.predict(n=len(val_transformed))

    plt.figure(figsize=(8,5))
    val_transformed.plot(label='actual')
    pred_series.plot(label='forecast')
    plt.title('MAPE: {:.2f}%'.format(mape(pred_series, val_transformed)))
    plt.legend()
    plt.show()

def backtest(model):
    backtest_series = model.historical_forecasts(series_transformed,
                                start=pd.Timestamp("2021-03-01 00:00:00+00:00"),
Exemplo n.º 2
0
my_model.fit(train_transformed, val_series=val_transformed, verbose=True)


def eval_model(model):
    pred_series = model.predict(n=96)
    plt.figure(figsize=(8, 5))
    series_transformed.plot(label='actual')
    pred_series.plot(label='forecast')
    plt.title('MAPE: {:.2f}%'.format(mape(pred_series, val_transformed)))
    plt.legend()


eval_model(my_model)

best_model = RNNModel.load_from_checkpoint(model_name='Eq_RNN', best=True)
eval_model(best_model)

backtest_series = my_model.historical_forecasts(series_transformed,
                                                start=pd.Timestamp('20021231'),
                                                forecast_horizon=12,
                                                retrain=False,
                                                verbose=True)

plt.figure(figsize=(8, 5))
series_transformed.plot(label='actual')
backtest_series.plot(label='backtest')
plt.legend()
plt.title('Backtest, starting Jan 2003, 12-months horizon')
print('MAPE: {:.2f}%'.format(
    mape(transformer.inverse_transform(series_transformed),