How do you update the value of a 0-dim tensor in-place?
t = torch.tensor(0, device='cuda')
# Now we want to set the value to 8 without creating a new tensor...
t = 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?
Can a tensor which has a zero for the length of any dimension, contain values?
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.
Got it, I mistook what you said for a tensor which has zero as one of the dimensions.
The following seems to work for your problem:
t = torch.tensor(5)
Another option might be
t.data = tensor(8).
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?)?
You’re right. Updating
t.data also makes the corresponding updates to the
dtype. Sorry I couldn’t be of more help.
After more doc-searching I have settled for:
I guess this will have to do until there is an official intended way to update the value of a 0-dim tensor.