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 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.