RuntimeError: The size of tensor a (40) must match the size of tensor b (64) at non-singleton dimension 1

Hi all,

Am attempting to implement MMD from https://github.com/ZhaoZhibin/UDTL/blob/45c256f69c7f0bf06d8b9505834edf991d4484d4/loss/DAN.py
but have encountered the error of adding/subtracting tensors of different sizes.

This is the calling function :

distance_loss=DAN(features.narrow(0, 0, labels.size(0)),features.narrow(0, labels.size(0), inputs.size(0)-labels.size(0)))

The first argument is meant to extract features from source while the second argument refers to the features from the target source,

And this is the function (from the link) for the operations

def DAN(source, target, kernel_mul=2.0, kernel_num=5, fix_sigma=None):
batch_size = int(source.size()[0])
kernels = gaussian_kernel(source, target,
kernel_mul=kernel_mul, kernel_num=kernel_num, fix_sigma=fix_sigma)
XX = kernels[:batch_size, :batch_size]
YY = kernels[batch_size:, batch_size:]
XY = kernels[:batch_size, batch_size:]
YX = kernels[batch_size:, :batch_size]
loss = torch.mean(XX + YY - XY - YX)
return loss

Sizes:
‘features’ - [104,128,125] per batch
‘labels’ - [64] per batch

I believe the problem to the error code is in the loss statement where loss= XX+YY-XY-YX.

Appreciate any advice on this. Thanks

Could you check the shape of kernels and the value of batch_size?
If I’m not mistaken this code would only work, if the shapes are batch_size*2 as seen here:

# works
kernels = torch.randn(128, 128)
batch_size = 64

XX = kernels[:batch_size, :batch_size]
YY = kernels[batch_size:, batch_size:]
XY = kernels[:batch_size, batch_size:]
YX = kernels[batch_size:, :batch_size]
loss = torch.mean(XX + YY - XY - YX)

# fails
kernels = torch.randn(128, 127)
batch_size = 64

XX = kernels[:batch_size, :batch_size]
YY = kernels[batch_size:, batch_size:]
XY = kernels[:batch_size, batch_size:]
YX = kernels[batch_size:, :batch_size]
loss = torch.mean(XX + YY - XY - YX)