botiverse.models.LSTM package#

Submodules#

botiverse.models.LSTM.LSTM module#

class botiverse.models.LSTM.LSTM.LSTMCell(input_size, hidden_size)[source]#

Bases: Module

An interface for a single LSTM layer.

Initialize the parameters single LSTM layer given the size of the inputs and the required hidden size.

Parameters:
  • input_size (int) – The size of the input to the LSTM layer

  • hidden_size (int) – The size of the hidden state of the LSTM layer

forward(input, h, c)[source]#

Forward pass of the LSTM layer which passes in the input and previous states and returns the new hidden and cell states

Parameters:
  • input (torch.Tensor) – The input to the LSTM layer which is of shape (batch_size, input_size)

  • h (torch.Tensor) – The previous hidden state of the LSTM layer which is of shape (batch_size, hidden_size)

  • c (torch.Tensor) – The previous cell state of the LSTM layer which is of shape (batch_size, hidden_size)

Returns:

The new hidden state and cell state of the LSTM layer which are of shapes (batch_size, hidden_size) each

Return type:

tuple

training: bool#
class botiverse.models.LSTM.LSTM.LSTMX(input_size, hidden_size, num_layers=1)[source]#

Bases: Module

An interface for a multi-layer LSTM where the hidden state of each layer is of the same size.

Initialize the parameters of an LSTM with an arbitrary number of layers all of which have the same hidden size.

Parameters:
  • input_size (int) – The size of the input to the LSTM layer

  • hidden_size (int) – The size of the hidden state of the LSTM layer

  • num_layers (int) – The number of LSTM layers stacked on top of each other (default: 1)

forward(input, ho=None)[source]#

Forward pass of the LSTM which takes the input and initial hidden/cell state and returns the output due to the last token in the sequence.

Parameters:
  • input (torch.Tensor) – The input to the LSTM layer which is of shape (batch_size, seq_len, input_size)

  • hₒ (torch.Tensor) – The initial hidden and cell states of the LSTM layer which are of shape (num_layers, batch_size, hidden_size)

Returns:

The output due to the last token in the sequence which is of shape (batch_size, hidden_size)

Return type:

torch.Tensor

training: bool#
class botiverse.models.LSTM.LSTM.LSTMClassifier(input_size, hidden_size, num_classes)[source]#

Bases: Module

LSTMClassifier class defines a high-level interface of the LSTM model for classification.

Initialize the LSTMClassifier with the given parameters.

Parameters:
  • input_size (int) – The size of the input to the LSTM layer

  • hidden_size (int) – The size of the hidden state of the LSTM layer

  • num_classes (int) – The number of classes to classify the input into

forward(x)[source]#

Forward pass of the LSTMClassifier which takes the input and passes it through all the LSTM layers and an output layer to produce an output.

Parameters:

x (torch.Tensor) – The input to the LSTMClassifier which is of shape (batch_size, seq_len, input_size)

Returns:

The output of the LSTMClassifier which is of shape (batch_size, num_classes)

Return type:

torch.Tensor

fit(X, y, λ=0.001, α=0.001, max_epochs=100, patience=5, val_ratio=0.2)[source]#

Fit the LSTMClassifier to the given data.

Parameters:
  • X (numpy.ndarray) – The input data of shape (batch_size, seq_len, input_size)

  • y (numpy.ndarray) – The labels of the data of shape (batch_size)

  • hidden_size (int) – The size of the hidden state of the LSTM layer (default: 64)

  • λ (float) – The learning rate (default: 0.001)

  • num_epochs (int) – The number of epochs to train the model for (default: 100)

predict(X)[source]#

Predict the labels of the given data by passing it through the LSTMClassifier.

Parameters:

X (numpy.ndarray) – The input data of shape (batch_size, seq_len, input_size)

Returns:

The predicted labels of the data of shape (batch_size)

Return type:

numpy.ndarray

evaluate(Xt, yt)[source]#

Evaluate the LSTMClassifier on the given data.

Parameters:
  • X (numpy.ndarray) – The input data of shape (batch_size, seq_len, input_size)

  • y (numpy.ndarray) – The labels of the data of shape (batch_size)

Returns:

The accuracy of the LSTMClassifier on the given data

Return type:

float

save(path)[source]#

Save the LSTMClassifier to a file.

Parameters:

path (str) – The path to the file

load(path)[source]#

Load the LSTMClassifier from a file.

Parameters:

path (str) – The path to the file

training: bool#

Module contents#