torch_geometric.metrics.LinkPredDiversity

class LinkPredDiversity(k: int, category: Tensor)[source]

Bases: _LinkPredMetric

A link prediction metric to compute the Diversity @ \(k\) of predictions according to item categories.

Diversity is computed as

\[div_{u@k} = 1 - \left( \frac{1}{k \cdot (k-1)} \right) \sum_{i \neq j} sim(i, j)\]

where

\[\begin{split}sim(i,j) = \begin{cases} 1 & \quad \text{if } i,j \text{ share category,}\\ 0 & \quad \text{otherwise.} \end{cases}\end{split}\]

which measures the pair-wise inequality of recommendations according to item categories.

Parameters:
  • k (int) – The number of top-\(k\) predictions to evaluate against.

  • category (torch.Tensor) – A vector that assigns each destination node to a specific category.

update(pred_index_mat: Tensor, edge_label_index: Union[Tensor, Tuple[Tensor, Tensor]], edge_label_weight: Optional[Tensor] = None) None[source]

Updates the state variables based on the current mini-batch prediction.

update() can be repeated multiple times to accumulate the results of successive predictions, e.g., inside a mini-batch training or evaluation loop.

Parameters:
  • pred_index_mat (torch.Tensor) – The top-\(k\) predictions of every example in the mini-batch with shape [batch_size, k].

  • edge_label_index (torch.Tensor) – The ground-truth indices for every example in the mini-batch, given in COO format of shape [2, num_ground_truth_indices].

  • edge_label_weight (torch.Tensor, optional) – The weight of the ground-truth indices for every example in the mini-batch of shape [num_ground_truth_indices]. If given, needs to be a vector of positive values. Required for weighted metrics, ignored otherwise. (default: None)

Return type:

None

compute() Tensor[source]

Computes the final metric value.

Return type:

Tensor

reset() None

Resets metric state variables to their default value.

Return type:

None