How do you update the value of a 0-dim tensor in-place?
import torch
t = torch.tensor(0, device='cuda')
# Now we want to set the value to 8 without creating a new tensor...
t[0] = 8 # <-- Bad: Fails with IndexError
t = 8 # <-- Bad: t is an int, not a Tensor, after this
What is the official/recommended way I’m supposed to achieve this?
Yes it can and is not uncommon, try the code out. 0-dim basically means it is a single scalar value and not a 1-dim list that currently only happens to contain a single value.
Note that 0-dim means that is has no dimensions, as opposed to one of the dimensions having a size of 0.
Yes, both those seem to be options. I was hoping/thinking there surely must be a way to do it though without having to create a new tensor in the process.
When creating that new tensor I would have to be careful about dtype and device (at least when assigning to t.data?)?