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

PyTorch torch.zeros function

PyTorch torch.zeros function


PyTorch torch reference manual PyTorch torch reference manual

torch.zeros A function in PyTorch for creating an all-zero tensor. It creates a tensor of a given shape with every element initialized to 0.

This is one of the most common ways to initialize tensors in deep learning, often used to create bias vectors, placeholders, or to initialize model parameters.

Function definition

torch.zeros(*size, dtype=None, device=None, requires_grad=False, pin_memory=False)

Parameters:

Return Value:


Usage examples

The following are some uses of torch.zeros Examples of the function.

Example 1: Create a 1D all-zero tensor

Example

import

torch

Create an all-zero tensor with 5 elements

x

=

torch.

zeros

(

5

)

print

(

x

)

The output is:

tensor([0., 0., 0., 0., 0.])

In this example, we create a 1D all-zero tensor with 5 elements.

Example 2: Create a 2D all-zero tensor

Example

import

torch

Create a 3x4 all-zero tensor (matrix)

x

=

torch.

zeros

(

3

,

4

)

print

(

x

)

print

(

x.

shape

)

The output is:

tensor([[0., 0., 0., 0.],

        [0., 0., 0., 0.],

        [0., 0., 0., 0.]])

torch.Size([3, 4])

In this example, we create a 2D all-zero tensor with 3 rows and 4 columns.

Example 3: Create an all-zero tensor with an integer dtype

Example

import

torch

Create an all-zero tensor with an integer dtype

x

=

torch.

zeros

(

3

,

3

,

dtype

=

torch.

int32

)

print

(

x

)

print

(

x.

dtype

)

The output is:

tensor([[0, 0, 0],

        [0, 0, 0],

        [0, 0, 0]], dtype=torch.int32)

在这个示例中,我们创建了整数类型的全零张量,默认的 float32 被覆盖为 int32。

示例 4: 创建需要梯度的全零张量

实例

import torch

# 创建需要梯度的全零张量
x = torch.zeros(3, requires_grad=True)

print(x.requires_grad)

输出结果为:


True

In this example, we create a tensor of zeros that requires gradient computation, which is used for learnable parameters when training neural networks.

Example 5: Create a tensor of zeros on a CUDA device

Example

import

torch

Check whether CUDA is available

if

torch.

cuda

.

is_available

(

)

:

   

Create an all-zero tensor on a CUDA device

    x

=

torch.

zeros

(

3

,

4

,

device

=

'cuda'

)

   

print

(

x.

device

)

else

:

   

print

(

"CUDA not available"

)

The output is:

cuda:0

In this example, we check whether CUDA is available, and then create an all-zero tensor on the GPU.


The difference between torch.zeros and torch.zeros_like

  • torch.zeros(*size): Creates a tensor of zeros according to the specified size.
  • torch.zeros_like(input): Creates an all-zero tensor according to the shape of the input tensor, preserving the input tensor's dtype and device.

Use Cases

torch.zeros The function is typically used in the following scenarios:

  1. Initialize the bias vector: In neural networks, biases are usually initialized to zero.
  2. Create a placeholder: Serves as an input placeholder in a dynamic computation graph.
  3. fill the array: Create an array initialized to zero, then fill it with data later.
  4. Mathematical operations: In cumulative operations that require zero as the starting value.

Notes

  • By default,torch.zeros What is created is float32 type of tensor.
  • If you need to create an all-zero tensor with the same shape as another tensor, you can use torch.zeros_like().
  • Tensors of zeros are often used for initialization in deep learning, but in some cases may lead to the vanishing gradient problem.

PyTorch torch reference manual Pytorch torch Reference Manual