[PYTHON] [Pytorch] torch.zeros vs torch.zeros_like

I'm curious about the difference between torch.zeros and torch.zeros_like, so I'll write it.

How to use torch.zeros

Returns a tensor with a value of 0.

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

How to use torch.zeros_like

It is used when you want to set all the values of a certain tensor to 0.

>>> input = torch.empty(3, 2)
tensor([[3.2561e+09, 3.0936e-41],
        [0.0000e+00, 0.0000e+00],
        [3.2557e+09, 3.0936e-41]])
>>> torch.zeros_like(input)
tensor([[0., 0.],
        [0., 0.],
        [0., 0.]])

In the above example, all input values are set to 0.

Recommended Posts

[Pytorch] torch.zeros vs torch.zeros_like
PyTorch C ++ VS Python (2019 Edition)
[PyTorch] CPU vs. GPU vs. TPU [Fine Tuning]