Skip to content

Commit 0384ee1

Browse files
Neural-Link Teamtensorflow-copybara
authored andcommitted
Improves documentation comments in the regularizer.py, utils.py, and configs.py files.
PiperOrigin-RevId: 271235203
1 parent 4d72a90 commit 0384ee1

File tree

3 files changed

+164
-137
lines changed

3 files changed

+164
-137
lines changed

neural_structured_learning/configs/configs.py

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ class AdvNeighborConfig(object):
4545
0.001.
4646
adv_grad_norm: type of tensor norm to normalize the gradient. Input will be
4747
converted to `nsl.configs.NormType` when applicable (e.g., `'l2'` ->
48-
`NormType.L2`). Default set to L2 norm.
48+
`nls.configs.NormType.L2`). Default set to L2 norm.
4949
"""
5050
feature_mask = attr.ib(default=None)
5151
adv_step_size = attr.ib(default=0.001)
@@ -112,7 +112,8 @@ class AdvTargetConfig(object):
112112
113113
Attributes:
114114
target_method: type of adversarial targeting method. The value needs to be
115-
one of the enums from AdvTargetType (e.g., AdvTargetType.LEAST).
115+
one of the enums from `nsl.configs.AdvTargetType` (e.g.,
116+
`nsl.configs.AdvTargetType.LEAST`).
116117
random_seed: a Python integer as seed in 'random_uniform' op.
117118
"""
118119
target_method = attr.ib(default=AdvTargetType.GROUND_TRUTH)
@@ -176,16 +177,17 @@ def all(cls):
176177

