zeros(), zeros_like(), ones() and ones_like() in PyTorch

Rmag Breaking News

zeros() can create a 1D or more D tensor filled with zero or more 0., 0, 0.+0.j or False as shown below:

*Memos:

zeros() can be used only from torch but not from a tensor.
The default type is float32.

import torch

torch.zeros(0)
torch.zeros((0,))
# tensor([])

torch.zeros(3)
torch.zeros((3,))
# tensor([0., 0., 0.])

torch.zeros(3, 2)
torch.zeros((3, 2))
# tensor([[0., 0.], [0., 0.], [0., 0.]])

torch.zeros(3, 2, 4)
torch.zeros((3, 2, 4))
# tensor([[[0., 0., 0., 0.], [0., 0., 0., 0.]],
# [[0., 0., 0., 0.], [0., 0., 0., 0.]],
# [[0., 0., 0., 0.], [0., 0., 0., 0.]]])

torch.zeros(0, dtype=torch.long)
torch.zeros((0,), dtype=torch.long)
# tensor([], dtype=torch.int64)

torch.zeros(3, dtype=torch.long)
torch.zeros((3,), dtype=torch.long)
# tensor([0, 0, 0])

torch.zeros(3, 2, dtype=torch.long)
torch.zeros((3, 2), dtype=torch.long)
# tensor([[0, 0], [0, 0], [0, 0]])

torch.zeros(3, 2, 4, dtype=torch.long)
torch.zeros((3, 2, 4), dtype=torch.long)
# tensor([[[0, 0, 0, 0], [0, 0, 0, 0]],
# [[0, 0, 0, 0], [0, 0, 0, 0]],
# [[0, 0, 0, 0], [0, 0, 0, 0]]])

torch.zeros(0, dtype=torch.complex64)
torch.zeros((0,), dtype=torch.complex64)
# tensor([], dtype=torch.complex64)

torch.zeros(3, dtype=torch.complex64)
torch.zeros((3,), dtype=torch.complex64)
# tensor([0.+0.j, 0.+0.j, 0.+0.j])

torch.zeros(3, 2, dtype=torch.complex64)
torch.zeros((3, 2), dtype=torch.complex64)
# tensor([[0.+0.j, 0.+0.j],
# [0.+0.j, 0.+0.j],
# [0.+0.j, 0.+0.j]])

torch.zeros(3, 2, 4, dtype=torch.complex64)
torch.zeros((3, 2, 4), dtype=torch.complex64)
# tensor([[[0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j],
# [0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j]],
# [[0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j],
# [0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j]],
# [[0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j],
# [0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j]]])

torch.zeros(0, dtype=torch.bool)
torch.zeros((0,), dtype=torch.bool)
# tensor([], dtype=torch.bool)

torch.zeros(3, dtype=torch.bool)
torch.zeros((3,), dtype=torch.bool)
# tensor([False, False, False])

torch.zeros(3, 2, dtype=torch.bool)
torch.zeros((3, 2), dtype=torch.bool)
# tensor([[False, False],
# [False, False],
# [False, False]])

torch.zeros(3, 2, 4, dtype=torch.bool)
torch.zeros((3, 2, 4), dtype=torch.bool)
# tensor([[[False, False, False, False],
# [False, False, False, False]],
# [[False, False, False, False],
# [False, False, False, False]],
# [[False, False, False, False],
# [False, False, False, False]]])

zeros_like can replace the zero or more integers, floating-point numbers complex numbers or boolean values of 0D or more D tensor with zero or more 0., 0, 0.+0.j or False as shown below. *zeros_like() can be used only from torch but not from a tensor:

import torch

my_tensor = torch.tensor(7.)
torch.zeros_like(my_tensor)
# tensor(0.)
torch.zeros_like(my_tensor, dtype=torch.long)
# tensor(0)
torch.zeros_like(my_tensor, dtype=torch.complex64)
# tensor(0.+0.j)
torch.zeros_like(my_tensor, dtype=torch.bool)
# tensor(False)

