<jupyter_start><jupyter_text>Low-Rank Sinkhorn We experiment with the low-rank (LR) Sinkhorn solver, proposed by [Scetbon et. al](http://proceedings.mlr.press/v139/scetbon21a/scetbon21a.pdf) as an alternative to the Sinkhorn algorithm. The idea of that solver is to compute optimal transport couplings that are low-rank, by design. Rather than look for a $n\times m$ matrix $P_\varepsilon$ that has a factorization $D(u)\exp(-C/\varepsilon)D(v)$ (as computed by the Sinkhorn algorithm) when solving a problem with cost $C$, the set of feasible plans is restricted to those adopting a factorization of the form $P_r = Q D(1/g) R^T$, where $Q$ is $n\times r$, $R$ is $r \times m$ are two thin matrices, and $g$ is a $r$-dimensional probability vector.<jupyter_code>import jax.numpy as jnp
import jax
import matplotlib.pyplot as plt
plt.rcParams.update({'font.size': 18})
import ott

def create_points(rng, n, m, d):
  rngs = jax.random.split(rng, 4)
  x = jax.random.normal(rngs[0], (n,d)) + 1
  y = jax.random.uniform(rngs[1], (m,d))
  a = jax.random.uniform(rngs[2], (n,))
  b = jax.random.uniform(rngs[3], (m,))
  a = a / jnp.sum(a)
  b = b / jnp.sum(b)
  return x, y, a, b<jupyter_output><empty_output><jupyter_text>Create an OT problem comparing two point clouds<jupyter_code>rng = jax.random.PRNGKey(0)
n, m, d = 19, 35, 2
x, y, a, b = create_points(rng, n=n, m=m, d=d)

geom = ott.geometry.pointcloud.PointCloud(x, y, epsilon=0.1)
ot_prob = ott.core.problems.LinearProblem(geom, a, b)<jupyter_output><empty_output><jupyter_text>Solve it with Sinkhorn and plot plan/map<jupyter_code>solver = ott.core.sinkhorn.Sinkhorn()
ot_sink = solver(ot_prob)

transp_cost = jnp.sum(ot_sink.matrix * geom.cost_matrix)
plt.imshow(ot_sink.matrix, cmap='Purples')
plt.title('Sinkhorn, Cost: ' + str(transp_cost))
plt.colorbar()
plt.show()
plott = ott.tools.plot.Plot()
_ = plott(ot_sink)<jupyter_output><empty_output><jupyter_text>Experimentations with the Low-Rank approachSolve that problem using the Low-Rank Sinkhorn solver, with a rank parameterized to be equal to the half of $r=\min(n,m)/2$<jupyter_code>solver = ott.core.sinkhorn_lr.LRSinkhorn(rank=int(min(n,m)/2))
ot_lr = solver(ot_prob)

transp_cost = ot_lr.compute_reg_ot_cost(ot_prob)
plt.imshow(ot_lr.matrix, cmap='Purples')
plt.colorbar()
plt.title('LR, Cost: ' + str(transp_cost))
plt.show()
plott = ott.tools.plot.Plot()
_ = plott(ot_lr)<jupyter_output><empty_output><jupyter_text>Play with larger scalesOne of the interesting features of the low-rank approach lies in its ability to scale, since its iterations are of complexity $O( (n+m) r)$ rather than $O(nm)$. We consider this by sampling two points clouds of size 1 million in $d=7$.<jupyter_code>n, m, d =10^6, 10^6+1, 7
x, y, a, b = create_points(rng, n=n, m=m, d=d)<jupyter_output><empty_output><jupyter_text>We compute plans satisfy a rank constraint $r$, for various values of $r$,<jupyter_code>geom = ott.geometry.pointcloud.PointCloud(x, y, epsilon=0.1)
ot_prob = ott.core.problems.LinearProblem(geom, a, b)
costs = []
ranks = [1, 5, 10, 15, 20, 35, 50, 100, 500, 1000]
for rank in ranks:
  solver = ott.core.sinkhorn_lr.LRSinkhorn(rank=rank)
  ot_lr = solver(ot_prob)
  costs.append(ot_lr.compute_reg_ot_cost(ot_prob))<jupyter_output><empty_output><jupyter_text>As expected, the optimal cost decreases with rank, as shown in the plot below. Recall that, because of the non-convexity of the original problem, there may be small bumps along the way. For these two fairly concentrated distributions, it seems possible to produce plans that have relatively small rank yet low cost.<jupyter_code>plt.plot(ranks, costs)
plt.xscale('log')
plt.xlabel('rank')
plt.ylabel('cost')
plt.title('Transport cost as a function of rank')
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13125165387668694, 'fraction_numerical': 0.01587721619476052, 'mean_word_length': 5.5625, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '426529', 'license': 'Apache-2.0', 'n_tokens_mistral': 1497, 'n_tokens_neox': 1418, 'n_words': 399, 'path': 'docs/notebooks/LRSinkhorn.ipynb', 'repo_name': 'theouscidda6/ott', 'size': '3779'}