Module flowcon.utils.torchutils

Various PyTorch utility functions.

Functions

def batch_JTJ_logabsdet(inputs, outputs)
def batch_jacobian(g, x)
def batch_trace(M)
def batchwise_dot_prod(bvector1, bvector2)
def cbrt(x)

Cube root. Equivalent to torch.pow(x, 1/3), but numerically stable.

def create_alternating_binary_mask(features, even=True)

Creates a binary mask of a given dimension which alternates its masking.

:param features: Dimension of mask. :param even: If True, even values are assigned 1s, odd 0s. If False, vice versa. :return: Alternating binary mask of type torch.Tensor.

def create_mid_split_binary_mask(features)

Creates a binary mask of a given dimension which splits its masking at the midpoint.

:param features: Dimension of mask. :return: Binary mask split at midpoint of type torch.Tensor

def create_random_binary_mask(features)

Creates a random binary mask of a given dimension with half of its entries randomly set to 1s.

:param features: Dimension of mask. :return: Binary mask with half of its entries set to 1s, of type torch.Tensor.

def gaussian_kde_log_eval(samples, query)
def get_num_parameters(model)

Returns the number of trainable parameters in a model of type nets.Module :param model: nets.Module containing trainable parameters :return: number of trainable parameters in model

def get_temperature(max_value, bound=0.999)

For a dataset with max value 'max_value', returns the temperature such that

sigmoid(temperature * max_value) = bound.

If temperature is greater than 1, returns 1.

:param max_value: :param bound: :return:

def gradient(y, x, grad_outputs=None)
def logabsdet(x)

Returns the log absolute determinant of square matrix x.

def merge_leading_dims(x, num_dims)

Reshapes the tensor x such that the first num_dims dimensions are merged to one.

def np_to_tensor(input: Union[numpy.__array_like._SupportsArray[numpy.dtype], numpy.__nested_sequence._NestedSequence[numpy.__array_like._SupportsArray[numpy.dtype]], bool, int, float, complex, str, bytes, numpy.__nested_sequence._NestedSequence[Union[bool, int, float, complex, str, bytes]]], dtype=None, device='cpu') ‑> torch.Tensor
def random_orthogonal(size)

Returns a random orthogonal matrix as a 2-dim tensor of shape [size, size].

def repeat_rows(x, num_reps)

Each row of tensor x is repeated num_reps times along leading dimension.

def safe_detach(tensor)
def sample_rademacher_like(y)
def searchsorted(bin_locations, inputs, eps=1e-06)
def sech2(x)
def set_seeds(SEED)
def split_leading_dim(x, shape)

Reshapes the leading dim of x to have the given shape.

def sum_except_batch(x, num_batch_dims=1)

Sums all elements of x except for the first num_batch_dims dimensions.

def tensor2numpy(x)
def tensor_to_np(tensor: torch.Tensor) ‑> Union[numpy.__array_like._SupportsArray[numpy.dtype], numpy.__nested_sequence._NestedSequence[numpy.__array_like._SupportsArray[numpy.dtype]], bool, int, float, complex, str, bytes, numpy.__nested_sequence._NestedSequence[Union[bool, int, float, complex, str, bytes]]]
def tile(x, n)