torch.nn.functional.cosine_similarity fails with a size or dimension error.
Why this happens
F.cosine_similarity(x1, x2, dim=1) computes similarity along the axis given by dim, which defaults to 1. The two errors have different causes.
"The size of tensor a (384) must match the size of tensor b (768)" means the two embeddings have different lengths, which again usually means two different models.
"Dimension out of range (expected to be in range of [-1, 0], but got 1)" means you passed 1D tensors. A 1D tensor has only axis 0, so the default dim=1 doesn't exist.
Answer 1: Set dim correctly for your shapes
For two single vectors, compare along the last axis:
import torch.nn.functional as F
sim = F.cosine_similarity(a, b, dim=-1) # a, b shape (384,)
dim=-1 always means "the last axis", so it works for both 1D and 2D inputs.
