在使用下面的文章和第三方库
Marco Cuturi, Sinkhorn Distances: Lightspeed Computation of Optimal Transport, NIPS 2013
https://pythonot.github.io/
计算Entropic Regularization的Wassertein loss时,总是出现如下问题:
Warning: numerical errors at iteration 0
导致计算出的Wassertein distance为0,从而影响网络的训练。
经过分析发现,Warning: numerical errors at iteration 0出现当且仅当矩阵K的转置中含有0元素,而根据文章:
理论上是不会有0元素的。因此原因其实很简单,就是γ太小了!例如当γ=0.001时
(Pdb) M array([[0.26596661, 0.23665913, 0.25129144, ..., 0.25782531, 0.25837383, 0.24954239], [0.24605081, 0.24002687, 0.23672875, ..., 0.24692256, 0.24100467, 0.23279034], [0.25954123, 0.24295745, 0.23761645, ..., 0.25519669, 0.24264093, 0.24277576], ..., [0.26867422, 0.26456161, 0.28494031, ..., 0.27320845, 0.28464691, 0.27690985], [0.31739641, 0.29082387, 0.28749557, ..., 0.30555643, 0.28721051, 0.30280537], [0.27102495, 0.24505942, 0.25931435, ..., 0.26164062, 0.26206772, 0.24817103]]) (Pdb) M.min() 0.21602636026751282 (Pdb) (M/reg).min() 216.0263602675128 (Pdb) (-M/reg).min() -1000.0 (Pdb) np.exp((-M/reg)).min() 0.0
将γ适度调大至0.1,即可解决Warning: numerical errors at iteration 0从而出现计算错误的问题。