Hi all,

I have two tensors with shape [B1, N] and [B2, N], N is the vector length.

I want to get a similarity metric with shape [B1, B2] with the current API.

Can I do that at once? I found that it always rises shape mismatching error.

Hi all,

I have two tensors with shape [B1, N] and [B2, N], N is the vector length.

I want to get a similarity metric with shape [B1, B2] with the current API.

Can I do that at once? I found that it always rises shape mismatching error.

YEs, you can by the way below.

Assume the shape of x1 and x2 is [B1, N] and [B2, N], then cosine similarity is computed below.

x1_norm = F.normalize(x1, p=2, dim=1)

x2_norm = F.normalize(x2, p=2, dim=1)

cosine similarity = x1_norm * x2_norm.t()

Yes, but you have to unsqueeze and swap shape, so the [B1, N] becomes [B1, N, 1] and [B2, N] becomes [1, N, B2]:

```
t1 = torch.randn((17,1024,1))
t2 = torch.randn((1,1024,512))
s1 = torch.nn.functional.cosine_similarity(t1,t2)
print(s1.shape)
```

gives

```
torch.Size([17, 512])
```