This function creates and trains a Transformer-based model for time series
forecasting using the Keras library. It allows customization of key architectural
parameters such as sequence size, attention head size, number of attention heads,
feed-forward network dimensions, number of Transformer blocks, and MLP
(multi-layer perceptron) configurations including units and dropout rates.
Before running this function, we advise the users to install Python in your system and create the virtual conda environment.
Installation of the modules such as 'tensorflow', 'keras' and 'pandas' are necessary for this package. If the user
does not know about these steps, they can use the install_r_dependencies() function which is available in this package.
The function begins by generating training sequences from the input data (df)
based on the specified sequence_size. Sliding windows of input sequences are
created as x, while the subsequent values in the series are used as targets (y).
The model architecture includes an input layer, followed by one or more Transformer
encoder blocks, a global average pooling layer for feature aggregation, and MLP
layers for further processing. The final output layer is designed for the forecasting task.
The model is compiled using the Adam optimizer and the mean squared error (MSE)
loss function. Training is performed with the specified number of epochs,
batch_size, and early stopping configured through the patience parameter.
During training, 20% of the data is used for validation, and the best model weights
are restored when validation performance stops improving.
The package requires a dataset with two columns: Date (formatted as dates) and the Close price (numerical).
After loading the data and formatting it appropriately, the TRANSFORMER function
trains a Transformer-based model to predict future closing prices. It outputs
essential performance metrics like RMSE, MAPE, and sMAPE, along with visualizations
such as training loss trends and an actual vs. predicted plot. These features make
it an invaluable tool for understanding and forecasting stock market trends effectively..