Pytorch tensor.data and tensor.item() can get the value of a tensor. In this tutorial, we will introduce the difference between them.
tensor.data
tensor.data can get a copy of the original tensor, here is the detail.
Understand PyTorch tensor.data with Examples – PyTorch Tutorial
tensor.item()
It will return a real value of an element in a tensor. We will use an example to show the difference between them.
For example:
import torch x = torch.tensor([[1.0, 2.0],[2.0, 3.0]], requires_grad = True) print(x) print(type(x)) print(x[0][1]) z = x[0][1].item() print(z) print(type(z))
Run this code, we will see:
tensor([[1., 2.], [2., 3.]], requires_grad=True) <class 'torch.Tensor'> tensor(2., grad_fn=) 2.0 <class 'float'>
From this result, we can find:
tensor.item() can be applied on an element, it will return a python type, such as float.
We also can use index to get an element value , for example x[0][1], however, the returned type is tensor.
Look at example below:
z = x[0].item() print(z) print(type(z))
Run this code, we will see this error:
ValueError: only one element tensors can be converted to Python scalars
Here we use x[0] to get an item. However, x[0] contains two elements. It also means we only can use tensor.item() on a tensor that only cotains an element.