Cosine similarity computation. tensor size not matching?

Hi, I’m working on image retrieval problem. I computed query vectors and reference vectors and I’m trying to apply cosine similarity between these two vectors.
numpy computation is applyed with keras, so I’m trying to convert it to tensor. I tried with the annotation and the following error log comes up.
Any advice on this?

The error points to a size mismatch in dim0.
Could you print the shapes of query_vecs and reference_vecs and check, why the shapes are different?

torch.Size([32, 1024, 7, 7])

torch.Size([32, 1024, 7, 7])

torch.Size([32, 1024, 7, 7])

torch.Size([32, 1024, 7, 7])

torch.Size([32, 1024, 7, 7])

torch.Size([32, 1024, 7, 7])

torch.Size([20, 1024, 7, 7])

I printed out the shapes and I found that the last part of the batch size is different. how can I fix this problem? do I have to set batchsize that can be exactly divided by the dataset size? thanks for the advice?

Generally it shouldn’t be a problem, if your last batch is smaller, as the target should have the same shape.
However, while your data has a batch size of 20, the counterpart has 22 samples.
Do you have any idea how this might happen in your code?

A fast workaround would be to drop the last batch by setting drop_last=True in your DataLoader.

thanks for the reply. may I ask one last question? everything is working except torch.argsort.
Even thogh there is torch.argsort in the document, error says that there is no attribute ‘argsort’ in the module ‘torch’. what is wrong with this?

Could you check your PyTorch version? If I recall correctly, torch.argsort was introduced in 1.0.0.
I would recommend to update in case you are using an older version or you could use torch.sort instead and use the second returned value.

I ran the following command
print(torch.version) and I got 0.4.1
isn’t this the latest version of pytorch?

No, the current latest stable version is 1.0.0.
Have a look at the website to install the stable of nightly release.

1 Like

thank you so much for your help!!

1 Like