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 GAN 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
¶ - Returns
Boolean to indicate index of the color channels in the sample x.
-
property
clip_values
¶ Return the clip values of the input samples.
- Returns
Clip values (min, max).
-
compute_loss
(x: np.ndarray, y: np.ndarray, **kwargs) → np.ndarray¶ Compute the loss of the neural network for samples x.
- Parameters
x – Samples of shape (nb_samples, nb_features) or (nb_samples, nb_pixels_1, nb_pixels_2, nb_channels) or (nb_samples, nb_channels, nb_pixels_1, nb_pixels_2).
y – Target values (class labels) one-hot-encoded of shape (nb_samples, nb_classes) or indices of shape (nb_samples,).
- Returns
Loss values.
- Return type
Format as expected by the model
-
property
encoding_length
¶ Returns the length of the encoding size output.
- Returns
The length of the encoding size output.
-
property
feed_dict
¶ Return the feed dictionary for the session run evaluating the classifier.
- Returns
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: np.ndarray, layer: Union[int, str], batch_size: int, framework: bool = False) → np.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
¶ Return the input placeholder.
- Returns
The input placeholder.
-
property
input_shape
¶ Return the shape of one input sample.
- Returns
Shape of one input sample.
-
property
layer_names
¶ 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
¶ Return the loss function.
- Returns
The loss function.
-
loss_gradient
(x, y, training_mode: bool = False, **kwargs) → np.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
¶ Returns the generator tensor.
- Returns
The generator tensor.
-
predict
(x: np.ndarray, batch_size: int = 128, **kwargs) → np.ndarray¶ Perform projections over a batch of encodings.
- Parameters
x – Encodings.
batch_size (
int
) – Batch size.
- Returns
Array of prediction projections of shape (num_inputs, nb_classes).
-
property
sess
¶ 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.
-