Shortcuts

torch.masked_select

torch.masked_select(input, mask, *, out=None) 张量

返回一个新的1-D张量,该张量根据布尔掩码maskinput张量进行索引,其中mask是一个BoolTensor

The shapes of the mask tensor and the input tensor don’t need to match, but they must be 可广播的.

注意

返回的张量不使用与原始张量相同的存储空间

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

  • mask (BoolTensor) – 包含用于索引的二进制掩码的张量

Keyword Arguments

输出 (张量, 可选) – 输出张量。

示例:

>>> x = torch.randn(3, 4)
>>> x
tensor([[ 0.3552, -2.3825, -0.8297,  0.3477],
        [-1.2035,  1.2252,  0.5002,  0.6248],
        [ 0.1307, -2.0608,  0.1244,  2.0139]])
>>> mask = x.ge(0.5)
>>> mask
tensor([[False, False, False, False],
        [False, True, True, True],
        [False, False, False, True]])
>>> torch.masked_select(x, mask)
tensor([ 1.2252,  0.5002,  0.6248,  2.0139])
优云智算