z = torch.tensor(1, dtype= torch.int64)
z[:] = 5
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: slice() cannot be applied to a 0-dim tensor.
I'm trying to assign a value to a torch tensor but because it has zero dimensions the slice operator doesn't work. How do I assign a new value then?
You can index with the empty index (i.e. an empty tuple ()
) into a 0-d tensor:
import torch
z = torch.tensor(1, dtype=torch.int64)
z[()] = 5
print(z)
# >>> tensor(5)
The Numpy documentation¹ states: An empty (tuple) index is a full scalar index into a zero-dimensional array – which is exactly what you need in this case.
Likewise, you can use an ellipsis ...
:
z[...] = 42
print(z)
# >>> tensor(42)
Slicing with the colon :
requires at least one dimension being present in the tensor. Neither the empty index ()
nor ellipsis ...
have this requirement.
¹) I did not find a corresponding statement in the PyTorch docs, but apparently PyTorch follows the same paradigm.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With