I am trying to get a history of values of x with ‘for’ statement.
For python floats, I get the expected list.
The result is surprising with tensors: all entries of the list are the same!
Please let me know why this happens with tensors. Thanks.
— code —
import torch
# when x is a python float
x_hist = []
x = 1.1
for i in range(3):
x -= 0.1
x_hist.append(x)
print(x_hist)
# out [1.0, 0.9, 0.8]
# when x is a tensor
x_hist = []
x = torch.tensor(1.1)
for i in range(3):
x -= 0.1
x_hist.append(x)
print(x_hist)
# out [tensor(0.8000), tensor(0.8000), tensor(0.8000)]
In the case of tensor, x is a reference to the tensor. When we append it to the list, the reference is appended (not the value).
Hence, when you modify x later in-place (x -= 0.1) and print the elements in the list later, they all print the same (latest) value of x.