Forecasts time series using recurrent neural networks (RNN, LSTM, GRU) with ForecasterRnn and create_and_compile_model. Covers model architecture, training, and multi-series deep learning.
Ships in skforecast. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
Read when SKILL.md points to them.
Use ForecasterRnn when:
Requirements: pip install skforecast[deeplearning] (installs keras)
choosing-a-forecaster (confirm ForecasterRnn is the right choice for the data size and pattern)feature-engineering (RNN models still benefit from cyclical / calendar exogenous features)hyperparameter-optimization (tune RNN architecture and training hyperparameters)prediction-intervals (only conformal intervals are supported for ForecasterRnn)Scan before writing code. Each row lists a rule, the symptom when it is broken, and the recovery. Full pitfall catalog: the troubleshooting-common-errors skill.
| Rule | Symptom | Recovery |
|---|---|---|
lags in ForecasterRnn must match create_and_compile_model(..., lags=...) | Input shape mismatch error during fit | Use the same lags value in both calls |
ForecasterRnn supports only method='conformal' for intervals | Error when calling predict_interval(method='bootstrapping') | Use method='conformal' |
Pass exog to create_and_compile_model if exog is used in fit() / predict() | Architecture mismatch or failure when predicting with exog | Build the model with the same exog you train on |
Scale inputs with transformer_series=MinMaxScaler() | Poor convergence; RNNs are scale-sensitive | Always set a scaler on transformer_series |
fit() does not reset the network weights | Calling fit() again (or backtesting a trained forecaster) resumes training from the learned weights | Create a new ForecasterRnn from the untrained model (the forecaster stores a copy, so the original model object stays untrained) |
Custom Keras models must use the layer names of create_and_compile_model | ValueError: No such layer: series_input (or output_dense_td_layer) when creating the forecaster | Name the inputs series_input / exog_input, the output layer output_dense_td_layer, and return shape (steps, n_levels) |
import pandas as pd
from skforecast.deep_learning import ForecasterRnn, create_and_compile_model
from sklearn.preprocessing import MinMaxScaler
# 1. Prepare data (DataFrame with DatetimeIndex, columns = series)
series = pd.read_csv('data.csv', index_col='date', parse_dates=True)
series = series.asfreq('h')
# 2. Create and compile a Keras model
model = create_and_compile_model(
series=series,
lags=48,
steps=24,
levels=series.columns.tolist(), # All series
recurrent_layer='LSTM', # 'LSTM', 'GRU', or 'RNN'
recurrent_units=[64, 32], # Units per recurrent layer
dense_units=[32], # Units per dense layer
compile_kwargs={'optimizer': 'adam', 'loss': 'mse'},
)
# 3. Create forecaster
forecaster = ForecasterRnn(
levels=series.columns.tolist(),
lags=48,
estimator=model,
transformer_series=MinMaxScaler(feature_range=(0, 1)),
fit_kwargs={'epochs': 50, 'batch_size': 32, 'verbose': 0},
)
# 4. Train
forecaster.fit(series=series)
# 5. Predict
predictions = forecaster.predict(steps=24)
from skforecast.deep_learning import create_and_compile_model
# Simple LSTM
model = create_and_compile_model(
series=series,
lags=48,
steps=24,
levels='target',
recurrent_layer='LSTM',
recurrent_units=[64],
dense_units=[32],
compile_kwargs={'optimizer': 'adam', 'loss': 'mse'},
)
# Stacked LSTM (multiple recurrent layers)
model = create_and_compile_model(
series=series,
lags=48,
steps=24,
levels=series.columns.tolist(),
recurrent_layer='LSTM',
recurrent_units=[128, 64, 32], # 3 stacked LSTM layers
dense_units=[64, 32], # 2 dense layers
compile_kwargs={'optimizer': 'adam', 'loss': 'mse'},
)
# GRU variant (faster training)
model = create_and_compile_model(
series=series,
lags=48,
steps=24,
levels='target',
recurrent_layer='GRU',
recurrent_units=[64],
dense_units=[32],
compile_kwargs={'optimizer': 'adam', 'loss': 'mse'},
)
# Advanced: customize layer kwargs
model = create_and_compile_model(
series=series,
lags=48,
steps=24,
levels=series.columns.tolist(),
recurrent_layer='LSTM',
recurrent_units=[128, 64],
recurrent_layers_kwargs={'activation': 'tanh'}, # default
dense_units=[64],
dense_layers_kwargs={'activation': 'relu'}, # default
output_dense_layer_kwargs={'activation': 'linear'}, # default
compile_kwargs={'optimizer': 'adam', 'loss': 'mse'},
model_name='my_lstm_model',
)
When using exogenous variables, pass exog to create_and_compile_model so it
builds the correct architecture (uses TimeDistributed layers internally).
# exog must be a DataFrame covering the training period
exog = pd.DataFrame({'temperature': [...], 'holiday': [...]}, index=series.index)
model = create_and_compile_model(
series=series,
lags=48,
steps=24,
levels=series.columns.tolist(),
exog=exog, # Passes exog info to build architecture
recurrent_layer='LSTM',
recurrent_units=[64, 32],
dense_units=[32],
compile_kwargs={'optimizer': 'adam', 'loss': 'mse'},
)
forecaster = ForecasterRnn(
levels=series.columns.tolist(),
lags=48,
estimator=model,
fit_kwargs={'epochs': 50, 'batch_size': 32, 'verbose': 0},
)
forecaster.fit(series=series, exog=exog)
predictions = forecaster.predict(steps=24, exog=exog_test) # exog_test covers forecast horizon
ForecasterRnn finds the inputs and the output of the model by layer name, so a
custom model must follow the conventions of create_and_compile_model: input
named series_input, output layer named output_dense_td_layer, and model
output of shape (steps, n_levels).
import keras
# Build your own model for full control (single level, no exog)
lags, steps, n_series, n_levels = 48, 24, 1, 1
inputs = keras.layers.Input(shape=(lags, n_series), name='series_input')
x = keras.layers.LSTM(64, return_sequences=True)(inputs)
x = keras.layers.LSTM(32)(x)
x = keras.layers.Dense(32, activation='relu')(x)
x = keras.layers.Dense(steps * n_levels, name='output_dense_td_layer')(x)
outputs = keras.layers.Reshape((steps, n_levels))(x) # Output shape (steps, n_levels)
model = keras.Model(inputs=inputs, outputs=outputs)
model.compile(optimizer='adam', loss='mse')
forecaster = ForecasterRnn(
levels='target',
lags=48,
estimator=model,
transformer_series=MinMaxScaler(feature_range=(0, 1)),
fit_kwargs={'epochs': 100, 'batch_size': 32},
)
Multi-series custom model: For N levels, the output layer should be
Dense(steps * n_levels, name='output_dense_td_layer')followed byReshape((steps, n_levels)). For models with exogenous variables, see references/architecture-options.md.
Split the data chronologically into train, validation and test. Pass the
validation data through fit_kwargs with the keys series_val (and exog_val
if the model uses exog). They are not Keras arguments: the forecaster extracts
them to build the validation set and passes the remaining keys to model.fit().
import keras
import numpy as np
from keras.callbacks import EarlyStopping
# Chronological split (never random)
n_val, n_test = 24 * 60, 24 * 30
series_train = series.iloc[:-(n_val + n_test)]
series_val = series.iloc[-(n_val + n_test):-n_test]
keras.utils.set_random_seed(123) # Reproducible weight initialization and training
model = create_and_compile_model(
series=series, lags=48, steps=24, levels=['target'], recurrent_layer='GRU'
)
forecaster = ForecasterRnn(
estimator=model,
levels=['target'],
lags=48,
transformer_series=MinMaxScaler(),
fit_kwargs={
'epochs': 50, # Maximum, early stopping decides when to stop
'batch_size': 512,
'callbacks': [
EarlyStopping(monitor='val_loss', patience=3, restore_best_weights=True)
],
'series_val': series_val, # Validation series (exog_val too if exog is used)
},
)
forecaster.fit(series=series_train)
forecaster.plot_history() # Training vs validation loss per epoch
best_epochs = int(np.argmin(forecaster.history_['val_loss'])) + 1
ForecasterRnn stores a copy of the model it receives, and fit() does not
reset the weights: calling it again resumes training from the learned weights.
To train from scratch, create a new ForecasterRnn from the original model,
which is still untrained.
For the final evaluation, add the validation partition to the training set. Early stopping is no longer possible, so train a new network for the number of epochs identified before.
from skforecast.model_selection import backtesting_forecaster_multiseries, TimeSeriesFold
cv = TimeSeriesFold(
steps=forecaster.max_step,
initial_train_size=len(series) - n_test, # Train + validation
refit=False, # Retraining RNNs is expensive; set True only if needed
)
keras.utils.set_random_seed(123)
forecaster_bt = ForecasterRnn(
estimator=model, # Untrained: the first forecaster trained its own copy
levels=['target'],
lags=48,
transformer_series=MinMaxScaler(),
fit_kwargs={'epochs': best_epochs, 'batch_size': 512, 'verbose': 0},
)
metric, predictions = backtesting_forecaster_multiseries(
forecaster=forecaster_bt,
series=series,
cv=cv,
metric='mean_absolute_error',
)
ForecasterRnn supports conformal prediction only. In-sample residuals
(store_in_sample_residuals=True in fit(), or set_in_sample_residuals())
come from data the network was trained on, so they are too small and the
intervals are usually too narrow. Prefer out-of-sample residuals computed on a
calibration set that was not used to fit the network (for example, the
validation partition).
# Predictions on the calibration set with the trained forecaster
# initial_train_size=None: backtesting uses the forecaster as it is, no refit
cv_cal = TimeSeriesFold(steps=forecaster.max_step, initial_train_size=None, refit=False)
_, pred_cal = backtesting_forecaster_multiseries(
forecaster=forecaster, series=series_val, cv=cv_cal, metric='mean_absolute_error'
)
pred_target = pred_cal.loc[pred_cal['level'] == 'target']
# y_true and y_pred in the original scale, the forecaster applies the transformation
forecaster.set_out_sample_residuals(
y_true={'target': pred_target['y_true']},
y_pred={'target': pred_target['pred']},
)
predictions = forecaster.predict_interval(
steps=24,
last_window=series_val, # Forecast right after the calibration period
method='conformal', # Only 'conformal' supported
interval=[0.1, 0.9],
use_in_sample_residuals=False,
use_binned_residuals=True, # Better calibration with binned residuals
)
Conformal intervals add a margin to the point prediction, so the lower bound can be negative for non-negative series; clip it at zero if needed.
transformer_series=MinMaxScaler().EarlyStopping with series_val in fit_kwargs decide when to stop.lags parameter in ForecasterRnn and create_and_compile_model must match.refit=False or refit=5.predict_interval(method='bootstrapping'): ForecasterRnn only supports method='conformal'.exog in create_and_compile_model: If you use exog in fit()/predict(), you must also pass exog when building the model so the architecture accounts for the extra input features.fit() again to retrain from scratch: fit() resumes from the learned weights, and backtesting a trained forecaster continues its training. Create a new ForecasterRnn from the untrained model instead.set_out_sample_residuals() with predictions on a calibration set.keras.utils.set_random_seed() before creating each model.See references/architecture-options.md for
the complete create_and_compile_model signature, recurrent layer types,
output shape rules, exog architecture, custom Keras model requirements,
and fit_kwargs options.
What SKILL.md sets; muted values are Claude Code's defaults
/skforecast:deep-learning-forecastingfrom skforecast
npx claudepluginhub skforecast/skforecast --plugin skforecast17 skills in this plugin
/autocorrelation-and-lag-selectionAnalyzes time series dynamics with the fast skforecast.stats functions acf, pacf and calculate_lag_autocorrelation. Covers reading ACF/PACF patterns to identify AR/MA orders and seasonality, ranking lags by partial autocorrelation, and feeding the result to the lags argument of any skforecast forecaster. Use when the user wants to understand the dynamics of a series, choose a candidate set of lags before hyperparameter tuning, or replace a slow statsmodels acf/pacf call.
/backtesting-configurationConfigures TimeSeriesFold parameters for backtesting based on deployment scenarios. Maps business requirements (retraining frequency, forecast horizon, data budget) to cross-validation strategy parameters. Use when the user describes how they plan to deploy or evaluate a model.
/baseline-forecastingBuilds and evaluates simple baseline forecasts with ForecasterEquivalentDate (equivalent-date / seasonal-naive / moving-average) to benchmark machine learning models. Covers offset (int vs pandas DateOffset), n_offsets with agg_func, backtesting a baseline, comparing against ML with MASE, conformal intervals, and grid_search_equivalent_date. Use when the user wants a baseline, a naive or seasonal reference, or to check whether a model beats a simple rule. Not for choosing among ML forecasters (see choosing-a-forecaster) or tuning ML hyperparameters (see hyperparameter-optimization).
/choosing-a-forecasterGuides selection of the appropriate skforecast forecaster based on the user's data characteristics and requirements. Provides a decision matrix mapping use cases to forecaster classes. Use when the user is unsure which forecaster to use or asks for a recommendation.
/complete-api-referenceProvides complete constructor and method signatures for all skforecast forecasters, backtesting functions, search functions, cross-validation classes, preprocessing, feature selection, and drift detection. Use when the user needs exact parameter names, types, or defaults for any skforecast class or function.
/drift-detectionDetects data drift in time series forecasting pipelines using RangeDriftDetector and PopulationDriftDetector. Covers range-based out-of-range detection and statistical distribution tests. Use when the user wants to monitor model reliability in production.
Other plugins' skills for the same job