art.estimators.gan
¶
GAN Estimator API.
TensorFlowV2 GAN¶
- class art.estimators.gan.TensorFlowV2GAN(generator: GENERATOR_TYPE, discriminator: CLASSIFIER_TYPE, generator_loss=None, discriminator_loss=None, generator_optimizer_fct=None, discriminator_optimizer_fct=None)¶
This class implements a GAN with the TensorFlow v2 framework.
- __init__(generator: GENERATOR_TYPE, discriminator: CLASSIFIER_TYPE, generator_loss=None, discriminator_loss=None, generator_optimizer_fct=None, discriminator_optimizer_fct=None)¶
Initialization of a test TensorFlow v2 GAN
- Parameters
generator – a TensorFlow2 generator
discriminator – a TensorFlow v2 discriminator
generator_loss – the loss function to use for the generator
discriminator_loss – the loss function to use for the discriminator
generator_optimizer_fct – the optimizer function to use for the generator
discriminator_optimizer_fct – the optimizer function to use for the discriminator
- 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 discriminator: CLASSIFIER_TYPE¶
- Returns
the discriminator
- property discriminator_loss: tf.Tensor¶
- Returns
the loss fct used for the discriminator
- property discriminator_optimizer_fct: tf.Tensor¶
- Returns
the optimizer function for the discriminator
- fit(x: ndarray, y: ndarray, batch_size: int = 128, nb_epochs: int = 20, **kwargs) None ¶
Creates a generative model
- Parameters
x (
ndarray
) – the secret backdoor trigger that will produce the targety (
ndarray
) – the target to produce when using the triggerbatch_size (
int
) – batch_size of images used to train generatornb_epochs (
int
) – total number of iterations for performing the attack
- 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.
- property generator: GENERATOR_TYPE¶
- Returns
the generator
- property generator_loss: tf.Tensor¶
- Returns
the loss fct used for the generator
- property generator_optimizer_fct: tf.Tensor¶
- Returns
the optimizer function for the generator
- get_activations(x: ndarray, layer: Union[int, str], batch_size: int, framework: bool = False) ndarray ¶
Return the output of a specific layer for samples x where layer is the index of the layer between 0 and nb_layers - 1 or the name of the layer. The number of layers can be determined by counting the results returned by calling `layer_names.
- Return type
ndarray
- Parameters
x (
ndarray
) – Sampleslayer – Index or name of the layer.
batch_size (
int
) – Batch size.framework (
bool
) – If true, return the intermediate tensor representation of the activation.
- Returns
The output of layer, where the first dimension is the batch size corresponding to x.
- 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, 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)¶
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¶
Return the model.
- Returns
The model.
- predict(x: ndarray, batch_size: int = 128, **kwargs) ndarray ¶
Generates a sample.
- Return type
ndarray
- Parameters
x (
ndarray
) – A input seed.batch_size (
int
) – The batch size for predictions.
- Returns
The generated sample.
- set_params(**kwargs) None ¶
Take a dictionary of parameters and apply checks before setting them as attributes.
- Parameters
kwargs – A dictionary of attributes.