optimal_transport#
- bayesflow.utils.optimal_transport(x1: Tensor, x2: Tensor, conditions: Tensor | None = None, method='sinkhorn', return_assignments=False, **kwargs) tuple[Tensor, Tensor, Tensor | None, Tensor] | tuple[Tensor, Tensor, Tensor | None][source]#
Match elements from
x2ontox1by minimizing the transport cost.This function dispatches to a specific optimal transport method according to the selected
methodand cost formulation. Depending on the method used, elements in either tensor may be permuted, dropped, duplicated, or otherwise modified in order to achieve an optimal assignment.- Parameters:
- x1Tensor
Tensor of shape
(n, ...)containing samples from the first distribution.- x2Tensor
Tensor of shape
(m, ...)containing samples from the second distribution.- conditionsTensor, optional
Tensor of shape
(k, ...)providing conditioning information for conditional optimal transport. IfNone, unconditional optimal transport is performed. Default isNone.- methodstr, optional
Method used to compute the optimal transport plan (e.g.,
'sinkhorn'). Default is'sinkhorn'.- return_assignmentsbool
If
True, also return the assignment indices produced by the transport method. Default isFalse.- **kwargs
Additional keyword arguments passed to the selected optimal transport method.
- Returns:
- Tuple of tensors
If
return_assignmentsisFalse, returns three tensors of shapes(n, ...)and(m, ...)corresponding tox1,x2,conditionsreordered according to the optimal transport solution. Ifreturn_assignmentsisTrue, the reordered tensors and the corresponding assignment indices are returned as a fourth element.