Skip to content

Commit 1a3f730

Browse files
csferngtensorflow-copybara
authored andcommitted
Fix formatting of the docstrings for API doc.
PiperOrigin-RevId: 270134212
1 parent 7fca378 commit 1a3f730

File tree

2 files changed

+40
-39
lines changed

2 files changed

+40
-39
lines changed

neural_structured_learning/configs/configs.py

+7-7
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
14-
"""Classes for configuring modules in Neural Structured Learning."""
14+
"""Classes for configuring modules in Neural Structured Learning (NSL)."""
1515

1616
from __future__ import absolute_import
1717
from __future__ import division
@@ -38,14 +38,14 @@ class AdvNeighborConfig(object):
3838
"""Contains configuration for generating adversarial neighbors.
3939
4040
Attributes:
41-
feature_mask: mask (w/ 0-1 values) applied on gradient. The shape should be
42-
the same as (or broadcastable to) input features. If set to None, no
41+
feature_mask: mask (w/ 0-1 values) applied on the gradient. The shape should
42+
be the same as (or broadcastable to) input features. If set to `None`, no
4343
feature mask will be applied.
4444
adv_step_size: step size to find the adversarial sample. Default set to
4545
0.001.
4646
adv_grad_norm: type of tensor norm to normalize the gradient. Input will be
47-
converted to `NormType` when applicable (e.g., 'l2' -> NormType.L2).
48-
Default set to L2 norm.
47+
converted to `nsl.configs.NormType` when applicable (e.g., `'l2'` ->
48+
`NormType.L2`). Default set to L2 norm.
4949
"""
5050
feature_mask = attr.ib(default=None)
5151
adv_step_size = attr.ib(default=0.001)
@@ -59,8 +59,8 @@ class AdvRegConfig(object):
5959
Attributes:
6060
multiplier: multiplier to adversarial regularization loss. Default set to
6161
0.2.
62-
adv_neighbor_config: an AdvNeighborConfig object for generating adversarial
63-
neighbor examples.
62+
adv_neighbor_config: an `nsl.configs.AdvNeighborConfig` object for
63+
generating adversarial neighbor examples.
6464
"""
6565
multiplier = attr.ib(default=0.2)
6666
adv_neighbor_config = attr.ib(default=AdvNeighborConfig())

neural_structured_learning/lib/adversarial_neighbor.py

+33-32
Original file line numberDiff line numberDiff line change
@@ -223,47 +223,48 @@ def gen_adv_neighbor(input_features,
223223
config,
224224
raise_invalid_gradient=False,
225225
gradient_tape=None):
226-
"""Functional interface of _GenAdvNeighbor.
226+
"""Generates adversarial neighbors for the given input and loss.
227227
228-
This function provides a tensor/config-in & tensor-out functional interface
229-
that does the following:
230-
(a) Instantiates '_GenAdvNeighbor'
231-
(b) Invokes 'gen_neighbor' method
232-
(c) Returns the adversarial neighbors generated.
228+
This function implements the following operation:
229+
`adv_neighbor = input_features + adv_step_size * gradient`
230+
where `adv_step_size` is the step size (analogous to learning rate) for
231+
searching/calculating adversarial neighbor.
233232
234233
Arguments:
235-
input_features: a dense (float32) tensor or a dictionary of feature names
236-
and dense tensors. The shape of the tensor(s) should be either:
237-
(a) pointwise samples: [batch_size, feat_len], or
238-
(b) sequence samples: [batch_size, seq_len, feat_len]. Note that if the
239-
`input_features` is a dictionary, only dense (`float`) tensors in it
240-
will be perturbed and all other features (int, string, or sparse
241-
tensors) will be kept as-is in the returning `adv_neighbor`.
242-
labeled_loss: a scalar (float32) tensor calculated from true labels (or
243-
supervisions).
244-
config: `AdvNeighborConfig` object containing the following hyperparameters
245-
for generating adversarial samples. - 'feature_mask': mask (w/ 0-1 values)
246-
applied on graident. - 'adv_step_size': step size to find the adversarial
247-
sample. - 'adv_grad_norm': type of tensor norm to normalize the gradient.
248-
raise_invalid_gradient: (optional, default=False) a Boolean flag indicating
249-
whether to raise an error when gradients cannot be computed on some input
250-
features. There are three cases where gradients cannot be computed: (1)
251-
The feature is a SparseTensor. (2) The feature has a non-differentiable
252-
`tf.DType`, like string or integer. (3) The feature is not involved in
253-
loss computation. If set to False (default), those inputs without
254-
gradient will be ignored silently and not perturbed.
255-
gradient_tape: a `tf.GradientTape` object watching the calculation from
234+
input_features: A `Tensor` or a dictionary of `(feature_name, Tensor)`.
235+
The shape of the tensor(s) should be either:
236+
(a) pointwise samples: `[batch_size, feat_len]`, or
237+
(b) sequence samples: `[batch_size, seq_len, feat_len]`.
238+
Note that only dense (`float`) tensors in `input_features` will be
239+
perturbed and all other features (`int`, `string`, or `SparseTensor`) will
240+
be kept as-is in the returning `adv_neighbor`.
241+
labeled_loss: A scalar tensor of floating point type calculated from true
242+
labels (or supervisions).
243+
config: A `nsl.configs.AdvNeighborConfig` object containing the following
244+
hyperparameters for generating adversarial samples.
245+
- 'feature_mask': mask (with 0-1 values) applied on the graident.
246+
- 'adv_step_size': step size to find the adversarial sample.
247+
- 'adv_grad_norm': type of tensor norm to normalize the gradient.
248+
raise_invalid_gradient: (optional) A Boolean flag indicating whether to
249+
raise an error when gradients cannot be computed on any input feature.
250+
There are three cases where this error may happen:
251+
(1) The feature is a `SparseTensor`.
252+
(2) The feature has a non-differentiable `dtype`, like string or integer.
253+
(3) The feature is not involved in loss computation.
254+
If set to `False` (default), those inputs without gradient will be ignored
255+
silently and not perturbed.
256+
gradient_tape: A `tf.GradientTape` object watching the calculation from
256257
`input_features` to `labeled_loss`. Can be omitted if running in graph
257258
mode.
258259
259260
Returns:
260-
adv_neighbor: the perturbed example, with the same shape and structure as
261-
input_features
262-
adv_weight: a dense (float32) tensor with shape of [batch_size, 1],
263-
representing the weight for each neighbor
261+
adv_neighbor: The perturbed example, with the same shape and structure as
262+
`input_features`.
263+
adv_weight: A dense `Tensor` with shape of `[batch_size, 1]`,
264+
representing the weight for each neighbor.
264265
265266
Raises:
266-
ValueError: if `raise_invalid_gradient` is set and some of the input
267+
ValueError: In case of `raise_invalid_gradient` is set and some of the input
267268
features cannot be perturbed. See `raise_invalid_gradient` for situations
268269
where this can happen.
269270
"""

0 commit comments

Comments
 (0)