PyTorch is a popular framework for creating and manipulating tensors for deep learning and other applications. Tensors are multidimensional arrays that can store numerical data and perform various operations on them. This practical, straightforward article will show you how to create tensors with zeros and ones in PyTorch.
Generating tensors filled with zeros
torch.zeros(*size)
You can use the torch.zeros(*size) function to get the job done. It returns a tensor filled with the scalar value 0, with the shape defined by the variable argument size.
Example:
import torch
zero_tensor = torch.zeros(2, 3, 2)
print(zero_tensor)
Output:
tensor([[[0., 0.],
[0., 0.],
[0., 0.]],
[[0., 0.],
[0., 0.],
[0., 0.]]])
torch.zeros_like(input)
If you want to create a zero-tensor with the same shape as a given tensor, use the torch.zeros_like() function as follows:
import torch
input_tensor = torch.tensor([
[1, 2, 3],
[4, 5, 6],
])
# create a tensor with the same shape as input_tensor
zero_tensor = torch.zeros_like(input_tensor)
print(zero_tensor)
Output:
tensor([[0, 0, 0],
[0, 0, 0]])
Tensors filled with zeros are useful in many situations, especially when you need starting points for creating other tensors with specific values or patterns.
Creating tensors filled with ones
torch.ones(*size)
To create a PyTorch tensor filled with ones, you can use the torch.ones(*size) function. This function will return a tensor filled with the scalar value 1, with the shape defined by the variable argument size.
Example:
import torch
one_tensor = torch.ones(2, 4)
print(one_tensor)
Output:
tensor([[1., 1., 1., 1.],
[1., 1., 1., 1.]])
torch.ones_like(input)
Similar to torch.zeros_like(), we have the torch.ones_like() function that can be used to generate a one-tensor with the shape of an input tensor like so:
import torch
input_tensor = torch.tensor([
[1, 2, 3],
[4, 5, 6],
])
# create a tensor with the same shape as input_tensor
one_tensor = torch.ones_like(input_tensor)
print(one_tensor)
Output:
tensor([[1, 1, 1],
[1, 1, 1]])
In machine learning, tensors filled with ones can be used as a placeholder for inputs in a computational graph or a model.
Creating eye tensors with torch.eye(n, m=None)
What about generating a 2-D tensor with both zeros and ones?
An eye tensor is a 2-D tensor with ones on the diagonal and zeros elsewhere. It is also known as an identity matrix because multiplying any matrix by an identity matrix of the same shape does not change the original matrix.
The torch.eye(n, m=None) function can be used to generate eye tensors in PyTorch.
Example:
import torch
eye_tensor = torch.eye(3)
print(eye_tensor)
Output:
ensor([[1., 0., 0.],
[0., 1., 0.],
[0., 0., 1.]])
An eye tensor can be used as a placeholder for inputs or outputs in a computational graph or a model or as a basis for creating other tensors with specific values or patterns. It can also be used as a mask to indicate which elements of another tensor should be kept or discarded.