Shortcuts

torch.count_nonzero

torch.count_nonzero(input, dim=None) 张量

计算张量 input 沿给定 dim 的非零值的数量。 如果没有指定 dim,则计算张量中所有非零值的数量。

Parameters
  • 输入 (张量) – 输入张量。

  • dim (inttupleints可选) – 沿其计算非零值的 dim 或 dims 的元组。

示例:

>>> x = torch.zeros(3,3)
>>> x[torch.randn(3,3) > 0.5] = 1
>>> x
tensor([[0., 1., 1.],
        [0., 0., 0.],
        [0., 0., 1.]])
>>> torch.count_nonzero(x)
tensor(3)
>>> torch.count_nonzero(x, dim=0)
tensor([0, 1, 2])
优云智算