Question

How to assign value to a zero dimensional torch tensor?

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?

 3  74  3
1 Jan 1970

Solution

 1

You can do an in-place operation e.g. multiplication:

z *= 5

You can also directly assign the value to the underlying data property of the tensor:

z.data = torch.tensor(5)

Note that, autograd (Pytorch's backpropagation engine) would not track any computation made directly on a tensor's data attribute.

2024-07-06
heemayl

Solution

 1

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.

2024-07-08
simon