torchimage package


torchimage.misc module

Miscellaneous utilities for PyTorch

Many of these functions have not been implemented in PyTorch

torchimage.misc.describe(x: torch.Tensor)

Describe the distribution of numbers in the input tensor.

This function mimics the behavior of DataFrame and Series describe method in pandas.


x (torch.Tensor) – Input tensor to be described


desc – A dictionary from keywords (such as mean, std) to float values

Return type


torchimage.misc.outer(u, v)

Compute the outer product of two tensors.

For two tensors u of shape \((k_1, k_2, ..., k_m)\) and v of shape \((l_1, l_2, ..., l_n)\), their outer product \(\mathbf{w} = \mathbf{u} \otimes \mathbf{v}\) is defined such that \(\mathbf{w}[i_1, i_2, ..., i_m, j_1, j_2, ..., j_n] = \mathbf{u}[i_1, i_2, ..., i_m] \mathbf{v}[j_1, j_2, ..., j_n]\). w will have \(m+n\) dimensions and the shape of \((k_1, k_2, ..., k_m, l_1, l_2, ..., l_n)\)

  • u (torch.Tensor or np.ndarray) – Input tensor with arbitrary shape.

  • v (torch.Tensor or np.ndarray) – Input tensor with arbitrary shape.


w – Outer product of u and v.

w.shape is the same as u.shape + v.shape.

Return type


torchimage.misc.poly1d(x: torch.Tensor, p: list)

Calculate single-variable (one-dimensional) polynomial y = a_n * x^n + ... + a_2 * x^2 + a_1 * x + a_0

The extra memory required by this function is at most 2 times the size of x, one to store the output tensor and the other to keep a running ith power of x.

  • x (torch.Tensor) – Input data tensor to be substituted for the variable x.

  • p (sequence of float) – List of weights in the order of descending exponents, namely [a_n, a_{n-1}, …, a_2, a_1, a_0].


y – Output tensor with the same shape as x

Return type


torchimage.misc.safe_power(x, exponent, *, epsilon=1e-06)

Takes the power of each element in input with exponent and returns a tensor with the result.

This is a safer version of torch.pow (out = x ** exponent), which avoids:

  1. NaN/imaginary output when x < 0 and exponent has a fractional part

    In this case, the function returns the signed (negative) magnitude of the complex number.

  2. NaN/infinite gradient at x = 0 when exponent has a fractional part

    In this case, the positions of 0 are added by epsilon, so the gradient is back-propagated as if x = epsilon.

However, this function doesn’t deal with float overflow, such as 1e10000.

  • x (torch.Tensor or float) – The input base value.

  • exponent (torch.Tensor or float) –

    The exponent value.

    (At least one of x and exponent must be a torch.Tensor)

  • epsilon (float) – A small floating point value to avoid infinite gradient. Default: 1e-6


out – The output tensor.

Return type


Module contents