nsl.lib.random_in_norm_ball
Stay organized with collections
Save and categorize content based on your preferences.
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.
|
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2022-10-28 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2022-10-28 UTC."],[],[],null,["# nsl.lib.random_in_norm_ball\n\n\u003cbr /\u003e\n\n|-------------------------------------------------------------------------------------------------------------------------------------------------|\n| [View source on GitHub](https://github.com/tensorflow/neural-structured-learning/blob/v1.4.0/neural_structured_learning/lib/utils.py#L133-L209) |\n\nGenerates a random sample in a norm ball, conforming the given structure. \n\n nsl.lib.random_in_norm_ball(\n tensor_structure, radius, norm_type\n )\n\nThis function expects `tensor_structure` to represent a batch of examples,\nwhere the tensors in the structure are features. The norms are calculated in\nthe whole feature space (including all tensors).\n\nFor L-inf norm, each feature dimension can be sampled independently from a\nuniform distribution. For L2 and L1 norms, we use the result (Corollary 4 in\nBarthe et al., 2005) that the first `n` coordinates of a uniformly random\npoint on the `p`-norm unit sphere in `R^(n+p)` are uniformly distributed in\nthe `p`-norm unit ball in `R^n`. For L2 norm, we draw random points on the\nunit sphere from the standard normal distribution which density is\nproportional to `exp(-x^2)`(Corollary 1 in Harman and Lacko, 2010). For L1\nnorm, we draw random points on the 1-norm unit sphere from the standard\nLaplace distribution which density is proportional to `exp(-|x|)`. More\ndetails on the papers: \u003chttps://arxiv.org/abs/math/0503650\u003e and\n\u003chttps://doi.org/10.1016/j.jmva.2010.06.002\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Args ---- ||\n|--------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| `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. |\n| `radius` | The radius of the norm ball to sample from. |\n| `norm_type` | One of [`nsl.configs.NormType`](../../nsl/configs/NormType) which specifies the norm of the norm ball. |\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Returns ------- ||\n|---|---|\n| 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. ||\n\n\u003cbr /\u003e"]]