快捷键

torch.Tensor.sparse_mask

Tensor.sparse_mask(mask) Tensor

返回一个新的稀疏张量,其值来自偏移张量 self ,并通过稀疏张量 mask 的索引进行过滤。 mask 稀疏张量的值将被忽略。 selfmask 张量必须具有相同的形状。

注意

如果 mask 没有合并,则返回的稀疏张量可能包含重复的值。因此,如果不需要这种行为,建议传递 mask.coalesce()

注意

即使 self 中相应的值为零,返回的稀疏张量也具有与稀疏张量 mask 相同的索引。

参数:

mask(张量)- 一个稀疏张量,其索引用作过滤器

示例:

>>> nse = 5
>>> dims = (5, 5, 2, 2)
>>> I = torch.cat([torch.randint(0, dims[0], size=(nse,)),
...                torch.randint(0, dims[1], size=(nse,))], 0).reshape(2, nse)
>>> V = torch.randn(nse, dims[2], dims[3])
>>> S = torch.sparse_coo_tensor(I, V, dims).coalesce()
>>> D = torch.randn(dims)
>>> D.sparse_mask(S)
tensor(indices=tensor([[0, 0, 0, 2],
                       [0, 1, 4, 3]]),
       values=tensor([[[ 1.6550,  0.2397],
                       [-0.1611, -0.0779]],

                      [[ 0.2326, -1.0558],
                       [ 1.4711,  1.9678]],

                      [[-0.5138, -0.0411],
                       [ 1.9417,  0.5158]],

                      [[ 0.0793,  0.0036],
                       [-0.2569, -0.1055]]]),
       size=(5, 5, 2, 2), nnz=4, layout=torch.sparse_coo)

© 版权所有 PyTorch 贡献者。

使用 Sphinx 构建,并使用 Read the Docs 提供的主题。

文档

PyTorch 的全面开发者文档

查看文档

教程

深入了解初学者和高级开发者的教程

查看教程

资源

查找开发资源并获得您的疑问解答

查看资源