Methods

class BaseModel(name)[source]

Bases: object

Abstract class representing a generic GP

Class initialization.
Args

name (string): the name attribute of the method.

TODO: add more generic args?

abstract fit(X, y, **kwargs)[source]

Fit hyperparameters using Maximum Likelihood Estimation

Args:

X (2d torch.tensor): training data in 2d array form (nsamples,nfeatures) y (1d torch.tensor): training data in list form, (nsameples) for each response

abstract fit_transform(X, **kwargs)[source]
Args:

X (2d torch.tensor): training data in 2d array form (nsamples,nfeatures)

abstract predict(X, **kwargs)[source]
Args:

X (2d torch.tensor): training data in 2d array form (nsamples,nfeatures)

ARIMA

arima{} is parameterized by $p$, the number of autoregressive terms, $d$,
the number of differences needed for stationarity and $q$,

the number of lagged forecast errors in the forecasting equation: $hat{x_t} = alpha + beta_1 x_{t-1} +…+ beta_p x_{t-p} - beta_1 e_{t-1} - beta_q e_{t-q}$ where $beta$ represent autoregressive terms, $theta$ represent

moving average terms, and $alpha$ is a bias/intercept term. Potential uses include autoregression, imputation, and fault detection.

param str name

generic name to save and load this model.

param int p

number of autoregressive terms $x_{t-1} + … + x_{t-p}$

param int d

differencing factor between autoregressive terms and moving average terms.

param int q

number of moving average terms. $q$ in the equation above.

base implementation from statsmodels.tsa.arima.model.arima

KNN

class KNN(name, n_neighbors=10)[source]

Bases: BaseModel

fit(X, **kwargs)[source]

Fit hyperparameters using Maximum Likelihood Estimation

Args:

X (2d torch.tensor): training data in 2d array form (nsamples,nfeatures) y (1d torch.tensor): training data in list form, (nsameples) for each response

fit_transform(X, **kwargs)[source]
Args:

X (2d torch.tensor): training data in 2d array form (nsamples,nfeatures)

predict(X, **kwargs)[source]
Args:

X (2d torch.tensor): training data in 2d array form (nsamples,nfeatures)

LSTM

class LSTM(name, batch_size=10, train_iters=10, hidden_size=64, layer_dim=1, learning_rate=0.2)[source]

Bases: BaseModel

fit(X, **kwargs)[source]

Fit hyperparameters using Maximum Likelihood Estimation

Args:

X (2d torch.tensor): training data in 2d array form (nsamples,nfeatures) y (1d torch.tensor): training data in list form, (nsameples) for each response

fit_transform(X, **kwargs)[source]
Args:

X (2d torch.tensor): training data in 2d array form (nsamples,nfeatures)

predict(X, **kwargs)[source]
Args:

X (2d torch.tensor): training data in 2d array form (nsamples,nfeatures)

class LSTM_Model(input_dim, batch_size, seq_len, layer_dim, output_dim, hidden_dim, dropout=0.1)[source]

Bases: Module

https://towardsdatascience.com/building-rnn-lstm-and-gru-for-time-series-using-pytorch-a46e5b094e7b

forward(x)[source]

Defines the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

RNN

class RNN(input_size, batch_size, seq_len, output_size, hidden_dim, n_layers)[source]

Bases: Module

https://towardsdatascience.com/building-rnn-lstm-and-gru-for-time-series-using-pytorch-a46e5b094e7b

forward(x)[source]

Defines the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

class VanillaRNN(name, train_iters=10, num_layers=3, hidden_size=1, batch_size=5, learning_rate=0.01)[source]

Bases: BaseModel

fit(X, **kwargs)[source]

Fit hyperparameters using Maximum Likelihood Estimation

Args:

X (2d torch.tensor): training data in 2d array form (nsamples,nfeatures) y (1d torch.tensor): training data in list form, (nsameples) for each response

fit_transform(X, **kwargs)[source]
Args:

X (2d torch.tensor): training data in 2d array form (nsamples,nfeatures)

predict(X, **kwargs)[source]
Args:

X (2d torch.tensor): training data in 2d array form (nsamples,nfeatures)

VAE

class Decoder(sequence_length, batch_size, hidden_size, hidden_layer_depth, latent_length, output_size, number_of_features, dtype, block='LSTM')[source]

Bases: Module

