Dice
Boolean contingency counts; both vectors empty gives NaN. For Boolean formulas a=c11, b=c10, c=c01, d=c00 and t=a+b+c+d. Optional w replaces counts by weight sums.
Shapes and Modes
| Input | Layout |
|---|---|
| One sample | [D] |
| Collection X | [...,N,D] |
| Collection Y | [...,M,D] |
| Mode | Selector | Output |
|---|---|---|
| all-pairs | compare(X,Y,metric=...) | [...,N,M] |
| paired | compare(X,Y,metric=...,mode="paired") | [...,N]; equal sample counts |
| condensed | compare(X,metric=...,mode="condensed") | [...,N*(N-1)//2]; strict upper triangle |
| single | compare(x,y,metric=...,mode="single") | [...]; leading batches only |
Leading batch dimensions broadcast. See mode and shape conventions.
Entry Points
| Named Python entry | Function |
|---|---|
| All-pairs | dice_distances |
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: dice.
Accepted metric options: w. 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.
import torch
import torchpairwise as tp
X = torch.tensor([[True, False], [True, True]]) # [2,2]
Y = torch.tensor([[True, True], [False, True], [True, False]])
metric = "dice_distances"
all_pairs = tp.compare(X, Y, metric=metric) # [2,3]
paired_Y = Y[:2]
paired = tp.compare(X, paired_Y, metric=metric, mode="paired") # [2]
condensed = tp.compare(X, metric=metric, mode="condensed") # [1]
x, y = X[0], Y[0]
single = tp.compare(x, y, metric=metric, mode="single") # scalar
#include <torchpairwise.h>
namespace tp = torchpairwise::ops;
auto X = at::tensor({true, false, true, true}, at::kBool).reshape({2, 2});
auto Y = at::tensor({true, true, false, true, true, false}, at::kBool).reshape({3, 2});
const std::string metric = "dice_distances";
tp::ComparisonOptions options;
auto all_pairs = tp::compare(X, Y, metric, "all_pairs", options); // [2,3]
auto paired_Y = Y.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);
auto x = X[0], y = Y[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
Boolean/equality inputs do not have derivatives; feature weights may have derivatives for finite weighted formulas. See the detailed conventions and precision and deployment.
Related Implementations
Related APIs can differ in reduction, normalization, units or boundary behavior; match these before comparing values.