jeanfeydy / geomloss

Geometric loss functions between point clouds, images and volumes
MIT License
599 stars 60 forks source link

Inconsistency with OT with Non Uniforme Weights ? What are we doing wrong ? #45

Open PierreColombo opened 3 years ago

PierreColombo commented 3 years ago

Hello, Thanks a lot for your great librairy ! We are trying to compute the transportation cost with Geomloss and we obtain inconsitent results when compared with ot. We want to use non uniforme weight with a custom cost function. Maybe you could help us to spot the error ?

OT

import numpy as np 
import ot.gpu as otgpu
size = 10000
weight = np.random.rand(size)
D_1 = weight / np.linalg.norm(weight, 1)
M = np.random.rand(size,size)
X =otgpu.sinkhorn(D_1,D_2,M_cupy, reg=0.05)
transportation_cost = np.sum(np.multiply(X,M))

We have transportation_cost=0.05004567861659549

Geomloss

def cost_function_custom(X,Y): 
    return torch.tensor(M).unsqueeze(0).cuda()

w_x = torch.tensor(D_1).cuda().clone() 
w_y = torch.tensor(D_2).cuda().clone() 
loss = SamplesLoss(loss="sinkhorn", p=2, blur=.05,backend="tensorized",cost=cost_function_custom,debias =False,scaling = 0.9999)
loss = loss(w_x,w_x.unsqueeze(1),w_y,w_y.unsqueeze(1))

We obtain loss=tensor(0.0150, device='cuda:0', dtype=torch.float64)

Are we missing something ?

Thanks a lot for your time. Cheers

AdrienCorenflos commented 3 years ago

Hey, I had the same issue when playing with the library for early tests of one of my papers. The regularisation param does not have the same meaning in pot and geomloss (reg = blur^p).

Hope that helps

Adrien