注意
跳转到末尾 以下载完整示例代码。
2D分布上的切片Wasserstein距离
这个例子说明了按[31]中提出的方式计算切片Wasserstein距离。
[31] Bonneel, Nicolas 等. “测度的切片和拉东瓦瑟斯坦重心.” 数学成像与视觉期刊 51.1 (2015): 22-45
# Author: Adrien Corenflos <adrien.corenflos@aalto.fi>
#
# License: MIT License
# sphinx_gallery_thumbnail_number = 2
import matplotlib.pylab as pl
import numpy as np
import ot
生成数据
n = 200 # nb samples
mu_s = np.array([0, 0])
cov_s = np.array([[1, 0], [0, 1]])
mu_t = np.array([4, 4])
cov_t = np.array([[1, -0.8], [-0.8, 1]])
xs = ot.datasets.make_2D_samples_gauss(n, mu_s, cov_s)
xt = ot.datasets.make_2D_samples_gauss(n, mu_t, cov_t)
a, b = np.ones((n,)) / n, np.ones((n,)) / n # uniform distribution on samples
绘制数据

Text(0.5, 1.0, 'Source and target distributions')
不同种子和投影数量的切片瓦瑟斯坦距离
n_seed = 20
n_projections_arr = np.logspace(0, 3, 10, dtype=int)
res = np.empty((n_seed, 10))
绘制切片的瓦瑟斯坦距离
pl.figure(2)
pl.plot(n_projections_arr, res_mean, label="SWD")
pl.fill_between(
n_projections_arr, res_mean - 2 * res_std, res_mean + 2 * res_std, alpha=0.5
)
pl.legend()
pl.xscale("log")
pl.xlabel("Number of projections")
pl.ylabel("Distance")
pl.title("Sliced Wasserstein Distance with 95% confidence interval")
pl.show()

脚本的总运行时间: (0分钟 2.260秒)