nsl.lib.gen_adv_neighbor
Stay organized with collections
Save and categorize content based on your preferences.
Generates adversarial neighbors for the given input and loss.
nsl.lib.gen_adv_neighbor(
input_features,
labeled_loss,
config,
raise_invalid_gradient=False,
gradient_tape=None,
pgd_model_fn=None,
pgd_loss_fn=None,
pgd_labels=None,
use_while_loop=False
)
This function implements the following operation:
adv_neighbor = input_features + adv_step_size * gradient
where adv_step_size
is the step size (analogous to learning rate) for
searching/calculating adversarial neighbor.
Args |
input_features
|
a dense (float32) tensor, a list of dense tensors, or a
dictionary of feature names and dense tensors. The shape of the tensor(s)
should be either:
(a) pointwise samples: [batch_size, feat_len] , or
(b) sequence samples: [batch_size, seq_len, feat_len] . Note that only
dense (float ) tensors in input_features will be perturbed and all
other features (int , string , or SparseTensor ) will be kept as-is
in the returning adv_neighbor .
|
labeled_loss
|
A scalar tensor of floating point type calculated from true
labels (or supervisions).
|
config
|
A nsl.configs.AdvNeighborConfig object containing the following
hyperparameters for generating adversarial samples.
- 'feature_mask': mask (with 0-1 values) applied on the graident.
- 'adv_step_size': step size to find the adversarial sample.
- 'adv_grad_norm': type of tensor norm to normalize the gradient.
|
raise_invalid_gradient
|
(optional) A Boolean flag indicating whether to
raise an error when gradients cannot be computed on any input feature.
There are three cases where this error may happen: (1) The feature is a
SparseTensor . (2) The feature has a non-differentiable dtype , like
string or integer. (3) The feature is not involved in loss computation.
If set to False (default), those inputs without gradient will be
ignored silently and not perturbed.
|
gradient_tape
|
A tf.GradientTape object watching the calculation from
input_features to labeled_loss . Can be omitted if running in graph
mode.
|
pgd_model_fn
|
The model to generate adversaries for. Generates predictions
for a given set of inputs, in the shape of input_features .
|
pgd_loss_fn
|
The loss function. Takes samples of labels and a model
predictions.
|
pgd_labels
|
labels for the input features. This should have shape
[batch_size, 1] . Required to generate adversaries with PGD, unused
otherwise.
|
use_while_loop
|
A Boolean indicating whether the PGD steps should be done in
a tf.while_loop . This can potentially reduce memory footprint, but is
not compatible to pgd_model_fn with side effects. (default=False)
|
Returns |
adv_neighbor
|
The perturbed example, with the same shape and structure as
input_features .
|
adv_weight
|
A dense Tensor with shape of [batch_size, 1] ,
representing the weight for each neighbor.
|
Raises |
ValueError
|
In case of raise_invalid_gradient is set and some of the input
features cannot be perturbed. See raise_invalid_gradient for situations
where this can happen.
|
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.
[null,null,["Last updated 2022-10-28 UTC."],[],[],null,["# nsl.lib.gen_adv_neighbor\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/adversarial_neighbor.py#L343-L415) |\n\nGenerates adversarial neighbors for the given input and loss. \n\n nsl.lib.gen_adv_neighbor(\n input_features,\n labeled_loss,\n config,\n raise_invalid_gradient=False,\n gradient_tape=None,\n pgd_model_fn=None,\n pgd_loss_fn=None,\n pgd_labels=None,\n use_while_loop=False\n )\n\nThis function implements the following operation:\n`adv_neighbor = input_features + adv_step_size * gradient`\nwhere `adv_step_size` is the step size (analogous to learning rate) for\nsearching/calculating adversarial neighbor.\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Args ---- ||\n|--------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| `input_features` | a dense (float32) tensor, a list of dense tensors, or a dictionary of feature names and dense tensors. The shape of the tensor(s) should be either: (a) pointwise samples: `[batch_size, feat_len]`, or (b) sequence samples: `[batch_size, seq_len, feat_len]`. Note that only dense (`float`) tensors in `input_features` will be perturbed and all other features (`int`, `string`, or `SparseTensor`) will be kept as-is in the returning `adv_neighbor`. |\n| `labeled_loss` | A scalar tensor of floating point type calculated from true labels (or supervisions). |\n| `config` | A [`nsl.configs.AdvNeighborConfig`](../../nsl/configs/AdvNeighborConfig) object containing the following hyperparameters for generating adversarial samples. \u003cbr /\u003e - 'feature_mask': mask (with 0-1 values) applied on the graident. - 'adv_step_size': step size to find the adversarial sample. - 'adv_grad_norm': type of tensor norm to normalize the gradient. |\n| `raise_invalid_gradient` | (optional) A Boolean flag indicating whether to raise an error when gradients cannot be computed on any input feature. There are three cases where this error may happen: (1) The feature is a `SparseTensor`. (2) The feature has a non-differentiable `dtype`, like string or integer. (3) The feature is not involved in loss computation. If set to `False` (default), those inputs without gradient will be ignored silently and not perturbed. |\n| `gradient_tape` | A [`tf.GradientTape`](https://www.tensorflow.org/api_docs/python/tf/GradientTape) object watching the calculation from `input_features` to `labeled_loss`. Can be omitted if running in graph mode. |\n| `pgd_model_fn` | The model to generate adversaries for. Generates predictions for a given set of inputs, in the shape of `input_features`. |\n| `pgd_loss_fn` | The loss function. Takes samples of labels and a model predictions. |\n| `pgd_labels` | labels for the input features. This should have shape `[batch_size, 1]`. Required to generate adversaries with PGD, unused otherwise. |\n| `use_while_loop` | A Boolean indicating whether the PGD steps should be done in a [`tf.while_loop`](https://www.tensorflow.org/api_docs/python/tf/while_loop). This can potentially reduce memory footprint, but is not compatible to `pgd_model_fn` with side effects. (default=False) |\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Returns ------- ||\n|----------------|----------------------------------------------------------------------------------------------|\n| `adv_neighbor` | The perturbed example, with the same shape and structure as `input_features`. |\n| `adv_weight` | A dense `Tensor` with shape of `[batch_size, 1]`, representing the weight for each neighbor. |\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Raises ------ ||\n|--------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| `ValueError` | In case of `raise_invalid_gradient` is set and some of the input features cannot be perturbed. See `raise_invalid_gradient` for situations where this can happen. |\n\n\u003cbr /\u003e"]]