Skip to content




class Covariance(Metric):
 | def __init__(self) -> None

This Metric calculates the unbiased sample covariance between two tensors. Each element in the two tensors is assumed to be a different observation of the variable (i.e., the input tensors are implicitly flattened into vectors and the covariance is calculated between the vectors).

This implementation is mostly modeled after the streaming_covariance function in Tensorflow. See:

The following is copied from the Tensorflow documentation:

The algorithm used for this online computation is described in Specifically, the formula used to combine two sample comoments is C_AB = C_A + C_B + (E[x_A] - E[x_B]) * (E[y_A] - E[y_B]) * n_A * n_B / n_AB The comoment for a single batch of data is simply sum((x - E[x]) * (y - E[y])), optionally masked.


class Covariance(Metric):
 | ...
 | def __call__(
 |     self,
 |     predictions: torch.Tensor,
 |     gold_labels: torch.Tensor,
 |     mask: Optional[torch.BoolTensor] = None
 | )


  • predictions : torch.Tensor
    A tensor of predictions of shape (batch_size, ...).
  • gold_labels : torch.Tensor
    A tensor of the same shape as predictions.
  • mask : torch.BoolTensor, optional (default = None)
    A tensor of the same shape as predictions.


class Covariance(Metric):
 | ...
 | def get_metric(self, reset: bool = False) -> float


  • The accumulated covariance.


class Covariance(Metric):
 | ...
 | def reset(self)