View source on GitHub 
MinDiffLoss abstract base class.
model_remediation.min_diff.losses.MinDiffLoss(
membership_transform=None,
predictions_transform=None,
membership_kernel=None,
predictions_kernel=None,
name: Optional[Text] = None
)
Inherits from: tf.keras.losses.Loss
Arguments  

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 usingmembership
,predictions
and optionallysample_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.
Raises  

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
@abc.abstractmethod
call( membership: types.TensorType, predictions: types.TensorType, sample_weight: Optional[types.TensorType] = None )
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 .

from_config
@classmethod
from_config( config )
Creates a MinDiffLoss
instance from the config.
Any subclass with additional attributes or a different initialization
signature will need to override this method or get_config
.
Returns  

A new MinDiffLoss instance corresponding to config .

get_config
get_config()
Creates a config dictionary for the MinDiffLoss
instance.
Any subclass with additional attributes will need to override this method.
When doing so, users will mostly likely want to first call super
.
Returns  

A config dictionary for the MinDiffLoss isinstance.

__call__
__call__(
membership: types.TensorType,
predictions: types.TensorType,
sample_weight: Optional[types.TensorType] = None
)
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 .

_apply_kernels
_apply_kernels(
membership: types.TensorType,
predictions: types.TensorType
) > Tuple[types.TensorType, types.TensorType]
Applies losses.MinDiffKernel
attributes to corresponding inputs.
Arguments  

membership

membership column as described in MinDiffLoss.call .

predictions

predictions tensor as described in MinDiffLoss.call .

In particular, self.membership_kernel
, if not None
, will
be applied to membership
and self.predictions_kernel
, if not
None
, will be applied to predictions
.
loss = ... # MinDiffLoss subclass instance
loss.membership_kernel = min_diff.losses.GaussKernel()
loss.predictions_kernel = min_diff.losses.LaplaceKernel()
# Call with test inputs.
loss._apply_kernels([1, 2, 3], [4, 5, 6]) # (GaussKernel([1, 2, 3]),
# LaplaceKernel([4, 5, 6]))
If self.*_kernel
is None
, then the corresponding input is returned
unchanged.
loss = ... # MinDiffLoss subclass instance
loss.membership_kernel = None
loss.predictions_kernel = min_diff.losses.GaussKernel
# Call with test inputs.
loss._apply_kernels([1, 2, 3], [4, 5, 6]) # ([1, 2, 3], GaussKernel([4, 5, 6]))
# With both kernels set to None, _apply_kernels is the identity.
loss.predictions_kernel = None
# Call with test inputs.
loss._apply_kernels([1, 2, 3], [4, 5, 6]) # ([1, 2, 3], [4, 5, 6])
This function is meant for internal use by subclasses when the instance is invoked.
Returns  

Tuple of (membership_kernel_output , predictions_kernel_output ).

_preprocess_inputs
_preprocess_inputs(
membership: types.TensorType,
predictions: types.TensorType,
sample_weight: Optional[types.TensorType] = None
) > Tuple[types.TensorType, types.TensorType, types.TensorType]
Preprocesses inputs by applying transforms and normalizing weights.
Arguments  

membership

membership column as described in
MinDiffLoss.call .

predictions

predictions tensor as described in MinDiffLoss.call .

sample_weight

sample_weight tensor as described in MinDiffLoss.call .

The three inputs are processed in the following way:
membership
: hasself.membership_transform
applied to it, if it's present, and is cast totf.float32
.predictions
: hasself.predictions_transform
applied to it, if it's present, and is cast totf.float32
.sample_weight
: is validated, cast totf.float32
, and normalized. If it isNone
, it is set to a normalized tensor of equal weights.
This method is meant for internal use by subclasses when the instance is invoked.
Returns  

Tuple of (membership , predictions , normed_weights ).

Raises  

tf.errors.InvalidArgumentError

if sample_weight has negative
entries.
