Utils

Loggers

class speeq.utils.loggers.TBLogger(log_dir: Union[str, Path], n_logs: int, clear_screen: bool, *args, **kwargs)[source]

Bases: ILogger

log(history: dict)[source]
log_step(key: str, category: str, value: Union[int, float]) None[source]
speeq.utils.loggers.get_logger(name: str, log_dir: Union[str, Path], n_logs: int, *args, **kwargs) ILogger[source]

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.clear()[source]
speeq.utils.utils.get_key_tag(key: str, category: str) str[source]
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_pad_mask(seq_len: int, pad_len: int)[source]
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.get_text_list(data: List[dict], key='text') List[str][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.load_csv(file_path, encoding='utf-8', sep=',')[source]
speeq.utils.utils.load_json(file_path, encoding='utf-8')[source]
speeq.utils.utils.load_state_dict(state_path: Union[str, Path]) tuple[source]
speeq.utils.utils.load_text(file_path, encoding='utf-8')[source]
speeq.utils.utils.save_json(file_path, data: Union[dict, list], encoding='utf-8') None[source]
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.save_text(file_path, data: str, encoding='utf-8') 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]