Fixed: IndexError: Dimension out of range (expected to be in range of [-1, 0], but got 1) in cosine_similarity
I compared two flat vectors with PyTorch's cosine_similarity, and it stopped with IndexError: Dimension out of range (expected to be in range of [-1, 0], but got 1). The function compares along dimension 1 unless told otherwise, and a flat vector has only dimension 0. Passing dim=0 fixed it.
What I Was Doing
I was writing a guide to cosine_similarity in PyTorch 2.15.0, on Python 3.15 on an Apple M1 Pro, and compared two flat vectors of two numbers each, the simplest way to write them:
import torch
from torch.nn.functional import cosine_similarity
a = torch.tensor([1.0, 0.0])
b = torch.tensor([1.0, 1.0])
print(cosine_similarity(a, b))
uv run python cosine_1d.py
What I Expected
The cosine similarity of the two vectors, about 0.7071: they're 45 degrees apart.
What I Got
Traceback (most recent call last):
File "/Users/you/source/pytorch-cosine/cosine_1d.py", line 6, in <module>
print(cosine_similarity(a, b))
~~~~~~~~~~~~~~~~~^^^^^^
IndexError: Dimension out of range (expected to be in range of [-1, 0], but got 1)
![The terminal after uv run python cosine_1d.py: a traceback pointing at cosine_similarity(a, b) on line 6 and ending in IndexError: Dimension out of range (expected to be in range of [-1, 0], but got 1).](https://strdewiridedevblogsprod.blob.core.windows.net/images/blog/ai/pytorch/fixes/2026/pytorch-cosine-1d-01-error.webp)
What I Checked
Which dimension the function uses. The cosine_similarity reference shows its full form, cosine_similarity(x1, x2, dim=1, eps=1e-8), and describes dim as the "Dimension along which cosine similarity is computed. Default: 1".
Which dimensions my vectors have. torch.tensor([1.0, 0.0]) has the shape [2]: one dimension, number 0. Its last dimension can also be called -1, which is why the error says the allowed range is [-1, 0]. The default dim=1 asks for a second dimension the vectors don't have.
The default suits tables of vectors, one vector per row: for a shape of [rows, numbers], dimension 1 holds each vector's numbers.
Why It Happens
cosine_similarity assumes its inputs are tables with one vector per row, and compares along dim=1. Two flat vectors have only dimension 0, so dim=1 is out of range.
The Fix
Tell the function to compare along dimension 0:
import torch
from torch.nn.functional import cosine_similarity
a = torch.tensor([1.0, 0.0])
b = torch.tensor([1.0, 1.0])
print(cosine_similarity(a, b, dim=0))

After the Fix
uv run python cosine_1d_fixed.py
tensor(0.7071)

0.7071, the cosine of 45 degrees, as a tensor with a single number. The commands work unchanged in PowerShell.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
Passing dim=0 to tables of vectors | The function compares the columns instead of the rows: for two tables of two rows, the rows gave [0.7071, 1.0000], the columns [1.0000, 0.7071] | Keep the default dim=1 for one vector per row |
| Making the vectors into rows without changing the code that reads the result | The result becomes tensor([0.7071]), with the shape [1], instead of tensor(0.7071) | Read the number with .item() |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
Dimension out of range (expected to be in range of [-2, 1], but got 2) | Tables of vectors, which have dimensions 0 and 1, with dim=2 | Use dim=1, the dimension that holds each vector's numbers |
RuntimeError: expected common dtype to be floating point, yet common dtype is Long | Both vectors hold whole numbers | Fixed: RuntimeError: expected common dtype to be floating point, yet common dtype is Long in cosine_similarity |
FAQ
Is dim=-1 the same as dim=0 here?
For flat vectors, yes: -1 means the last dimension, which is dimension 0 when there's only one. cosine_similarity(a, b, dim=-1) printed tensor(0.7071) too. For tables of vectors, -1 is dimension 1: on two tables of two rows, dim=-1 and the default both gave tensor([0.7071, 1.0000]). So dim=-1 works for both shapes.
Can I make the default work instead?
Yes: give each vector a row of its own. a.unsqueeze(0) turns the shape [2] into [1, 2], and cosine_similarity(a.unsqueeze(0), b.unsqueeze(0)) printed tensor([0.7071]).
Conclusion
IndexError: Dimension out of range (expected to be in range of [-1, 0], but got 1) came from cosine_similarity's default dim=1 on two flat vectors, which have only dimension 0. cosine_similarity(a, b, dim=0) returned tensor(0.7071).
Additional Resources
- torch.nn.functional.cosine_similarity —
dim, its default and how the result's shape is made. - torch.unsqueeze — adding a dimension of size one.
