caveat.models.utils
ScheduledOptim(optimizer, lr_mul, d_model, n_warmup_steps)
#
Bases: _LRScheduler
A simple wrapper class for learning rate scheduling
Source code in caveat/models/utils.py
build_hidden_layers(config)
#
Build hidden layer sizes from config.
PARAMETER | DESCRIPTION |
---|---|
config
|
Configuration dictionary containing hidden layer parameters.
TYPE:
|
RAISES | DESCRIPTION |
---|---|
ValueError
|
If both hidden_layers and hidden_n/hidden_size are specified. |
ValueError
|
If hidden_layers is not a list. |
ValueError
|
If hidden_layers contains non-integer values. |
ValueError
|
If neither hidden_layers nor hidden_n/hidden_size are specified. |
RETURNS | DESCRIPTION |
---|---|
list
|
List of hidden layer sizes.
TYPE:
|
Source code in caveat/models/utils.py
calc_output_padding_1d(length, target, kernel_size, stride, padding, patience=20)
#
Calculate the output padding required for a 1D transposed convolution to achieve a target length. This function iterates over possible padding values and output padding values to find a combination that results in the desired target length after a 1D transposed convolution. Args: length (int): The length of the input. target (int): The desired length of the output. kernel_size (int): The size of the convolution kernel. stride (int): The stride of the convolution. padding (int): The initial padding value. patience (int, optional): The maximum number of iterations to try for padding and output padding. Default is 20. Returns: tuple: A tuple containing the padding and output padding values that achieve the target length. Raises: ValueError: If no combination of padding and output padding can achieve the target length within the given patience.
Source code in caveat/models/utils.py
calc_output_padding_2d(size)
#
Calculate output padding for a transposed convolution such that output dims will match dimensions of inputs to a convolution of given size. For each dimension, padding is set to 1 if even size, otherwise 0.
PARAMETER | DESCRIPTION |
---|---|
size
|
input size (h, w) |
RETURNS | DESCRIPTION |
---|---|
array
|
np.array: required padding |
Source code in caveat/models/utils.py
conv1d_size(length, kernel_size, stride, padding=0)
#
Calculate output dimensions for 1d convolution.
PARAMETER | DESCRIPTION |
---|---|
length
|
Input size.
TYPE:
|
kernel_size
|
Kernel_size.
TYPE:
|
stride
|
Stride.
TYPE:
|
padding
|
Input padding.
TYPE:
|
Returns: int: Output size.
Source code in caveat/models/utils.py
conv2d_size(size, kernel_size=3, stride=2, padding=1, dilation=1)
#
Calculate output dimensions for 2d convolution.
PARAMETER | DESCRIPTION |
---|---|
size
|
Input size, may be integer if symetric. |
kernel_size
|
Kernel_size. Defaults to 3. |
stride
|
Stride. Defaults to 2. |
padding
|
Input padding. Defaults to 1. |
dilation
|
Dilation. Defaults to 1. |
RETURNS | DESCRIPTION |
---|---|
array
|
np.array: Output size. |
Source code in caveat/models/utils.py
duration_mask(mask)
#
hot_argmax(batch, axis=-1)
#
Encoded given axis as one-hot based on argmax for that axis.
PARAMETER | DESCRIPTION |
---|---|
batch
|
Input tensor.
TYPE:
|
axis
|
Axis index to encode. Defaults to -1.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
tensor
|
One hot encoded tensor.
TYPE:
|
Source code in caveat/models/utils.py
mask_after_eos(acts, sos=0, eos=1)
#
Mask all values after the first occurrence of the end of sequence (eos) token. This is useful for ensuring that any predictions or calculations do not consider values that occur after the end of a sequence, which is often required in sequence models.
PARAMETER | DESCRIPTION |
---|---|
acts
|
Input tensor of shape [N, steps].
TYPE:
|
eos
|
End of sequence token index. Defaults to 1.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Tensor
|
Masked tensor with values after the first eos token set to 0.
TYPE:
|
Source code in caveat/models/utils.py
normalise_log_durations(batch, sos=0, eos=1)
#
Normalise the durations in the log_probs tensor to sum to 1, excluding start of sequence (sos) and end of sequence (eos) tokens. SOS and EOS locations are determined by the argmax of the activity logits. This function is useful for ensuring that the durations of activities in a sequence sum to 1, which is often required for models that predict durations as part of their output. but cannot deal with all weird edge cases. Args: input (Tensor): Log probabilities tensor of shape [N, steps, encodings + 1]. batch (int): Start of sequence token index. Defaults to 0. eos (int): End of sequence token index. Defaults to 1. Returns: Tensor: Normalised log probabilities tensor with durations summing to 1.
Source code in caveat/models/utils.py
transconv_size_1d(length, kernel_size, stride, padding, output_padding, dilation=1)
#
transconv_size_2d(size, kernel_size=3, stride=2, padding=1, dilation=1, output_padding=1)
#
Calculate output dimension for 2d transpose convolution.
PARAMETER | DESCRIPTION |
---|---|
size
|
Input size, may be integer if symetric. |
kernel_size
|
Kernel size. Defaults to 3. |
stride
|
Stride. Defaults to 2. |
padding
|
Input padding. Defaults to 1. |
dilation
|
Dilation. Defaults to 1. |
output_padding
|
Output padding. Defaults to 1. |
RETURNS | DESCRIPTION |
---|---|
array
|
np.array: Output size. |