Skip to main content


Performs Transformer forecasting

Source Code: [link]

openbb.forecast.trans(data: Union[pd.Series, pd.DataFrame], target_column: str = "close", n_predict: int = 5, train_split: float = 0.85, past_covariates: Optional[str] = None, forecast_horizon: int = 5, input_chunk_length: int = 14, output_chunk_length: int = 5, d_model: int = 64, nhead: int = 4, num_encoder_layers: int = 3, num_decoder_layers: int = 3, dim_feedforward: int = 512, activation: str = "relu", dropout: float = 0.0, batch_size: int = 32, n_epochs: int = 300, learning_rate: float = 0.001, model_save_name: str = "trans_model", force_reset: bool = True, save_checkpoints: bool = True, metric: str = "mape")


dataUnion[pd.Series, pd.DataFrame]Input DataNoneFalse
n_predictintDays to predict. Defaults to 5.5True
target_columnstrTarget column to forecast. Defaults to "close".closeTrue
train_splitfloatTrain/val split. Defaults to
past_covariatesstrMultiple secondary columns to factor in when forecasting. Defaults to None.NoneTrue
forecast_horizonintForecast horizon when performing historical forecasting. Defaults to 5.5True
input_chunk_lengthintNumber of past time steps that are fed to the forecasting module at prediction time. Defaults to 14.14True
output_chunk_lengthintThe length of the forecast of the model. Defaults to 5.5True
d_modelintThe number of expected features in the encoder/decoder inputs. Defaults to 64.64True
nheadintThe number of heads in the multi-head attention mechanism. Defaults to 4.4True
num_encoder_layersintThe number of encoder layers in the encoder. Defaults to 3.3True
num_decoder_layersintThe number of decoder layers in the encoder. Defaults to 3.3True
dim_feedforwardintThe dimension of the feedforward network model. Defaults to 512.512True
activationstrThe activation function of encoder/decoder intermediate layer, ‘relu’ or ‘gelu’. Defaults to 'relu'.reluTrue
dropoutfloatFraction of neurons affected by Dropout. Defaults to
batch_sizeintNumber of time series (input and output sequences) used in each training pass. Defaults to 32.32True
n_epochsintNumber of epochs over which to train the model. Defaults to 100.300True
learning_ratefloatDefaults to 1e-3.0.001True
model_save_namestrName for model. Defaults to "brnn_model".trans_modelTrue
force_resetboolIf set to True, any previously-existing model with the same name will be reset (all checkpoints will be
discarded). Defaults to True.
save_checkpointsboolWhether or not to automatically save the untrained model and checkpoints from training. Defaults to True.TrueTrue
metricstrMetric to use for model selection. Defaults to "mape".mapeTrue


Tuple[List[TimeSeries], List[TimeSeries], List[TimeSeries], Optional[float], type[TransformerModel]]Adjusted Data series,
Historical forecast by best RNN model,
list of Predictions,
Mean average precision error,
Best transformer Model.