Utils
Loggers
Utils
- speeq.utils.utils.add_pos_enc(x: Tensor) Tensor[source]
Adds positional encodings to the input tensor x.
Args:
x (Tensor): The input tensor of shape [B, M, d].
Returns:
Tensor: The input added to at the positional encoding.
- speeq.utils.utils.calc_data_len(result_len: int, pad_len: Union[Tensor, int], data_len: Union[Tensor, int], kernel_size: int, stride: int) Union[Tensor, int][source]
Calculates the new data portion size after applying convolution on a padded tensor
Args:
result_len (int): The length after the convolution is applied.
pad_len Union[Tensor, int]: The original padding portion length.
data_len Union[Tensor, int]: The original data portion legnth.
kernel_size (int): The convolution kernel size.
stride (int): The convolution stride.
Returns:
Union[Tensor, int]: The new data portion length.
- speeq.utils.utils.get_mask_from_lens(lengths: Tensor, max_len: int) Tensor[source]
Creates a mask tensor from lengths tensor.
- Args:
lengths (Tensor): The lengths of the original tensors of shape [B].
max_len (int): the maximum lengths.
- Returns:
Tensor: The mask of shape [B, max_len] and True whenever the index in the data portion.
- speeq.utils.utils.get_positional_encoding(max_length: int, d_model: int) Tensor[source]
Create positional encoding tensor as described in https://arxiv.org/abs/1706.03762
Args:
max_length (int): The maximum length of the positionals sequence.
d_model (int): The dimensionality of the positionals sequence.
Returns:
Tensor: Positional tensor of shape [1, max_length, d_model]
- speeq.utils.utils.get_state_dict(model: Module, optimizer: Optimizer, step: int, history: dict) dict[source]
- speeq.utils.utils.has_bnorm(model: Module) bool[source]
Checks if a model contains a batch normalization layer.
- Args:
model (Module): The model to check.
- Returns:
bool: A boolean value indicating whether the provided model contains batch normalization or not.
- speeq.utils.utils.save_state_dict(model_name: str, outdir: Union[str, Path], model: Module, optimizer: Optimizer, step: int, history: dict) None[source]
- speeq.utils.utils.set_state_dict(model: Module, state_path: Union[Path, str], optimizer: Optional[Optimizer] = None)[source]
- speeq.utils.utils.truncate_attention_mask(mask: Tensor, right_size: int, left_size: int) Tensor[source]
creates a truncation mask that can be used to mask attention to only look at the time steps with a certain range. Specifically, it allows attention to look at right_size steps to the right and left_size steps to the left of each time step.
Args:
mask (Tensor): The original mask, which is True for the data positions and False for the padding ones. It has a shape of [B, M].
right_size (int): The size of the right window that each time step is allowed to look at.
left_size (int): The size of the left window that each time step is allowed to look at.
- Returns:
Tensor: The new mask tensor of shape [B, M, M]