src.core.gru package#
Submodules#
src.core.gru.dataset module#
- src.core.gru.dataset.create_dataloader(X, y=None, val_split=0.2, batch_size=32, seed=2024, data='train')[source]#
Creates Train, Validation and Test DataLoader
- Parameters:
X (torch.Tensor (num_samples, seq_len)) – Input source tokens
y (torch.Tensor (num_samples, seq_len), optional) – Input target tokens, defaults to None
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
data (str, optional) – Type of data, defaults to “train”
- Returns:
Train, Val / Test dataloaders
- Return type:
tuple (torch.utils.data.DataLoader, torch.utils.data.DataLoader) / torch.utils.data.DataLoader
src.core.gru.gru module#
- class src.core.gru.gru.GRU(config_dict)[source]#
Bases:
object
A class to run GRU data preprocessing, training and inference
- Parameters:
config_dict (dict) – Config Params Dictionary
src.core.gru.model module#
- class src.core.gru.model.GRUCell(h_dim, inp_x_dim, out_x_dim)[source]#
Bases:
Module
GRU 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.gru.model.GRUModel(config_dict)[source]#
Bases:
Module
GRU Architecture
- Parameters:
config_dict (dict) – Config Params Dictionary
- forward(X)[source]#
Forward Propogation
- Parameters:
X (torch.Tensor (batch_size, seq_len)) – Input tokens
- Returns:
Predicted labels
- Return type:
toch.Tensor (batch_size, seq_len, num_classes)
Initialized hidden state
- Returns:
Hidden state
- Return type:
torch.Tensor (num_samples, h_dim)
- class src.core.gru.model.GRUTrainer(model, optimizer, config_dict)[source]#
Bases:
Module
GRU Trainer
- Parameters:
model (torch.nn.Module) – GRU model
optimizer (torch.optim) – Optimizer
config_dict (dict) – Config Params Dictionary
- calc_loss(y_pred, y_true)[source]#
Crossentropy loss for predicted tokens
- Parameters:
y_pred (torch.Tensor (batch_size, seq_len, num_vocab)) – Predicted tokens
y_true (torch.Tensor (batch_size, seq_len)) – True tokens
- Returns:
BCE Loss
- Return type:
torch.float32
- 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 labels
- Return type:
numpy.ndarray (num_samples, seq_len)