rai_toolbox.perturbations.uniform_like_linf_n_ball_#

rai_toolbox.perturbations.uniform_like_linf_n_ball_(x, epsilon=1.0, param_ndim=None, generator=<torch._C.Generator object>)[source]#

Uniform sampling within an \(\epsilon\)-sized n-ball for \(L^{\infty}\)-norm. The result overwrites x in-place.

Parameters:
x: Tensor, shape-(N, D, …)

The tensor to generate a new random tensor from, i.e., returns a tensor of similar shape and on the same device.

epsilonfloat, optional (default=1)

Determines the radius of the ball.

param_ndimOptional[int]

Unused. Included for parity with other init functions.

generatortorch.Generator, optional (default=`torch.default_generator`)

Controls the RNG source.

Returns:
xTensor, shape-(N, D, …)

The input tensor, which has been modified in-place.

Examples

>>> import torch as tr
>>> from rai_toolbox.perturbations.init import uniform_like_linf_n_ball_
>>> x = tr.zeros(2, 3)
>>> uniform_like_linf_n_ball_(x, epsilon=2.0)
>>> x
tensor([[ 1.7092, -1.8723, -0.0806],
        [-1.4680, -1.8782, -0.1998]])
>>> x.abs() < 2.
tensor([[True, True, True],
        [True, True, True]])