art.experimental.estimators.classification

Experimental classifiers.

JAX Classifier

class art.experimental.estimators.classification.JaxClassifier(model: List, predict_func: Callable, loss_func: Callable, update_func: Callable, input_shape: Tuple[int, ...], nb_classes: int, 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 classifier with the Jax framework.

__init__(model: List, predict_func: Callable, loss_func: Callable, update_func: Callable, input_shape: Tuple[int, ...], nb_classes: int, 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)) None

Initialization specifically for the Jax-based implementation.

Parameters
  • model (List) – Jax model, represented as a list of model parameters.

  • predict_func (Callable) – A function used to predict model output given the model and the input.

  • loss_func (Callable) – The loss function for which to compute gradients for training.

  • update_func (Callable) – The update function for which to train the model.

  • input_shape (Tuple) – The shape of one input instance.

  • nb_classes (int) – The number of classes of the model.

  • 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.

class_gradient(x: numpy.ndarray, label: Optional[Union[int, List[int]]] = None, **kwargs) numpy.ndarray

Compute per-class derivatives w.r.t. x.

Return type

ndarray

Parameters
  • x (ndarray) – Sample input with shape as expected by the model.

  • label – Index of a specific per-class derivative. If an integer is provided, the gradient of that class output is computed for all samples. If multiple values as provided, the first dimension should match the batch size of x, and each value will be used as target for its corresponding sample in x. If None, then gradients for all classes will be computed for each sample.

Returns

Array of gradients of input features w.r.t. each class in the form (batch_size, nb_classes, input_shape) when computing for all classes, otherwise shape becomes (batch_size, 1, input_shape) when label parameter is specified.

property clip_values: Optional[CLIP_VALUES_TYPE]

Return the clip values of the input samples.

Returns

Clip values (min, max).

compute_loss(x: numpy.ndarray, y: numpy.ndarray, **kwargs) numpy.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

fit(*args, **kwargs)

Fit the classifier on the training set (x, y).

Parameters
  • x – Training data.

  • y – Target values.

  • batch_size – Size of batches.

  • nb_epochs – Number of epochs to use for training.

  • kwargs – Dictionary of framework-specific arguments. This parameter is not currently supported for PyTorch and providing it takes no effect.

fit_generator(generator: DataGenerator, nb_epochs: int = 20, **kwargs) None

Fit the classifier using the generator that yields batches as specified.

Parameters
  • generator – Batch generator providing (x, y) for each epoch.

  • nb_epochs (int) – Number of epochs to use for training.

  • kwargs – Dictionary of framework-specific arguments.

get_activations(x: numpy.ndarray, layer: Optional[Union[str, int]] = None, batch_size: int = 128, framework: bool = False) numpy.ndarray

Return the output of the specified layer for input x. layer is specified by layer index (between 0 and nb_layers - 1) or by name. The number of layers can be determined by counting the results returned by calling layer_names.

Return type

ndarray

Parameters
  • x (ndarray) – Input for computing the activations.

  • layer – Layer for computing the activations

  • batch_size (int) – Size of batches.

  • 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, ...]

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.

property loss_func: Callable

Return the loss function.

Returns

The loss function.

loss_gradient(x: numpy.ndarray, y: numpy.ndarray, **kwargs) numpy.ndarray

Compute the gradient of the loss function w.r.t. x.

Return type

ndarray

Parameters
  • x (ndarray) – Sample input with shape as expected by the model.

  • y (ndarray) – Target values.

Returns

Array of gradients of the same shape as x.

property model: List

Return the model.

Returns

The model.

property nb_classes: int

Return the number of output classes.

Returns

Number of classes in the data.

predict(*args, **kwargs)

Perform prediction for a batch of inputs.

Parameters
  • x – Input samples.

  • batch_size – Size of batches.

Returns

Array of predictions of shape (nb_inputs, nb_classes).

property predict_func: Callable

Return the predict function.

Returns

The predict function.

save(filename: str, path: Optional[str] = None) None

Save a model to file in the format specific to the backend framework.

Parameters
  • filename (str) – Name of the file where to store the model.

  • path – Path of the folder where to store the model. If no path is specified, the model will be stored in the default data location of the library ART_DATA_PATH.

set_params(**kwargs) None

Take a dictionary of parameters and apply checks before setting them as attributes.

Parameters

kwargs – A dictionary of attributes.

property update_func: Callable

Return the update function.

Returns

The update function.