trajdl.algorithms.t2vec module#
- class trajdl.algorithms.t2vec.DecoderWithAttention(hidden_size: int, num_layers: int, embedding_layer: BaseTokenEmbeddingLayer, dropout: float = 0.0)[source]#
Bases:
Module- forward(x: LongTensor, hidden_state: Tensor, all_encoder_hidden_states: Tensor)[source]#
- Parameters:
x (torch.LongTensor, shape is (batch_size, seq_length))
hidden_state ((num_layers, batch_size, hidden_size))
all_encoder_hidden_states ((batch_size, seq_len, hidden_size))
Output
----------
output ((batch_size, seq_len, hidden_size))
- class trajdl.algorithms.t2vec.GlobalAttention(hidden_size: int)[source]#
Bases:
Module$$a = sigma((W_1 q)H)$$ $$c = tanh(W_2 [a H, q])$$
- class trajdl.algorithms.t2vec.StackingGRU(input_size: int, hidden_size: int, num_layers: int, dropout: float = 0.0)[source]#
Bases:
ModuleMulti-layer CRU Cell
- class trajdl.algorithms.t2vec.T2VEC(embedding_dim: int, hidden_size: int, tokenizer: str | AbstractTokenizer, knn_indices_path: str, knn_distances_path: str, num_layers: int = 1, bidirectional_encoder: bool = False, embedding_path: str | None = None, freeze_embedding: bool = False, dropout: float = 0.0)[source]#
Bases:
BaseLightningModel- compute_loss(batch: T2VECSample)[source]#
- Parameters:
batch (T2VECSample)
- Returns:
loss (torch.Tensor) β shape is (1,)
batch_size (int)
- encode(batch: T2VECSample) Tuple[Tensor, Tensor][source]#
- forward(batch: T2VECSample) Tensor[source]#
ζ¨ηηι»θΎ
- Parameters:
batch (T2VECSample)
- Return type:
torch.Tensor, shape is (batch_size, hidden_size)
- training_step(batch: T2VECSample, batch_idx: int) Tensor[source]#
- Parameters:
batch (T2VECSample)
- validation_step(batch: T2VECSample, batch_idx: int) Tensor[source]#
Operates on a single batch of data from the validation set. In this step youβd might generate examples or calculate anything of interest like accuracy.
- Parameters:
batch β The output of your data iterable, normally a
DataLoader.batch_idx β The index of this batch.
dataloader_idx β The index of the dataloader that produced this batch. (only if multiple dataloaders used)
- Returns:
Tensor- The loss tensordict- A dictionary. Can include any keys, but must include the key'loss'.None- Skip to the next batch.
# if you have one val dataloader: def validation_step(self, batch, batch_idx): ... # if you have multiple val dataloaders: def validation_step(self, batch, batch_idx, dataloader_idx=0): ...
Examples:
# CASE 1: A single validation dataset def validation_step(self, batch, batch_idx): x, y = batch # implement your own out = self(x) loss = self.loss(out, y) # log 6 example images # or generated text... or whatever sample_imgs = x[:6] grid = torchvision.utils.make_grid(sample_imgs) self.logger.experiment.add_image('example_images', grid, 0) # calculate acc labels_hat = torch.argmax(out, dim=1) val_acc = torch.sum(y == labels_hat).item() / (len(y) * 1.0) # log the outputs! self.log_dict({'val_loss': loss, 'val_acc': val_acc})
If you pass in multiple val dataloaders,
validation_step()will have an additional argument. We recommend setting the default value of 0 so that you can quickly switch between single and multiple dataloaders.# CASE 2: multiple validation dataloaders def validation_step(self, batch, batch_idx, dataloader_idx=0): # dataloader_idx tells you which dataset this is. ...
Note
If you donβt need to validate you donβt need to implement this method.
Note
When the
validation_step()is called, the model has been put in eval mode and PyTorch gradients have been disabled. At the end of validation, the model goes back to training mode and gradients are enabled.
- class trajdl.algorithms.t2vec.T2VECEncoder(embedding_layer: BaseTokenEmbeddingLayer, padding_value: int, hidden_size: int, num_layers: int, bidirectional: bool = False, dropout: float = 0.0)[source]#
Bases:
Module- forward(src: LongTensor, src_lengths: List[int])[source]#
Define 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
Moduleinstance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.