PyTorch torch.tensor Function


Pytorch torch 参考手册PyTorch torch Reference Manual

torch.tensorIt is the core function in PyTorch for creating tensors. It creates a new tensor by copying data and does not preserve the automatic gradient history of the original data.

This is one of the most commonly used ways to create PyTorch tensors and is suitable for most scenarios.

Function Definition

torch.tensor(data, dtype=None, device=None, requires_grad=False, pin_memory=False)

Parameters:

  • data(any type): The data of the tensor, 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.int64etc.
  • 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 a gradient needs to be computed; defaults toFalse。
  • pin_memory(bool, optional): Whether to use pinned memory; defaults toFalse。

Return Value:

  • torch.TensorReturns a PyTorch tensor.

Usage Examples

The following are some examples of using thetorch.tensorfunction.

Example 1: Creating a Tensor from a List

Example

import torch

# Create a 1D tensor from a Python list
data = [1, 2, 3, 4, 5]
x = torch.tensor(data)

print(x)
print(x.dtype)

The output is:

tensor([1, 2, 3, 4, 5])
torch.int64

In this example, we created a 1D tensor from a Python list. PyTorch automatically infers the data type asint64。

Example 2: Specifying the Data Type

Example

import torch

# Create a tensor of type float32
x = torch.tensor([1.0, 2.0, 3.0], dtype=torch.float32)

print(x)
print(x.dtype)

The output is:

tensor([1., 2., 3.])
torch.float32

In this example, we explicitly specified the data type of the tensor asfloat32。

Example 3: Creating 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)

print(x.requires_grad)

The output is:

True

In this example, we created a tensor that requires gradient computation, which is very useful when training neural networks.

Example 4: Creating 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)

print(x)
print(x.shape)

The output is:

tensor([[1, 2, 3],
        [4, 5, 6]])
torch.Size([2, 3])

In this example, we created a 2x3 2D tensor from a 2D list.

Example 5: Creating 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')
    print(x.device)
else:
    print("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.


Difference Between torch.tensor and torch.as_tensor

torch.tensorandtorch.as_tensorBoth are used to create tensors, but they have important differences:

  • torch.tensorAlways copies data; the created tensor does not share memory with the original data.
  • torch.as_tensorShares data as much as possible, does not copy memory, and only copies when necessary.

If you need to preserve the autograd history or avoid unnecessary data copying, you can usetorch.as_tensor。


Notes

  • torch.tensorIt copies the data, so modifications to the returned tensor will not affect the original data.
  • If you need to create a new tensor with the same shape and device as another tensor, you can usetorch.zeros_like()ortorch.ones_like()。
  • When creating a tensor, if you do not specifydtype, PyTorch will automatically infer the data type.

Pytorch torch 参考手册PyTorch torch Reference Manual

Other Extensions