Distances#

Distance base class#

class inFairness.distances.distance.Distance[source]#

Abstract base class for model distances

fit(**kwargs)[source]#

Fits the metric parameters for learnable metrics Default functionality is to do nothing. Subclass should overwrite this method to implement custom fit logic

abstract forward(x, y)[source]#

Subclasses must override this method to compute particular distances

Returns:

distance between two inputs

Return type:

Tensor

load_state_dict(state_dict, strict=True)[source]#

Copies parameters and buffers from state_dict into this module and its descendants. If strict is True, then the keys of state_dict must exactly match the keys returned by this module’s state_dict() function.

Parameters:
  • state_dict (dict) – a dict containing parameters and persistent buffers.

  • strict (bool, optional) – whether to strictly enforce that the keys in state_dict match the keys returned by this module’s state_dict() function. Default: True

Returns:

  • missing_keys is a list of str containing the missing keys

  • unexpected_keys is a list of str containing the unexpected keys

Return type:

NamedTuple with missing_keys and unexpected_keys fields

Note

If a parameter or buffer is registered as None and its corresponding key exists in state_dict, load_state_dict() will raise a RuntimeError.


Mahalanobis distance#

class inFairness.distances.MahalanobisDistances[source]#

Base class implementing the Generalized Mahalanobis Distances

Mahalanobis distance between two points X1 and X2 is computed as:

\[\text{dist}(X_1, X_2) = (X_1 - X_2) \Sigma (X_1 - X_2)^{T}\]
fit(sigma)[source]#

Fit Mahalanobis Distance metric

Parameters:

sigma (torch.Tensor) – Covariance matrix

forward(X1, X2, itemwise_dist=True)[source]#

Computes the distance between data samples X1 and X2

Parameters:
  • X1 (torch.Tensor) – Data samples from batch 1 of shape (n_samples_1, n_features)

  • X2 (torch.Tensor) – Data samples from batch 2 of shape (n_samples_2, n_features)

  • itemwise_dist (bool, default: True) –

    Compute the distance in an itemwise manner or pairwise manner.

    In the itemwise fashion (itemwise_dist=False), distance is computed between the ith data sample in X1 to the ith data sample in X2. Thus, the two data samples X1 and X2 should be of the same shape

    In the pairwise fashion (itemwise_dist=False), distance is computed between all the samples in X1 and all the samples in X2. In this case, the two data samples X1 and X2 can be of different shapes.

Returns:

dist – Distance between samples of batch 1 and batch 2.

If itemwise_dist=True, item-wise distance is returned of shape (n_samples, 1)

If itemwise_dist=False, pair-wise distance is returned of shape (n_samples_1, n_samples_2)

Return type:

torch.Tensor

to(device)[source]#

Moves distance metric to a particular device

Parameters:

device (torch.device) –


Sensitive Subspace distance#

class inFairness.distances.SensitiveSubspaceDistance[source]#

Implements Sensitive Subspace metric base class that accepts the basis vectors of a sensitive subspace, and computes a projection that ignores the sensitive subspace.

The projection from the sensitive subspace basis vectors (A) is computed as:

\[P^{'} = I - (A (A A^{T})^{-1} A^{T})\]
compute_projection_complement(basis_vectors)[source]#

Compute the projection complement of the space defined by the basis_vectors:

projection complement given basis vectors (A) is computed as:

\[P^{'} = I - (A (A A^{T})^{-1} A^{T})\]
Parameters:

basis_vectors (torch.Tensor) – Basis vectors of the sensitive subspace Dimension (d, k) where d is the data features dimension and k is the number of protected dimensions

Returns:

projection complement – Projection complement computed as described above. Shape (d, d) where d is the data feature dimension

Return type:

torch.Tensor

fit(basis_vectors)[source]#

Fit Sensitive Subspace Distance metric

Parameters:

basis_vectors (torch.Tensor) – Basis vectors of the sensitive subspace