art.estimators.generation
¶
Generator API.
Mixin Base Class Generator¶
TensorFlow Generator¶
- class art.estimators.generation.TensorFlowGenerator(input_ph: tf.Placeholder, model: tf.Tensor, loss: Optional[tf.Tensor] = None, sess: Optional[tf.compat.v1.Session] = None, channels_first=False, clip_values: Optional[CLIP_VALUES_TYPE] = None, preprocessing_defences: Optional[Union[Preprocessor, List[Preprocessor]]] = None, postprocessing_defences: Optional[Union[Postprocessor, List[Postprocessor]]] = None, preprocessing: PREPROCESSING_TYPE = (0.0, 1.0), feed_dict: Optional[Dict[Any, Any]] = None)¶
This class implements a DGM with the TensorFlow framework.
- __init__(input_ph: tf.Placeholder, model: tf.Tensor, loss: Optional[tf.Tensor] = None, sess: Optional[tf.compat.v1.Session] = None, channels_first=False, clip_values: Optional[CLIP_VALUES_TYPE] = None, preprocessing_defences: Optional[Union[Preprocessor, List[Preprocessor]]] = None, postprocessing_defences: Optional[Union[Postprocessor, List[Postprocessor]]] = None, preprocessing: PREPROCESSING_TYPE = (0.0, 1.0), feed_dict: Optional[Dict[Any, Any]] = None)¶
Initialization specific to TensorFlow generator implementations.
- Parameters:
input_ph – The input placeholder.
model – TensorFlow model, neural network or other.
loss – The loss function for which to compute gradients. This parameter is necessary when training the model and when computing gradients w.r.t. the loss function.
sess – Computation session.
channels_first (
bool
) – Set channels first or last.clip_values – Tuple of the form (min, max) of floats or np.ndarray representing the minimum and maximum values allowed for features. If floats are provided, these will be used as the range of all features. If arrays are provided, each value will be considered the bound for a feature, thus the shape of clip values needs to match the total number of features.
preprocessing_defences – Preprocessing defence(s) to be applied by the classifier.
postprocessing_defences – Postprocessing defence(s) to be applied by the classifier.
preprocessing – Tuple of the form (subtrahend, divisor) of floats or np.ndarray of values to be used for data preprocessing. The first value will be subtracted from the input. The input will then be divided by the second one.
feed_dict – A feed dictionary for the session run evaluating the classifier. This dictionary includes all additionally required placeholders except the placeholders defined in this class.
- property channels_first: bool¶
- Returns:
Boolean to indicate index of the color channels in the sample x.
- property clip_values: Optional[CLIP_VALUES_TYPE]¶
Return the clip values of the input samples.
- Returns:
Clip values (min, max).
- clone_for_refitting() ESTIMATOR_TYPE ¶
Clone estimator for refitting.
- compute_loss(x: ndarray, y: ndarray, **kwargs) ndarray ¶
Compute the loss of the estimator for samples x.
- Parameters:
x (
ndarray
) – Input samples.y (
ndarray
) – Target values.
- Returns:
Loss values.
- Return type:
Format as expected by the model
- compute_loss_from_predictions(pred: ndarray, y: ndarray, **kwargs) ndarray ¶
Compute the loss of the estimator for predictions pred.
- Return type:
ndarray
- Parameters:
pred (
ndarray
) – Model predictions.y (
ndarray
) – Target values.
- Returns:
Loss values.
- property encoding_length: int¶
Returns the length of the encoding size output. :return: The length of the encoding size output.
- property feed_dict: Dict[Any, Any]¶
Return the feed dictionary for the session run evaluating the classifier. :return: The feed dictionary for the session run evaluating the classifier.
- fit(x, y, batch_size=128, nb_epochs=10, **kwargs)¶
Do nothing.
- fit_generator(generator: DataGenerator, nb_epochs: int = 20, **kwargs) None ¶
Fit the estimator using a generator yielding training batches. Implementations can provide framework-specific versions of this function to speed-up computation.
- Parameters:
generator – Batch generator providing (x, y) for each epoch.
nb_epochs (
int
) – Number of training epochs.
- get_activations(x: ndarray, layer: Union[int, str], batch_size: int, framework: bool = False) ndarray ¶
Do nothing.
- get_params() Dict[str, Any] ¶
Get all parameters and their values of this estimator.
- Returns:
A dictionary of string parameter names to their value.
- property input_ph: tf.Placeholder¶
Return the input placeholder. :return: The input placeholder.
- property input_shape: Tuple[int, ...]¶
Return the shape of one input sample. :return: Shape of one input sample.
- property layer_names: Optional[List[str]]¶
Return the names of the hidden layers in the model, if applicable.
- Returns:
The names of the hidden layers in the model, input and output layers are ignored.
Warning
layer_names tries to infer the internal structure of the model. This feature comes with no guarantees on the correctness of the result. The intended order of the layers tries to match their order in the model, but this is not guaranteed either.
- property loss: tf.Tensor¶
Return the loss function :return: The loss function.
- loss_gradient(x, y, training_mode: bool = False, **kwargs) ndarray ¶
Compute the gradient of the loss function w.r.t. x.
- Parameters:
x (Format as expected by the model) – Samples.
y (Format as expected by the model) – Target values.
- Returns:
Loss gradients w.r.t. x in the same format as x.
- Return type:
Format as expected by the model
- property model: tf.Tensor¶
Returns the generator tensor. :return: The generator tensor.
- predict(x: ndarray, batch_size: int = 128, **kwargs) ndarray ¶
Perform projections over a batch of encodings.
- Return type:
ndarray
- Parameters:
x (
ndarray
) – Encodings.batch_size (
int
) – Batch size.
- Returns:
Array of prediction projections of shape (num_inputs, nb_classes).
- property sess: tf.python.client.session.Session¶
Get current TensorFlow session.
- Returns:
The current TensorFlow session.
- set_params(**kwargs) None ¶
Take a dictionary of parameters and apply checks before setting them as attributes.
- Parameters:
kwargs – A dictionary of attributes.
TensorFlow 2 Generator¶
- class art.estimators.generation.TensorFlowV2Generator(encoding_length: int, model: tf.Tensor, channels_first: bool = False, clip_values: Optional[CLIP_VALUES_TYPE] = None, preprocessing_defences: Optional[Union[Preprocessor, List[Preprocessor]]] = None, postprocessing_defences: Optional[Union[Postprocessor, List[Postprocessor]]] = None, preprocessing: PREPROCESSING_TYPE = (0.0, 1.0))¶
This class implements a DGM with the TensorFlow framework.
- __init__(encoding_length: int, model: tf.Tensor, channels_first: bool = False, clip_values: Optional[CLIP_VALUES_TYPE] = None, preprocessing_defences: Optional[Union[Preprocessor, List[Preprocessor]]] = None, postprocessing_defences: Optional[Union[Postprocessor, List[Postprocessor]]] = None, preprocessing: PREPROCESSING_TYPE = (0.0, 1.0))¶
Initialization specific to TensorFlow generator implementations.
- Encoding_length:
length of the input seed
- Model:
TensorFlow model, neural network or other.
- Parameters:
channels_first (
bool
) – Set channels first or last.clip_values – Tuple of the form (min, max) of floats or np.ndarray representing the minimum and maximum values allowed for features. If floats are provided, these will be used as the range of all features. If arrays are provided, each value will be considered the bound for a feature, thus the shape of clip values needs to match the total number of features.
preprocessing_defences – Preprocessing defence(s) to be applied by the classifier.
postprocessing_defences – Postprocessing defence(s) to be applied by the classifier.
preprocessing – Tuple of the form (subtrahend, divisor) of floats or np.ndarray of values to be used for data preprocessing. The first value will be subtracted from the input. The input will then be divided by the second one.
- property channels_first: bool¶
- Returns:
Boolean to indicate index of the color channels in the sample x.
- property clip_values: Optional[CLIP_VALUES_TYPE]¶
Return the clip values of the input samples.
- Returns:
Clip values (min, max).
- clone_for_refitting() ESTIMATOR_TYPE ¶
Clone estimator for refitting.
- compute_loss(x: ndarray, y: Any, **kwargs) ndarray ¶
Compute the loss of the estimator for samples x.
- Parameters:
x (
ndarray
) – Input samples.y – Target values.
- Returns:
Loss values.
- Return type:
Format as expected by the model
- compute_loss_from_predictions(pred: ndarray, y: ndarray, **kwargs) ndarray ¶
Compute the loss of the estimator for predictions pred.
- Return type:
ndarray
- Parameters:
pred (
ndarray
) – Model predictions.y (
ndarray
) – Target values.
- Returns:
Loss values.
- property encoding_length: int¶
- Returns:
The length of the encoding size output.
- fit(x, y, batch_size=128, nb_epochs=10, **kwargs)¶
Do nothing.
- fit_generator(generator: DataGenerator, nb_epochs: int = 20, **kwargs) None ¶
Fit the estimator using a generator yielding training batches. Implementations can provide framework-specific versions of this function to speed-up computation.
- Parameters:
generator – Batch generator providing (x, y) for each epoch.
nb_epochs (
int
) – Number of training epochs.
- get_activations(x: ndarray, layer: Union[int, str], batch_size: int, framework: bool = False) ndarray ¶
Do nothing.
- get_params() Dict[str, Any] ¶
Get all parameters and their values of this estimator.
- Returns:
A dictionary of string parameter names to their value.
- property input_shape: Tuple[int, ...]¶
Return the shape of one input sample.
- Returns:
Shape of one input sample.
- property layer_names: Optional[List[str]]¶
Return the names of the hidden layers in the model, if applicable.
- Returns:
The names of the hidden layers in the model, input and output layers are ignored.
Warning
layer_names tries to infer the internal structure of the model. This feature comes with no guarantees on the correctness of the result. The intended order of the layers tries to match their order in the model, but this is not guaranteed either.
- loss_gradient(x, y, **kwargs) ndarray ¶
Compute the gradient of the loss function w.r.t. x.
- Parameters:
x (Format as expected by the model) – Samples.
y (Format as expected by the model) – Target values.
- Returns:
Loss gradients w.r.t. x in the same format as x.
- Return type:
Format as expected by the model
- property model: tf.Tensor¶
- Returns:
The generator tensor.
- predict(x: ndarray, batch_size: int = 128, training_mode: bool = False, **kwargs) ndarray ¶
Perform projections over a batch of encodings.
- Return type:
ndarray
- Parameters:
x (
ndarray
) – Encodings.batch_size (
int
) – Batch size.training_mode (
bool
) – True for model set to training mode and ‘False for model set to evaluation mode.
- Returns:
Array of prediction projections of shape (num_inputs, nb_classes).
- set_params(**kwargs) None ¶
Take a dictionary of parameters and apply checks before setting them as attributes.
- Parameters:
kwargs – A dictionary of attributes.