Skip to content

torch_based_adjuster

Documentation for TorchBasedAdjuster

Functionality

TorchBasedAdjuster adjusts vectors using a Torch-based optimizer. It computes similarities between query and item embeddings using various metrics and aggregates them based on the provided index information. The class facilitates the adjustment of vector representations through gradient-based optimization to enhance search relevance.

Parameters

  • search_index_info: Contains metric type and aggregation method.
  • adjustment_rate: Step size for the optimizer (default 0.1).
  • num_iterations: Number of iterations for adjustment (default 10).
  • softmin_temperature: Temperature for the soft minimum computation (default 1.0).

Usage

  • Purpose: To refine embedding vectors for improved representation based on a given similarity metric.
  • Motivation: Utilize Torch GPU acceleration for metric calculations and fine-tuning.
  • Inheritance: Inherits from VectorsAdjuster to standardize the vector adjustment interface.

Example

After creating an instance of TorchBasedAdjuster, compute similarities by calling compute_similarity with appropriate tensors.


Documentation for torch_based_adjuster.compute_similarity

Functionality

Computes similarity between query and item vectors. Uses the metric configured in search_index_info to calculate similarity. For MetricType.COSINE, it applies normalized dot product; for DOT, it computes a direct dot product; for EUCLID, it calculates the negative Euclidean distance. The method aggregates values using a differentiable softmin (when aggregation type is MIN) or the simple average (when aggregation type is AVG).

Parameters

  • queries: Tensor of shape [B, N1, D] where B is batch size, N1 is number of queries, and D is embedding dimension.
  • items: Tensor of shape [B, N2, M, D] where B is batch size, N2 is number of items, M is number of vectors per item, and D is embedding dimension.
  • softmin_temperature: Temperature parameter for the softmin calculation (default is 1.0).

Usage

  • Purpose: Compute similarity between queries and items using the defined metric and aggregation strategy.

Example

A simple usage example:

adjuster = TorchBasedAdjuster(search_index_info)
similarities = adjuster.compute_similarity(queries, items)

Documentation for TorchBasedAdjuster.adjust_vectors

Functionality

Adjusts vectors through gradient-based optimization to enhance search relevance. This method updates clicked and non-clicked vectors by maximizing similarity for clicked elements and reducing it for non-clicked ones. A cubic loss function emphasizes strong differences.

Parameters

  • data_for_improvement: List of ImprovementInput objects that include query vectors and corresponding clicked and non-clicked element vectors.

Usage

  • Purpose: Optimize vector representations for improved search outcomes.

Example

# Example usage:
adjuster = TorchBasedAdjuster(search_index_info, adjustment_rate=0.1)
updated_data = adjuster.adjust_vectors(data_for_improvement)