177178
@attr.s
178179
class DecayConfig(object):
179-
"""Contains configuration for computing decayed value.
180+
"""Contains configuration for decaying a value during training.
180181
181182
Attributes:
182-
decay_steps: A scalar int32 or int64 Tensor or a Python number. How often to
183-
apply decay. Must be positive.
184-
decay_rate: A scalar float32 or float64 Tensor or a Python number. Default
185-
set to 0.96.
186-
min_value: minimal acceptable value after applying decay. Default set to 0.0
187-
decay_type: Type of decay function to apply. Default set to
188-
DecayType.EXPONENTIAL_DECAY.
183+
decay_steps: A scalar `int32` or `int64` Tensor or a Python number that
184+
specifies the decay frequency, specied in units of training steps. Must be
185+
positive.
186+
decay_rate: A scalar `float32` or `float64` Tensor or a Python number.
187+
Defaults to 0.96.
188+
min_value: minimal acceptable value after applying decay. Defaults to 0.0.
189+
decay_type: Type of decay function to apply. Defaults to
190+
`nsl.configs.DecayType.EXPONENTIAL_DECAY`.
189191
"""
190192
decay_steps = attr.ib()
191193
decay_rate = attr.ib(default=0.96)
@@ -224,17 +226,18 @@ class VirtualAdvConfig(object):
224226
"""Contains configuration for virtual adversarial training.
225227
226228
Attributes:
227-
adv_neighbor_config: an AdvNeighborConfig object for generating virtual
228-
adversarial examples. Default set to AdvNeighborConfig.
229-
distance_config: a DistanceConfig object for calculating virtual adversarial
230-
loss. Default set to DistanceConfig.
229+
adv_neighbor_config: an `nsl.configs.AdvNeighborConfig` object for
230+
generating virtual adversarial examples. Defaults to
231+
`nsl.configs.AdvNeighborConfig()`.
232+
distance_config: a `nsl.configs.DistanceConfig` object for calculating
233+
virtual adversarial loss. Defaults to `nsl.configs.DistanceConfig()`.
231234
num_approx_steps: number of steps used to approximate the calculation of
232-
Hessian matrix required for creating virtual adversarial examples. Default
233-
set to 1.
235+
Hessian matrix required for creating virtual adversarial examples.
236+
Defaults to 1.
234237
approx_difference: the finite difference to approximate the calculation of
235-
Hessian matrix required for creating virtual adversarial examples. (The
236-
`xi` in Equation 12 in the paper: https://arxiv.org/pdf/1704.03976.pdf)
237-
Default set to 1e-6.
238+
the Hessian matrix required for creating virtual adversarial examples,
239+
namely, the `xi` in Equation 12 in the paper:
240+
https://arxiv.org/pdf/1704.03976.pdf. Defaults to 1e-6.
238241
"""
239242
adv_neighbor_config = attr.ib(default=AdvNeighborConfig())
240243
distance_config = attr.ib(default=DistanceConfig())
@@ -270,8 +273,9 @@ class GraphRegConfig(object):
270273
"""Contains the configuration for graph regularization.
271274
272275
Attributes:
273-
neighbor_config: An instance of `GraphNeighborConfig` that describes
274-
neighbor attributes for graph regularization.
276+
neighbor_config: A `nsl.configs.GraphNeighborConfig` instance that describes
277+
neighbor attributes for graph regularization. Defaults to
278+
`nsl.configs.GraphNeighborConfig()`.
275279
multiplier: The multiplier or weight factor applied on the graph
276280
regularization loss term. This value has to be non-negative. Defaults to
277281
0.01.

neural_structured_learning/lib/regularizer.py

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,17 @@ def adv_regularizer(adv_neighbors, target_scores, model_fn, loss_fn):
2323
"""Calculates adversarial loss from generated adversarial samples.
2424
2525
Args:
26-
adv_neighbors: dense (float32) tensor, with two possible shapes: (a)
27-
pointwise samples: batch_size x feat_len, or (b) sequence samples:
28-
batch_size x seq_len x feat_len
26+
adv_neighbors: dense `float32` tensor, with two possible shapes: (a)
27+
`[batch_size, feat_len]` for pointwise samples, or (b)
28+
`[batch_size, seq_len, feat_len]` for sequence samples.
2929
target_scores: target tensor used to compute loss.
30-
model_fn: a method that has input tensor (same shape as adv_neighbors),
31-
is_train and reuse as input, returns predicted logits.
32-
loss_fn: a loss function that has target and predction as input, and returns
33-
a float scalar
30+
model_fn: a method that has input tensor (same shape as `adv_neighbors`),
31+
`is_train` and `reuse` as inputs, and returns predicted logits.
32+
loss_fn: a loss function that has `target` and `prediction` as inputs, and
33+
returns a `float32` scalar.
3434
3535
Returns:
36-
adv_loss: a scalar (float32) for adversarial loss.
36+
adv_loss: a `float32` denoting the adversarial loss.
3737
"""
3838
adv_predictions = model_fn(
3939
adv_neighbors, is_train=tf.constant(False), reuse=True)
@@ -84,25 +84,28 @@ def virtual_adv_regularizer(input_layer,
8484
embedding_fn,
8585
virtual_adv_config,
8686
embedding=None):
87-
"""API to calculate virtual adversarial loss for given input.
87+
"""Calculates virtual adversarial loss for the given input.
8888
8989
Virtual adversarial loss is defined as the distance between the embedding of
90-
input and that of slightly perturbed input. Optimizing this loss helps
91-
smoothen models locally.
90+
the input and that of a slightly perturbed input. Optimizing this loss helps
91+
smooth models locally.
9292
9393
Reference paper: https://arxiv.org/pdf/1704.03976.pdf
9494
9595
Args:
96-
input_layer: a dense tensor for input features, with batch_size as the 1st
97-
dimension.
98-
embedding_fn: a unary function that takes the input layer to compute and
99-
return its embedding.
100-
virtual_adv_config: a VirtualAdvConfig object.
101-
embedding: (optional) a dense tensor for embedding of the input_layer. If
102-
not provided, it will be calculated by `embedding_fn(input_layer)`.
96+
input_layer: a dense tensor for input features whose first dimension is the
97+
training batch size.
98+
embedding_fn: a unary function that computes the embedding for the given
99+
`input_layer` input.
100+
virtual_adv_config: an `nsl.configs.VirtualAdvConfig` object that specifies
101+
parameters for generating adversarial examples and computing the the
102+
adversarial loss.
103+
embedding: (optional) a dense tensor representing the embedding of
104+
`input_layer`. If not provided, it will be calculated as
105+
`embedding_fn(input_layer)`.
103106
104107
Returns:
105-
virtual_adv_loss: a scalar (float32) for virtural adversarial loss.
108+
virtual_adv_loss: a `float32` denoting the virtural adversarial loss.
106109
"""
107110

108111
if embedding is None:

0 commit comments

Comments
 (0)