PyTorch torch.zeros function
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:
*size(int): The shape of the tensor, for example3,(3, 4),(2, 3, 4), etc.dtype(torch.dtype, optional): Specifies the tensor's data type, defaulting totorch.float32.device(torch.device, optional): Specifies the device on which the tensor is stored.requires_grad(bool, optional): Whether gradients need to be computed.pin_memory(bool, optional): Whether to use pinned memory.
Return Value:
torch.Tensor: Returns an all-zero tensor.
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
)
(
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
)
(
x
)
(
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
)
(
x
)
(
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'
)
(
x.
device
)
else
:
(
"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:
- Initialize the bias vector: In neural networks, biases are usually initialized to zero.
- Create a placeholder: Serves as an input placeholder in a dynamic computation graph.
- fill the array: Create an array initialized to zero, then fill it with data later.
- Mathematical operations: In cumulative operations that require zero as the starting value.
Notes
- By default,
torch.zerosWhat is created isfloat32type 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.