View source on GitHub |
Generates a random sample in a norm ball, conforming the given structure.
nsl.lib.random_in_norm_ball(
tensor_structure, radius, norm_type
)
This function expects tensor_structure
to represent a batch of examples,
where the tensors in the structure are features. The norms are calculated in
the whole feature space (including all tensors).
For L-inf norm, each feature dimension can be sampled independently from a
uniform distribution. For L2 and L1 norms, we use the result (Corollary 4 in
Barthe et al., 2005) that the first n
coordinates of a uniformly random
point on the p
-norm unit sphere in R^(n+p)
are uniformly distributed in
the p
-norm unit ball in R^n
. For L2 norm, we draw random points on the
unit sphere from the standard normal distribution which density is
proportional to exp(-x^2)
(Corollary 1 in Harman and Lacko, 2010). For L1
norm, we draw random points on the 1-norm unit sphere from the standard
Laplace distribution which density is proportional to exp(-|x|)
. More
details on the papers: https://arxiv.org/abs/math/0503650 and
https://doi.org/10.1016/j.jmva.2010.06.002
Args | |
---|---|
tensor_structure
|
A (nested) collection of batched, floating-point tensors. Only the structure (number of tensors, shape, and dtype) is used, not the values. The first dimension of each tensor is the batch size and must all be equal. |
radius
|
The radius of the norm ball to sample from. |
norm_type
|
One of nsl.configs.NormType which specifies the norm of the
norm ball.
|
Returns | |
---|---|
A (nested) collection of tensors in the same structure as tensor_structure
but has its values drawn randomly. The norm of each example in the batch
is less than or equal to the given radius.
|