PyTorch torch.zeros Function
Pytorch torch Reference Manual
torch.zerosIt is a function in PyTorch used to create all-zero tensors. It creates a tensor of the specified shape, with all elements 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 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, e.g.3、(3, 4)、(2, 3, 4)etc.dtype(torch.dtype, optional): Specifies the data type of the tensor, defaults totorch.float32。device(torch.device, optional): Specifies the device where the tensor is stored.requires_grad(bool, optional): Whether to compute gradients.pin_memory(bool, optional): Whether to use pinned memory.
Return Value:
torch.Tensor: Returns an all-zero tensor.
Usage Examples
The following are sometorch.zerosexamples of using the function.
Example 1: Creating a 1D All-Zero Tensor
Example
# Create an all-zero tensor containing 5 elements
x = torch.zeros(5)
print(x)
The output result is:
tensor([0., 0., 0., 0., 0.])
In this example, we created a 1D all-zero tensor containing 5 elements.
Example 2: Creating a 2D All-Zero Tensor
Example
# Create a 3x4 all-zero tensor (matrix)
x = torch.zeros(3, 4)
print(x)
print(x.shape)
The output result is:
tensor([[0., 0., 0., 0.],
[0., 0., 0., 0.],
[0., 0., 0., 0.]])
torch.Size([3, 4])
In this example, we created a 2D all-zero tensor with 3 rows and 4 columns.
Example 3: Creating an Integer-Type All-Zero Tensor
Example
# Create an all-zero tensor of integer type
x = torch.zeros(3, 3, dtype=torch.int32)
print(x)
print(x.dtype)
The output result is:
tensor([[0, 0, 0],
[0, 0, 0],
[0, 0, 0]], dtype=torch.int32)
</p>
<p>在这个示例中,我们创建了整数类型的全零张量,默认的 <code>float32</code> 被覆盖为 <code>int32</code>。</p>
<h3>示例 4: 创建需要梯度的全零张量</h3>
<div class="example">
<h2 class="example">实例</h2>
<div class="example_code">
<span style="color: Green;font-weight:bold;">import</span> torch<br />
<br />
<span style="color: #a50"># 创建需要梯度的全零张量</span><br />
x <span style="color: Gray;">=</span> torch.<span style="color: #05a;">zeros</span><span style="color: Olive;">(</span><span style="color: Maroon;">3</span><span style="color: Gray;">,</span> requires_grad<span style="color: Gray;">=</span><span style="color: Teal;">True</span><span style="color: Olive;">)</span><br />
<br />
<span style="color: Green;font-weight:bold;">print</span><span style="color: Olive;">(</span>x.<span style="color: #05a;">requires_grad</span><span style="color: Olive;">)</span><br />
</div>
</div>
<p>输出结果为:</p>
<pre>
True
In this example, we created an all-zero tensor that requires gradient computation, which is used for learnable parameters when training neural networks.
Example 5: Creating an All-Zero Tensor on a CUDA Device
Example
# Check if 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 is not available")
The output result is:
cuda:0
In this example, we check whether CUDA is available, and then create an all-zero tensor on the GPU.
Difference Between torch.zeros and torch.zeros_like
torch.zeros(*size): Creates an all-zero tensor according to the specified size.torch.zeros_like(input): Creates an all-zero tensor based on the shape of the input tensor, preserving the input tensor's dtype and device.
Use Cases
torch.zerosThe function is commonly used in the following scenarios:
- Initializing bias vectors: In neural networks, biases are usually initialized to zero.
- Creating placeholders: As input placeholders in dynamic computation graphs.
- Filling arrays: Create an array initially zero, and fill in data later.
- Mathematical operations: In accumulation operations where zero is needed as the starting value.
Notes
- By default,
torch.zerosit creates afloat32tensor of type. - If you need to create an all-zero tensor with the same shape as another tensor, you can use
torch.zeros_like()。 - All-zero tensors are commonly used for initialization in deep learning, but in some cases they may lead to the vanishing gradient problem.
Other Extensions