Skip to content

max_aggregator

Documentation for MaxAggregator

Functionality

This class computes the maximum value from a list of rank scores. It is designed to aggregate ranks by selecting the highest value provided. Additionally, it includes a differentiable method to facilitate gradient-based training.

Motivation

In ranking tasks, the maximum score often indicates the best candidate. This class encapsulates the logic to find the maximum and offers a differentiable version to ensure gradient flow during model training.

Inheritance

MaxAggregator inherits from the base class RanksAggregator.

Parameters

  • if_empty_value: A fallback value returned when no ranks are provided.

Usage

Initialize the aggregator with an optional if_empty_value. Then, call its aggregate methods with a list or tensor of ranks.

Example

aggregator = MaxAggregator(if_empty_value=0.0)
result = aggregator._aggregate([0.2, 0.8, 0.5])  # returns 0.8

Documentation for MaxAggregator._aggregate

Functionality

Evaluates the maximum rank from a list or tensor of ranks. If the ranks container is empty, it returns a configurable default value.

Parameters

  • ranks: A list of floats or a torch.Tensor of ranks.

Usage

  • Purpose: Extracts the highest rank from the provided set, using a default when no ranks are present.

Example

aggregator = MaxAggregator(if_empty_value=0.0)
result = aggregator._aggregate(ranks)

Documentation for MaxAggregator._aggregate_differentiable

Functionality

Calculates the differentiable maximum value from a tensor of ranks. It uses differentiable_extreme to ensure gradient flow during training, making it suitable for backpropagation.

Parameters

  • ranks: A tensor of rank values. The function computes the differentiable maximum from these values.

Usage

  • Purpose: Compute the maximum rank in a differentiable way, preserving gradient computations during model training.

Example

import torch
from embedding_studio.embeddings.features.ranks_aggregators.max_aggregator import MaxAggregator

# Create an instance of MaxAggregator
aggregator = MaxAggregator(if_empty_value=0.0)
ranks = torch.tensor([0.2, 0.5, 0.3])
result = aggregator._aggregate_differentiable(ranks)
print(result)