LSTM

class hana_ml.algorithms.pal.tsa.lstm.LSTM(learning_rate=None, gru=None, batch_size=None, time_dim=None, hidden_dim=None, num_layers=None, max_iter=None, interval=None, optimizer_type=None, stateful=None, bidirectional=None)

Long short-term memory (LSTM) is one of the most famous modules of Recurrent Neural Networks(RNN). It can not only process single data point, but also the entire sequences of data, such as speech and stock prices. This function in PAL is used for time series prediction. It is first given a time series table, by which it will be trained. Then, it is enabled to predict the next value of the time series after a few time-steps as the user specifies. In PAL, both LSTM and its widely used variant Gated Recurrent Units (GRU) are implemented.

Parameters:
learning_ratefloat, optional

The learning rate for gradient descent.

Defaults to 0.01.

gru{'gru', 'lstm'}, optional

Choose GRU or LSTM.

Defaults to 'lstm'.

batch_sizeint, optional

The number of pieces of data for training in one iteration.

Defaults to 32.

time_dimint, optional

It specifies how many time steps in a sequence that will be trained by LSTM/GRU and then for time series prediction.

The value of it must be smaller than the length of input time series minus 1.

Defaults to 16.

hidden_dimint, optional

The number of hidden neuron in LSTM/GRU unit.

Defaults to 128.

num_layersint, optional

The number of layers in LSTM/GRU unit.

Defaults to 1.

max_iterint, optional

The number of batches of data by which LSTM/GRU is trained.

Defaults to 1000.

intervalint, optional

Outputs the average loss within every INTERVAL iterations.

Defaults to 100.

optimizer_type{'SGD', 'RMSprop', 'Adam', 'Adagrad'}, optional

Chooses the optimizer.

Defaults to 'Adam'.

statefulbool, optional

If the value is True, it enables stateful LSTM/GRU.

Defaults to True.

bidirectionalbool, optional

If the value is True, it uses BiLSTM/BiGRU. Otherwise, it uses LSTM/GRU.

Defaults to False.

Examples

Input DataFrame df:

>>> df.head(3).collect()
   TIMESTAMP  SERIES
0          0    20.7
1          1    17.9
2          2    18.8

Create a LSTM model:

>>> lstm = LSTM(gru='lstm',
                bidirectional=False,
                time_dim=16,
                max_iter=1000,
                learning_rate=0.01,
                batch_size=32,
                hidden_dim=128,
                num_layers=1,
                interval=1,
                stateful=False,
                optimizer_type='Adam')

Perform fit():

>>> lstm.fit(data=df)

Perform predict():

>>> res = lstm.predict(data=df_predict)

Output:

>>> res.head(3).collect()
   ID      VALUE                                        REASON_CODE
0   0  11.673560  [{"attr":"T=0","pct":28.926935203430372,"val":...
1   1  14.057195  [{"attr":"T=3","pct":24.729787064691735,"val":...
2   2  15.119411  [{"attr":"T=2","pct":41.616207151605458,"val":...
Attributes:
loss_DateFrame

Loss.

model_DataFrame

Model content.

Methods

fit(data[, key, endog, exog])

Fit the model to the training dataset.

get_model_metrics()

Get the model metrics.

get_score_metrics()

Get the score metrics.

predict(data[, top_k_attributions])

Generates time series forecasts based on the fitted model.

fit(data, key=None, endog=None, exog=None)

Fit the model to the training dataset.

Parameters:
dataDataFrame

Input data, structured as follows.

  • The 1st column : index/timestamp, type INTEGER.

  • The 2nd column : time-series value, type INTEGER, DOUBLE, or DECIMAL(p,s).

  • Other columns : external data(regressors), type INTEGER, DOUBLE, DECIMAL(p,s), VARCHAR or NVARCHAR.

keystr, optional

The timestamp column of data. The type of key column is INTEGER.

Defaults to the first column of data if the index column of data is not provided. Otherwise, defaults to the index column of data.

endogstr, optional

The endogenous variable, i.e. time series. The type of endog column is INTEGER, DOUBLE, or DECIMAL(p, s).

Defaults to the first non-key column of data if not provided.

exogstr or a list of str, optional

An optional array of exogenous variables. The type of exog column is INTEGER, DOUBLE, or DECIMAL(p, s).

Defaults to None. Please set this parameter explicitly if you have exogenous variables.

Returns:
A fitted object of class "LSTM".
predict(data, top_k_attributions=None)

Generates time series forecasts based on the fitted model.

Parameters:
dataDataFrame

Data for prediction. Every row in the data should contain one piece of record data for prediction, i.e. it should be structured as follows:

  • First column: Record ID, type INTEGER.

  • Other columns : Time-series and external data values, arranged in time order.

The number of all columns but the first id column should be equal to the value of time_dim * (M-1), where M is the number of columns of the input data in the training phase.

top_k_attributionsint, optional

Specifies the number of features with highest attributions to output.

Defaults to 10 or 0 depending on the SAP HANA version.

Returns:
DataFrame

The aggregated forecasted values. Forecasted values, structured as follows:

  • ID, type INTEGER, timestamp.

  • VALUE, type DOUBLE, forecast value.

  • REASON_CODE, type NCLOB, Sorted SHAP values for test data at each time step.

get_model_metrics()

Get the model metrics.

Returns:
DataFrame

The model metrics.

get_score_metrics()

Get the score metrics.

Returns:
DataFrame

The score metrics.

Inherited Methods from PALBase

Besides those methods mentioned above, the LSTM class also inherits methods from PALBase class, please refer to PAL Base for more details.