src.core.rnn package#

Submodules#

src.core.rnn.dataset module#

class src.core.rnn.dataset.RNNDataset(config_dict)[source]#

Bases: Word2VecDataset

RNN Dataset

Parameters:

config_dict (dict) – Config Params Dictionary

get_data()[source]#

Generates tokens and labels from extracted data

Returns:

Input tokens, Labels

Return type:

tuple (numpy.ndarray [num_samples, seq_len], numpy.ndarray [num_samples, num_classes])

get_test_data()[source]#

Generates test tokens and labels from extracted data

Returns:

Input tokens, Labels

Return type:

tuple (numpy.ndarray [num_samples, seq_len], numpy.ndarray [num_samples, num_classes])

pad_slice_text(text_ls)[source]#

Pads and slices text to seq_len tokens

Parameters:

text_ls (list) – List of text samples

Returns:

list of preprocessed text

Return type:

list

src.core.rnn.dataset.create_dataloader(X, y, val_split=0.2, batch_size=32, seed=2024)[source]#

Creates Train, Validation DataLoader

Parameters:
  • X (torch.Tensor (num_samples, seq_len)) – Input tokens

  • y (torch.Tensor (num_samples, num_classes)) – Output Labels

  • val_split (float, optional) – validation split, defaults to 0.2

  • batch_size (int, optional) – Batch size, defaults to 32

  • seed (int, optional) – Seed, defaults to 2024

Returns:

Train, Val dataloaders

Return type:

tuple (torch.utils.data.DataLoader, torch.utils.data.DataLoader)

src.core.rnn.model module#

class src.core.rnn.model.RNNCell(h_dim, inp_x_dim, out_x_dim)[source]#

Bases: Module

RNN Cell

Parameters:
  • h_dim (int) – Hidden state vector dimension

  • inp_x_dim (int) – Input vector dimension

  • out_x_dim (int) – Output vector dimension

forward(ht_1, xt)[source]#

Forward propogation

Parameters:
  • ht_1 (torch.Tensor (batch_size, h_dim)) – Hidden state vector

  • xt (torch.Tensor (batch_size, embed_dim)) – Input vector

Returns:

New hidden states, output

Return type:

tuple (torch.Tensor [batch_size, h_dim], torch.Tensor [batch_size, out_dim])

class src.core.rnn.model.RNNModel(config_dict)[source]#

Bases: Module

RNN Architecture

Parameters:

config_dict (dict) – Config Params Dictionary

forward(X)[source]#

Forward propogation

Parameters:

X (torch.Tensor (batch_size, seq_len)) – Input tokens

Returns:

Prediction labels

Return type:

torch.Tensor (batch_size, num_classes)

init_hidden()[source]#

Initialized hidden states

Returns:

List of hidden states

Return type:

list

class src.core.rnn.model.RNNTrainer(model, optimizer, config_dict)[source]#

Bases: Module

RNN Trainer

Parameters:
  • model (torch.nn.Module) – RNN model

  • optimizer (torch.optim) – Optimizer

  • config_dict (dict) – Config Params Dictionary

fit(train_loader, val_loader)[source]#

Fits the model on dataset. Runs training and Validation steps for given epochs and saves best model based on the evaluation metric

Parameters:
  • train_loader (torch.utils.data.DataLoader) – Train Data loader

  • val_loader (torch.utils.data.DataLoader) – Validaion Data Loader

Returns:

Training History

Return type:

dict

predict(data_loader)[source]#

Runs inference to predict a translation of soruce sentence

Parameters:

data_loader (torch.utils.data.DataLoader) – Infer Data loader

Returns:

Predicted tokens

Return type:

numpy.ndarray (num_samples, num_classes)

train_one_epoch(data_loader, epoch)[source]#

Train step

Parameters:
  • data_loader (torch.utils.data.Dataloader) – Train Data Loader

  • epoch (int) – Epoch number

Returns:

Train Losse, Train Metrics

Return type:

tuple (torch.float32, dict)

val_one_epoch(data_loader)[source]#

Validation step

Parameters:

data_loader (torch.utils.data.Dataloader) – Validation Data Loader

Returns:

Validation Losse, Validation Metrics

Return type:

tuple (torch.float32, dict)

src.core.rnn.rnn module#

class src.core.rnn.rnn.RNN(config_dict)[source]#

Bases: object

A class to run RNN data preprocessing, training and inference

Parameters:

config_dict (dict) – Config Params Dictionary

run()[source]#

Runs Seq2Seq Training and saves output

run_infer()[source]#

Runs inference

Returns:

True and predicted captions

Return type:

tuple (torch.Tensor [batch_size, seq_len, num_classes], torch.Tensor [batch_size, seq_len, num_classes])

save_output()[source]#

Saves Training and Inference results

Module contents#