Suppose I have 1 x 1 torch variable and I want to convert it into a float, is there a better way than doing this?
x.data.numpy()[0, 0]
(Couldn’t find it in docs).
You don’t need the .numpy()
part.
1 Like
Regarding Variables, note that you cannot convert one directly to a numpy array, nor one of its coefficients to a scalar type. Since Variables only “wrap” around Tensors, to keep the autograd semantic working, if x is a Variable, x[i] is a 1d Variable of size 1.
To access the values of [the Tensor of] a Variable, you have to do it through its .data attribute, which is a Tensor.
1 Like