Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

PyTorch torch.tensor function

PyTorch torch.tensor function


PyTorch torch reference manual 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:

Return Value:


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

)

print

(

x

)

print

(

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

)

print

(

x

)

print

(

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

)

print

(

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

)

print

(

x

)

print

(

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'

)

   

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.


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:

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


Notes


PyTorch torch reference manual PyTorch torch reference manual