Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to assign value to a zero dimensional torch tensor?

Tags:

python

pytorch

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?

like image 682
Anonymous Avatar asked Sep 06 '25 19:09

Anonymous


1 Answers

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.

like image 119
simon Avatar answered Sep 11 '25 01:09

simon