PyTorch torch.tensor function
PyTorch torch reference manual
torch.tensor is the core function in PyTorch used to create tensors. It creates a new tensor by copying the data and does not preserve the autograd history of the original data.
This is one of the most commonly used ways to create PyTorch tensors and suits most scenarios.
Function definition
torch.tensor(data, dtype=None, device=None, requires_grad=False, pin_memory=False)
Parameters:
data(any type): The tensor data, which can be a Python list, tuple, NumPy array, etc.dtype(torch.dtype, optional): Specifies the data type of the tensor, such astorch.float32,torch.int64, etc.device(torch.device, optional): Specifies the device on which the tensor is stored, such astorch.device('cpu')ortorch.device('cuda').requires_grad(bool, optional): Whether gradients need to be computed. Defaults toFalse.pin_memory(bool, optional): Whether to use pinned memory. Defaults toFalse.
Return Value:
torch.Tensor: Returns a PyTorch tensor.
Usage examples
The following are some uses of torch.tensor Examples of the function.
Example 1: Create a tensor from a list
Example
import
torch
Create a one-dimensional tensor from a Python list
data
=
[
1
,
2
,
3
,
4
,
5
]
x
=
torch.
tensor
(
data
)
(
x
)
(
x.
dtype
)
The output is:
tensor([1, 2, 3, 4, 5])
torch.int64
In this example, we create a one-dimensional tensor from a Python list. PyTorch automatically infers the data type as int64.
Example 2: Specify the data type
Example
import
torch
Create a tensor of type float32
x
=
torch.
tensor
(
[
1.0
,
2.0
,
3.0
]
,
dtype
=
torch.
float32
)
(
x
)
(
x.
dtype
)
The output is:
tensor([1., 2., 3.])
torch.float32
In this example, we explicitly specify the tensor's data type as float32.
Example 3: Create a tensor that requires gradients
Example
import
torch
Create a tensor that requires gradient computation
x
=
torch.
tensor
(
[
1.0
,
2.0
,
3.0
]
,
requires_grad
=
True
)
(
x.
requires_grad
)
The output is:
True
In this example, we create a tensor that requires gradient computation, which is very useful when training neural networks.
Example 4: Create a tensor from a 2D list
Example
import
torch
Create a 2D tensor (matrix) from a 2D list
data
=
[
[
1
,
2
,
3
]
,
[
4
,
5
,
6
]
]
x
=
torch.
tensor
(
data
)
(
x
)
(
x.
shape
)
The output is:
tensor([[1, 2, 3],
[4, 5, 6]])
torch.Size([2, 3])
In this example, we create a 2x3 two-dimensional tensor from a 2D list.
Example 5: Create a tensor on a CUDA device
Example
import
torch
Check whether CUDA is available
if
torch.
cuda
.
is_available
(
)
:
Create a tensor on a CUDA device
x
=
torch.
tensor
(
[
1
,
2
,
3
]
,
device
=
'cuda'
)
(
x.
device
)
else
:
(
"CUDA is not available"
)
The output is:
cuda:0
In this example, we check whether CUDA is available and then create a tensor on the GPU.
The difference between torch.tensor and torch.as_tensor
torch.tensor and torch.as_tensor Both are used to create tensors, but they have important differences:
torch.tensor: Always copies data; the created tensor does not share memory with the original data.torch.as_tensor: Shares data whenever possible, does not copy memory, and copies only when necessary.
If you need to preserve the autograd history or avoid unnecessary data copying, you can use torch.as_tensor.
Notes
torch.tensorcopies the data, so modifications to the returned tensor do not affect the original data.- If you need to create a new tensor with the same shape and device as another tensor, you can use
torch.zeros_like()ortorch.ones_like(). - When creating a tensor, if you do not specify
dtype, PyTorch automatically infers the data type.