trajdl.utils package#

trajdl.utils.dist2weight(dist: ndarray, tokenizer: T2VECTokenizer, dist_decay_speed: float = 0.008) Tensor[source]#

dist: (vocab_size, k)

trajdl.utils.find_best_checkpoint(dir_path: str, is_maximizing: bool = True) str[source]#

Find the best checkpoint file based on validation loss.

Parameters:
  • dir_path (str) – The path to the directory containing checkpoint files.

  • is_maximizing (bool, optional) – Flag indicating whether to maximize or minimize validation loss. If True, the function seeks the checkpoint with the highest validation loss. If False, it seeks the checkpoint with the lowest validation loss. Default is True.

Returns:

The filename of the best checkpoint based on the specified criteria.

Return type:

str

Raises:

ValueError – If no valid checkpoint files are found in the specified directory.

Examples

>>> best_checkpoint_name = find_best_checkpoint("path_to_your_directory", is_maximizing=False)
>>> print(best_checkpoint_name)
'model-epoch=004-val_loss=2.498780.ckpt'
trajdl.utils.get_num_cpus() int[source]#
trajdl.utils.load_bucketizer(bucketizer: str | Bucketizer) Bucketizer[source]#
trajdl.utils.load_tokenizer(tokenizer: str | Path | AbstractTokenizer) AbstractTokenizer[source]#

load a tokenizer from a path of instance

trajdl.utils.tiny_value_of_dtype(dtype: dtype) float[source]#

Returns a moderately tiny value for a given PyTorch data type that is used to avoid numerical issues such as division by zero. This is different from info_value_of_dtype(dtype).tiny because it causes some NaN bugs. Only supports floating point dtypes.

trajdl.utils.try_gpu(i=0)[source]#

Return gpu(i) if available, else return cpu()

trajdl.utils.valid_lengths_to_mask(valid_lengths: List[int]) Tensor[source]#

Generate a mask tensor based on the valid lengths of sequences.

Parameters:

valid_lengths (List[int]) – A list of integers representing the valid lengths of each sequence in the batch.

Returns:

A binary mask tensor of shape (N, max_length), where N is the number of sequences and max_length is the maximum valid length found in the input list. Each row corresponds to a sequence with 1s up to its valid length and 0s beyond that.

Return type:

torch.Tensor

Example

>>> valid_lengths_to_mask([3, 2, 5])
tensor([[1., 1., 1., 0., 0.],
        [1., 1., 0., 0., 0.],
        [1., 1., 1., 1., 1.]])

1.  Submodules#