box_embeddings.common.tf_utils

Module Contents

tiny_value_of_dtype(dtype: tensorflow.dtypes.DType) float

This implementation is adopted from AllenNLP.

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.

Parameters

dtype – torch dtype of supertype float

Returns

Tiny value

Return type

float

Raises

TypeError – Given non-float or unknown type

log1mexp(x: tensorflow.Tensor, split_point: float = _log1mexp_switch, exp_zero_eps: float = 1e-07) tensorflow.Tensor

Computes log(1 - exp(x)).

Splits at x=log(1/2) for x in (-inf, 0] i.e. at -x=log(2) for -x in [0, inf).

= log1p(-exp(x)) when x <= log(1/2) or = log(-expm1(x)) when log(1/2) < x <= 0

For details, see

https://cran.r-project.org/web/packages/Rmpfr/vignettes/log1mexp-note.pdf

https://github.com/visinf/n3net/commit/31968bd49c7d638cef5f5656eb62793c46b41d76

Parameters
  • x – input tensor

  • split_point – Should be kept to the default of log(0.5)

  • exp_zero_eps – Default 1e-7

Returns

Elementwise log1mexp(x) = log(1-exp(x))

Return type

torch.Tensor

log1pexp(x: tensorflow.Tensor) tensorflow.Tensor

Computes log(1+exp(x))

see: Page 7, eqn 10 of https://cran.r-project.org/web/packages/Rmpfr/vignettes/log1mexp-note.pdf also see: https://github.com/SurajGupta/r-source/blob/master/src/nmath/plogis.c

Parameters

x – Tensor

Returns

Elementwise log1pexp(x) = log(1+exp(x))

Return type

torch.Tensor

softplus_inverse(t: tensorflow.Tensor, beta: float = 1.0, threshold: float = 20) tensorflow.Tensor

Computes the inverse softplus

Parameters
  • t – input tensor

  • beta – TODO

  • threshold – TODO

Returns

softplus inverse of the input

lse_eps = 1e-38
log_lse_eps
logsumexp2(t1: tensorflow.Tensor, t2: tensorflow.Tensor) tensorflow.Tensor

Performs element-wise logsumexp of two tensors in a numerically stable manner. This can also be thought as a soft/differentiable version of the max operator.

Specifically, it computes log(exp(t1) + exp(t2)).

Parameters
  • t1 – First tensor (left operand)

  • t2 – Second tensor (right operand)

Returns

logsumexp

inv_sigmoid(t1: tensorflow.Tensor) tensorflow.Tensor

Calculate the inverse of the sigmoid function

Parameters

t1 – a tensor

Returns

inverse sigmoid of the input

tf_index_select(input_: tensorflow.Tensor, dim: int, indices: List) tensorflow.Tensor
Parameters
  • input (tensor) – input tensor

  • dim (int) – dimension

  • indices (List) – selected indices list

Returns

Tensor