1
0

More than 3 years have passed since last update.

【pytorch】torch.zeros vs torch.zeros_like

Posted at

torch.zerosとtorch.zeros_likeの違いが気になったので書く。

torch.zerosの使い方

値が0のtensorを返す。

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

torch.zeros_likeの使い方

あるtensorの値をすべて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.]])

上の例はinputの値をすべて0にしている。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0