Comparisons and
Streamed Operators
Distances, divergences, kernels and scores for tensor samples. One comparison API, four pairing modes, shared native execution in Python and C++.
All-pairs, paired, condensed and single. Leading batch axes broadcast.
02 / TILED EXECUTIONStream a ResultReductions, top-k, statistics and matrix products without retaining a full generated matrix.
03 / METRIC REFERENCEStart with the EquationFind a comparison by formula, sample layout and supported modes.
A Comparison in Either Language
The top-bar Python / C++ switch changes paired examples throughout this guide. Use the same metric name and mode in both languages.
import torch
import torchpairwise as tp
X = torch.tensor([[0., 0.], [3., 4.]]) # [N=2,D=2]
Y = torch.tensor([[0., 4.], [3., 0.]]) # [M=2,D=2]
scores = tp.compare(X, Y, metric="sqeuclidean") # [2,2]
#include <torchpairwise.h>
namespace tp = torchpairwise::ops;
auto X = at::tensor({0.f, 0.f, 3.f, 4.f}).reshape({2, 2});
auto Y = at::tensor({0.f, 4.f, 3.f, 0.f}).reshape({2, 2});
auto scores = tp::compare(X, Y, "sqeuclidean"); // [2,2]
Result: [[16, 9], [9, 16]].
Start with the quick start, or browse the comparison catalog. Both numeric and nonnumeric samples use ordinary dense tensors; structured distributions group their component tensors.
Explore the example gallery for kernel ridge regression, truncated kernel PCA, kernel LDA, binary SVM, landmark regression, PageRank and kernel fitting with an intercept, graph diffusion and matrix-function actions. Each pairs a streamed algorithm with a small dense correctness reference.
Streamed sources also support pair masks, entropic transport over any lazy cost, Gaussian process prediction, user-defined metrics, encoded features with one or two encoders, and optional execution backends.
This guide documents TorchPairwise 1.0. See installation for matching your PyTorch build and migration when upgrading.
Python and native callers share built-in forward and first-backward algorithms. Metric-specific domains, autocast, compilation and boundary derivatives are part of each comparison's contract. A symmetric entry is not necessarily a triangle-inequality metric, and a score need not be a positive-semidefinite kernel.