Converts latent vector into output :param sequence_length: length of the input sequence :param batch_size: batch size of the input sequence :param hidden_size: hidden size of the RNN :param hidden_layer_depth: number of layers in RNN :param latent_length: latent vector length :param output_size: 2, one representing the mean, other log std dev of the output :param block: GRU/LSTM - use the same which you’ve used in the encoder :param dtype: Depending on cuda enabled/disabled, create the tensor

forward(latent)[source]

Converts latent to hidden to output :param latent: latent vector :return: outputs consisting of mean and std dev of vector

class Encoder(number_of_features, hidden_size, hidden_layer_depth, latent_length, dropout, block='LSTM')[source]

Bases: Module

Encoder network containing enrolled LSTM/GRU :param number_of_features: number of input features :param hidden_size: hidden size of the RNN :param hidden_layer_depth: number of layers in RNN :param latent_length: latent vector length :param dropout: percentage of nodes to dropout :param block: LSTM/GRU block

forward(x)[source]

Forward propagation of encoder. Given input, outputs the last hidden state of encoder :param x: input to the encoder, of shape (sequence_length, batch_size, number_of_features) :return: last hidden state of encoder, of shape (batch_size, hidden_size)

class Lambda(hidden_size, latent_length)[source]

Bases: Module

Lambda module converts output of encoder to latent vector :param hidden_size: hidden size of the encoder :param latent_length: latent vector length

forward(cell_output)[source]

Given last hidden state of encoder, passes through a linear layer, and finds the mean and variance :param cell_output: last hidden state of encoder :return: latent vector

class VRAE(name, batch_size=1, hidden_size=90, hidden_layer_depth=1, latent_length=20, learning_rate=0.005, block='LSTM', train_iters=5, dropout_rate=0.0, optimizer_name='Adam', loss_fn='MSELoss', cuda=False, print_every=100, clip=True, max_grad_norm=5, dload='.')[source]

Bases: Module, BaseModel

Variational recurrent auto-encoder. This module is used for dimensionality reduction of timeseries :param sequence_length: length of the input sequence :param number_of_features: number of input features :param hidden_size: hidden size of the RNN :param hidden_layer_depth: number of layers in RNN :param latent_length: latent vector length :param batch_size: number of timeseries in a single batch :param learning_rate: the learning rate of the module :param block: GRU/LSTM to be used as a basic building block :param n_epochs: Number of iterations/epochs :param dropout_rate: The probability of a node being dropped-out :param optimizer: ADAM/ SGD optimizer to reduce the loss function :param loss: SmoothL1Loss / MSELoss / ReconLoss / any custom loss which inherits from _Loss class :param boolean cuda: to be run on GPU or not :param print_every: The number of iterations after which loss should be printed :param boolean clip: Gradient clipping to overcome explosion :param max_grad_norm: The grad-norm to be clipped :param dload: Download directory where models are to be dumped

compute_loss(X, X_true)[source]

Given input tensor, forward propagate, compute the loss, and backward propagate. Represents the lifecycle of a single iteration :param X: Input tensor :return: total loss, reconstruction loss, kl-divergence loss and original input

fit(X, save=False, **kwargs)[source]

Calls _train function over a fixed number of epochs, specified by n_epochs :param dataset: X np.array :param bool save: If true, dumps the trained model parameters as pickle file at dload directory :return:

fit_transform(dataset, save=False, **kwargs)[source]

Combines the fit and transform functions above :param dataset: Dataset on which fit and transform have to be performed :param bool save: If true, dumps the model and latent vectors as pickle file :return: latent vectors for input dataset

forward(x)[source]

Forward propagation which involves one pass from inputs to encoder to lambda to decoder :param x:input tensor :return: the decoded output, latent vector

load(PATH)[source]

Loads the model’s parameters from the path mentioned :param PATH: Should contain pickle file :return: None

predict(X, save=False, **kwargs)[source]

Given input dataset, creates dataloader, runs dataloader on _batch_transform Prerequisite is that model has to be fit :param dataset: input dataset who’s latent vectors are to be obtained :param bool save: If true, dumps the latent vector dataframe as a pickle file :return:

reconstruct(X, save=False)[source]

Given input dataset, creates dataloader, runs dataloader on _batch_reconstruct Prerequisite is that model has to be fit :param dataset: input dataset who’s output vectors are to be obtained :param bool save: If true, dumps the output vector dataframe as a pickle file :return:

save(file_name)[source]

Pickles the model parameters to be retrieved later :param file_name: the filename to be saved as,`dload` serves as the download directory :return: None