model_remediation.min_diff.losses.MinDiffLoss

MinDiffLoss abstract base class.

Inherits from: tf.keras.losses.Loss

membership_transform Transform function used on membership. If None is passed in then membership is left as is. The function must return a tf.Tensor.
predictions_transform Transform function used on predictions. If None is passed in then predictions is left as is. The function must return a tf.Tensor.
membership_kernel String (name of kernel) or min_diff.losses.MinDiffKernel to be applied on membership. If None is passed in, then membership is left untouched when applying kernels.
predictions_kernel String (name of kernel) or min_diff.losses.MinDiffKernel to be applied on predictions. If None is passed in, then predictions is left untouched when applying kernels.
name Name used for logging and tracking.

To be implemented by subclasses:

  • call(): Contains the logic for loss calculation using membership, predictions and optionally sample_weight.

Example subclass implementation:

class MyMinDiffLoss(MinDiffLoss):

  def call(membership, predictions, sample_weight=None):
    loss = ...  # Internal logic to calculate loss.
    return loss

A MinDiffLoss instance measures the difference in prediction scores (typically score distributions) between two groups of examples identified by the value in the membership column.

If the predictions between the two groups are indistinguishable, the loss should be 0. The more different the two scores are, the higher the loss.

ValueError If a *_transform parameter is passed in but is not callable.
ValueError If a *_kernel parameter has an unrecognized type or value.

Methods

call

View source

Invokes the MinDiffLoss instance.

Arguments
membership Numerical Tensor indicating whether examples are part of the sensitive_group. This is often denoted with 1.0 or 0.0 for True or False respectively but the details are determined by the subclass implementation. Shape must be [batch_size, 1].
predictions Tensor of model predictions for examples corresponding to those in membership.
sample_weight Tensor of weights per example.

This method contains the logic for calculating the loss. It must be implemented by subclasses.

Returns
Scalar min_diff_loss.

__call__

View source

Invokes the MinDiffLoss instance.

Args
membership Labels indicating whether examples are part of the sensitive group. Shape must be [batch_size, d0, .. dN].
predictions Predicted values. Must be the same shape as membership.
sample_weight (Optional) acts as a coefficient for the loss. Must be of shape [batch_size] or [batch_size, 1]. If None then a tensor of ones with the appropriate shape is used.

Returns
Scalar min_diff_loss.