alibi.utils.gradients module

alibi.utils.gradients.num_grad_batch(func, X, args=(), eps=1e-08)[source]

Calculate the numerical gradients of a vector-valued function (typically a prediction function in classification) with respect to a batch of arrays X.

Parameters
  • func (Callable) – Function to be differentiated

  • X (ndarray) – A batch of vectors at which to evaluate the gradient of the function

  • args (Tuple) – Any additional arguments to pass to the function

  • eps (Union[float, ndarray]) – Gradient step to use in the numerical calculation, can be a single float or one for each feature

Return type

ndarray

Returns

An array of gradients at each point in the batch X

alibi.utils.gradients.perturb(X, eps=1e-08, proba=False)[source]

Apply perturbation to instance or prediction probabilities. Used for numerical calculation of gradients.

Parameters
  • X (ndarray) – Array to be perturbed

  • eps (Union[float, ndarray]) – Size of perturbation

  • proba (bool) – If True, the net effect of the perturbation needs to be 0 to keep the sum of the probabilities equal to 1

Return type

Tuple[ndarray, ndarray]

Returns

Instances where a positive and negative perturbation is applied.