Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Gaussian mixtures

Gaussian-Mixture Cauchy-Schwarz

gaussian_mixture_cauchy_schwarz_divergences
all-pairspairedcondensedsingledistanceMixtures
\[ D_{\mathrm{CS}}(p,q)=-\log\frac{K(p,q)}{\sqrt{K(p,p)K(q,q)}} \]

Uses Gaussian density overlap K and SPD covariances. Natural logarithm, no factor of two; stable log-overlap evaluation.

Shapes and Modes

InputLayout
One sample(means[K,D], covariances[K,D,D], weights[K])
Collection X(means[...,N,K,D], covariances[...,N,K,D,D], weights[...,N,K])
Collection Y(means[...,M,L,D], covariances[...,M,L,D,D], weights[...,M,L])
ModeSelectorOutput
all-pairscompare(X,Y,metric=...)[...,N,M]
pairedcompare(X,Y,metric=...,mode="paired")[...,N]; equal sample counts
condensedcompare(X,metric=...,mode="condensed")[...,N*(N-1)//2]; strict upper triangle
singlecompare(x,y,metric=...,mode="single")[...]; leading batches only

Leading batch dimensions broadcast. See mode and shape conventions.

Entry Points

Named Python entryFunction
All-pairsgaussian_mixture_cauchy_schwarz_divergences
Pairedpaired_gaussian_mixture_cauchy_schwarz_divergences
Condensedcondensed_gaussian_mixture_cauchy_schwarz_divergences

The Python and C++ compare selectors cover every mode shown above. cdist / pairwise_distances and paired_distances select this comparison; pdist / condensed_distances select its condensed mode. single_comparison is shorthand for single mode.

Catalog aliases: gaussian_mixture_cauchy_schwarz.

Accepted metric options: x1_lengths, x2_lengths, chunk_size, component_chunk_size. Omitted options keep the metric defaults; unused options are rejected.

Example

Choose Python or C++ in the top bar; the setting applies across this guide.

Python
import torch
import torchpairwise as tp

X = (torch.rand(2, 2, 2), torch.eye(2).repeat(2, 2, 1, 1), torch.ones(2, 2))
Y = (torch.rand(3, 2, 2), torch.eye(2).repeat(3, 2, 1, 1), torch.ones(3, 2))
# (means [N,K,D], covariances [N,K,D,D], weights [N,K]); K=D=2

metric = "gaussian_mixture_cauchy_schwarz_divergences"
all_pairs = tp.compare(X, Y, metric=metric)  # [2,3]
paired_Y = tuple(t[:2] for t in Y)
paired = tp.compare(X, paired_Y, metric=metric, mode="paired")  # [2]
condensed = tp.compare(X, metric=metric, mode="condensed")      # [1]
x, y = tuple(t[0] for t in X), tuple(t[0] for t in Y)
single = tp.compare(x, y, metric=metric, mode="single")  # scalar
C++ / LibTorch
#include <torchpairwise.h>

namespace tp = torchpairwise::ops;
tp::ComparisonCollection X{at::rand({2, 2, 2}),
    at::eye(2).repeat({2, 2, 1, 1}), at::ones({2, 2})};
tp::ComparisonCollection Y{at::rand({3, 2, 2}),
    at::eye(2).repeat({3, 2, 1, 1}), at::ones({3, 2})};

const std::string metric = "gaussian_mixture_cauchy_schwarz_divergences";
tp::ComparisonOptions options;
auto all_pairs = tp::compare(X, Y, metric, "all_pairs", options);  // [2,3]
tp::ComparisonCollection paired_Y{Y[0].narrow(0, 0, 2), Y[1].narrow(0, 0, 2), Y[2].narrow(0, 0, 2)};
auto paired = tp::compare(X, paired_Y, metric, "paired", options);  // [2]
// [1]
auto condensed = tp::compare(X, c10::nullopt, metric, "condensed", options);
tp::ComparisonCollection x{X[0][0], X[1][0], X[2][0]};
tp::ComparisonCollection y{Y[0][0], Y[1][0], Y[2][0]};
auto single = tp::compare(x, y, metric, "single", options);  // scalar

Streamed Evaluation

tp.lazy(x, y, name) binds this distance for reductions, selection and products without retaining its full generated comparison matrix. Native source factories depend on the sample family; see streamed operators and C++ use.

Numerical Contract

Floating CPU/CUDA inputs use matching float32 / float64, with lower precision promoted under autocast where supported. First derivatives follow the metric’s smooth and boundary conventions. See the detailed conventions and precision and deployment.