You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have 2 tensors x and y of shape (B, N, 3) where B is the batch size, and N is fixed. Each value x[..., i] is a probability distribution of length N. I want to use a fixed cost matrix C where each value C[i, j] defines the cost of moving mass from index i in one distribution to index j in another, for all computations in the batch. Therefore C has shape (N, N).
Can I use SampleLoss to compute the sinkhorn distances between the two batches? I initially thought you could using the 'cost' init arg by setting this to a function that expands the fixed cost matrix to match the batch size, but this returns the same 0 value for every sample in the batch:
fromgeomlossimportSamplesLossimporttorch# Compute example cost matrix# I want to be able to use arbitrary C hereN=30points=torch.rand(N, 2)
C=torch.cdist(points, points, p=2).pow(2)
defcustom_cost(x, y):
# x and y are the input batches with shapes (B, N, 3) batch_size, _, _=x.shapereturnC.unsqueeze(0).expand(batch_size, -1, -1)
defgenerate_probability_distributions(batch_size, num_points, dim):
# Generate random valuesrandom_tensor=torch.rand(batch_size, num_points, dim)
# Normalize along the last dimension to make them probability distributionsprobability_tensor=random_tensor/random_tensor.sum(dim=1, keepdim=True)
returnprobability_tensor# Generate example tensors x and yx=generate_probability_distributions(32, 30, 3)
y=generate_probability_distributions(32, 30, 3)
# Define losscriterion=SamplesLoss('sinkhorn', blur=0.05, scaling=0.9, cost=custom_cost, backend="tensorized")
loss=criterion(x, y)
print(loss)
Any idea what I'm doing wrong? Any help would be much appreciated. Thanks.
The text was updated successfully, but these errors were encountered:
ldswaby
changed the title
Can I compute the sinkhorn distance over 2 (1 pred 1 true) equally sized batches of p distributions using a fixed cost matrix defined elsewhere?
Can I compute the sinkhorn distance between 2 batches of p distributions using a fixed cost matrix defined elsewhere?
Jun 1, 2024
Hello, thanks a lot for all your work!
I have 2 tensors x and y of shape (B, N, 3) where B is the batch size, and N is fixed. Each value x[..., i] is a probability distribution of length N. I want to use a fixed cost matrix C where each value C[i, j] defines the cost of moving mass from index i in one distribution to index j in another, for all computations in the batch. Therefore C has shape (N, N).
Can I use SampleLoss to compute the sinkhorn distances between the two batches? I initially thought you could using the 'cost' init arg by setting this to a function that expands the fixed cost matrix to match the batch size, but this returns the same 0 value for every sample in the batch:
Output:
Any idea what I'm doing wrong? Any help would be much appreciated. Thanks.
The text was updated successfully, but these errors were encountered: