Debian Codegraph @

ot/bregman/_utils.py

raw symbols SCIP
1 # -*- coding: utf-8 -*-
2 """
3 Common tools of Bregman projections solvers for entropic regularized OT
4 """
5
6 # Author: Remi Flamary <[email protected]>
7 # Nicolas Courty <[email protected]>
8 #
9 # License: MIT License
10
11 from ..utils import list_to_array
12 from ..backend import get_backend
13
14
15 def geometricBar(weights, alldistribT):
16 """return the weighted geometric mean of distributions"""
17 weights, alldistribT = list_to_array(weights, alldistribT)
18 nx = get_backend(weights, alldistribT)
19 assert len(weights) == alldistribT.shape[1]
20 return nx.exp(nx.dot(nx.log(alldistribT), weights.T))
21
22
23 def geometricMean(alldistribT):
24 """return the geometric mean of distributions"""
25 alldistribT = list_to_array(alldistribT)
26 nx = get_backend(alldistribT)
27 return nx.exp(nx.mean(nx.log(alldistribT), axis=1))
28
29
30 def projR(gamma, p):
31 """return the KL projection on the row constraints"""
32 gamma, p = list_to_array(gamma, p)
33 nx = get_backend(gamma, p)
34 return (gamma.T * p / nx.maximum(nx.sum(gamma, axis=1), 1e-10)).T
35
36
37 def projC(gamma, q):
38 """return the KL projection on the column constraints"""
39 gamma, q = list_to_array(gamma, q)
40 nx = get_backend(gamma, q)
41 return gamma * q / nx.maximum(nx.sum(gamma, axis=0), 1e-10)