my_tensor = torch.tensor([7, 4, 5])
torch.zeros_like(my_tensor)
# tensor([0, 0, 0])
torch.zeros_like(my_tensor, dtype=torch.float)
# tensor([0., 0., 0.])
torch.zeros_like(my_tensor, dtype=torch.complex64)
# tensor([0.+0.j, 0.+0.j, 0.+0.j])
torch.zeros_like(my_tensor, dtype=torch.bool)
# tensor([False, False, False])

my_tensor = torch.tensor([[7+4j, 4+2j, 5+3j], [2+5j, 8+1j, 3+9j]])
torch.zeros_like(my_tensor)
# tensor([[0.+0.j, 0.+0.j, 0.+0.j], [0.+0.j, 0.+0.j, 0.+0.j]])
torch.zeros_like(my_tensor, dtype=torch.long)
# tensor([[0, 0, 0], [0, 0, 0]])
torch.zeros_like(my_tensor, dtype=torch.float)
# tensor([[0., 0., 0.], [0., 0., 0.]])
torch.zeros_like(my_tensor, dtype=torch.bool)
# tensor([[False, False, False], [False, False, False]])

my_tensor = torch.tensor([[[True, False, True], [False, True, False]],
[[False, True, False], [True, False, True]]])
torch.zeros_like(my_tensor)
# tensor([[[False, False, False], [False, False, False]],
# [[False, False, False], [False, False, False]]])
torch.zeros_like(my_tensor, dtype=torch.long)
# tensor([[[0, 0, 0], [0, 0, 0]],
# [[0, 0, 0], [0, 0, 0]]])
torch.zeros_like(my_tensor, dtype=torch.float)
# tensor([[[0., 0., 0.], [0., 0., 0.]],
# [[0., 0., 0.], [0., 0., 0.]]])
torch.zeros_like(my_tensor, dtype=torch.complex64)
# tensor([[[0.+0.j, 0.+0.j, 0.+0.j], [0.+0.j, 0.+0.j, 0.+0.j]],
# [[0.+0.j, 0.+0.j, 0.+0.j], [0.+0.j, 0.+0.j, 0.+0.j]]])

ones() can create a 1D or more D tensor filled with zero or more 1., 1, 1.+0.j or True as shown below:

*Memos:

ones() can be used only from torch but not from a tensor.
The default type is float32.

import torch

torch.ones(3)
torch.ones((3,))
# tensor([1., 1., 1.])

torch.ones(3, 2)
torch.ones((3, 2))
# tensor([[1., 1.], [1., 1.], [1., 1.]])

torch.ones(3, 2, 4)
torch.ones((3, 2, 4))
# tensor([[[1., 1., 1., 1.], [1., 1., 1., 1.]],
# [[1., 1., 1., 1.], [1., 1., 1., 1.]],
# [[1., 1., 1., 1.], [1., 1., 1., 1.]]])

torch.ones(3, dtype=torch.long)
torch.ones((3,), dtype=torch.long)
# tensor([1, 1, 1])

torch.ones(3, 2, dtype=torch.long)
torch.ones((3, 2), dtype=torch.long)
# tensor([[1, 1], [1, 1], [1, 1]])

torch.ones(3, 2, 4, dtype=torch.long)
torch.ones((3, 2, 4), dtype=torch.long)
# tensor([[[1, 1, 1, 1], [1, 1, 1, 1]],
# [[1, 1, 1, 1], [1, 1, 1, 1]],
# [[1, 1, 1, 1], [1, 1, 1, 1]]])

torch.ones(0, dtype=torch.complex64)
torch.ones((0,), dtype=torch.complex64)
# tensor([], dtype=torch.complex64)

torch.ones(3, dtype=torch.complex64)
torch.ones((3,), dtype=torch.complex64)
# tensor([1.+0.j, 1.+0.j, 1.+0.j])

torch.ones(3, 2, dtype=torch.complex64)
torch.ones((3, 2), dtype=torch.complex64)
# tensor([[1.+0.j, 1.+0.j],
# [1.+0.j, 1.+0.j],
# [1.+0.j, 1.+0.j]])

torch.ones(3, 2, 4, dtype=torch.complex64)
torch.ones((3, 2, 4), dtype=torch.complex64)
# tensor([[[1.+0.j, 1.+0.j, 1.+0.j, 1.+0.j],
# [1.+0.j, 1.+0.j, 1.+0.j, 1.+0.j]],
# [[1.+0.j, 1.+0.j, 1.+0.j, 1.+0.j],
# [1.+0.j, 1.+0.j, 1.+0.j, 1.+0.j]],
# [[1.+0.j, 1.+0.j, 1.+0.j, 1.+0.j],
# [1.+0.j, 1.+0.j, 1.+0.j, 1.+0.j]]])

torch.ones(0, dtype=torch.bool)
torch.ones((0,), dtype=torch.bool)
# tensor([], dtype=torch.bool)

torch.ones(3, dtype=torch.bool)
torch.ones((3,), dtype=torch.bool)
# tensor([True, True, True])

torch.ones(3, 2, dtype=torch.bool)
torch.ones((3, 2), dtype=torch.bool)
# tensor([[True, True],
# [True, True],
# [True, True]])

torch.ones(3, 2, 4, dtype=torch.bool)
torch.ones((3, 2, 4), dtype=torch.bool)
# tensor([[[True, True, True, True],
# [True, True, True, True]],
# [[True, True, True, True],
# [True, True, True, True]],
# [[True, True, True, True],
# [True, True, True, True]]])

ones_like() can replace the zero or more integers, floating-point numbers complex numbers or boolean values of 0D or more D tensor with zero or more 1., 1, 1.+0.j or True as shown below. *ones_like() can be used only from torch but not from a tensor:

import torch

my_tensor = torch.tensor(7.)
torch.ones_like(my_tensor)
# tensor(1.)
torch.ones_like(my_tensor, dtype=torch.long)
# tensor(1)
torch.ones_like(my_tensor, dtype=torch.complex64)
# tensor(1.+0.j)
torch.ones_like(my_tensor, dtype=torch.bool)
# tensor(True)

my_tensor = torch.tensor([7, 4, 5])
torch.ones_like(my_tensor)
# tensor([1, 1, 1])
torch.ones_like(my_tensor, dtype=torch.float)
# tensor([1., 1., 1.])
torch.ones_like(my_tensor, dtype=torch.complex64)
# tensor([1.+0.j, 1.+0.j, 1.+0.j])
torch.ones_like(my_tensor, dtype=torch.bool)
# tensor([True, True, True])

my_tensor = torch.tensor([[7+4j, 4+2j, 5+3j], [2+5j, 8+1j, 3+9j]])
torch.ones_like(my_tensor)
# tensor([[1.+0.j, 1.+0.j, 1.+0.j], [1.+0.j, 1.+0.j, 1.+0.j]])
torch.ones_like(my_tensor, dtype=torch.long)
# tensor([[1, 1, 1], [1, 1, 1]])
torch.ones_like(my_tensor, dtype=torch.float)
# tensor([[1., 1., 1.], [1., 1., 1.]])
torch.ones_like(my_tensor, dtype=torch.bool)
# tensor([[True, True, True], [True, True, True]])

my_tensor = torch.tensor([[[True, False, True], [False, True, False]],
[[False, True, False], [True, False, True]]])
torch.ones_like(my_tensor)
# tensor([[[True, True, True], [True, True, True]],
# [[True, True, True], [True, True, True]]])
torch.ones_like(my_tensor, dtype=torch.long)
# tensor([[[1, 1, 1], [1, 1, 1]],
# [[1, 1, 1], [1, 1, 1]]])
torch.ones_like(my_tensor, dtype=torch.float)
# tensor([[[1., 1., 1.], [1., 1., 1.]],
# [[1., 1., 1.], [1., 1., 1.]]])
torch.ones_like(my_tensor, dtype=torch.complex64)
# tensor([[[1.+0.j, 1.+0.j, 1.+0.j], [1.+0.j, 1.+0.j, 1.+0.j]],
# [[1.+0.j, 1.+0.j, 1.+0.j], [1.+0.j, 1.+0.j, 1.+0.j]]])

Leave a Reply

Your email address will not be published. Required fields are marked *