快捷键

torch.Tensor.index_reduce_

Tensor.index_reduce_(dim, index, source, reduce, *, include_self=True) Tensor

source 的元素累积到 self 张量中,按照 index 中给出的顺序累积到索引,使用 reduce 参数指定的累积方式。例如,如果 dim == 0index[i] == jreduce == prodinclude_self == True ,则 i 行的 source 被乘以 j 行的 self 。如果 include_self="True" ,则 self 张量中的值包含在累积中,否则,累积到 self 张量中的行被视为填充了累积恒等元。

dim 维度 source 必须与 index 的长度相同(index 必须是一个向量),并且所有其他维度必须与 self 匹配,否则将引发错误。

对于具有 reduce="prod"include_self=True 的 3-D 张量,输出如下:

self[index[i], :, :] *= src[i, :, :]  # if dim == 0
self[:, index[i], :] *= src[:, i, :]  # if dim == 1
self[:, :, index[i]] *= src[:, :, i]  # if dim == 2

注意

当在 CUDA 设备上给定张量时,此操作可能表现出非确定性。有关更多信息,请参阅可重现性。

注意

此函数仅支持浮点张量。

警告

此函数处于测试阶段,未来可能会发生变化。

参数:
  • dim(int)- 沿着哪个维度进行索引

  • index (Tensor) – 从 source 选择的索引,应具有 torch.int64 或 torch.int32 的数据类型

  • source (FloatTensor) – 包含要累加值的张量

  • reduce (str) – 要应用的降维操作( "prod" , "mean" , "amax" , "amin"

关键字参数:

include_self (bool) – 是否将 self 张量中的元素包含在降维中

示例:

>>> x = torch.empty(5, 3).fill_(2)
>>> t = torch.tensor([[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]], dtype=torch.float)
>>> index = torch.tensor([0, 4, 2, 0])
>>> x.index_reduce_(0, index, t, 'prod')
tensor([[20., 44., 72.],
        [ 2.,  2.,  2.],
        [14., 16., 18.],
        [ 2.,  2.,  2.],
        [ 8., 10., 12.]])
>>> x = torch.empty(5, 3).fill_(2)
>>> x.index_reduce_(0, index, t, 'prod', include_self=False)
tensor([[10., 22., 36.],
        [ 2.,  2.,  2.],
        [ 7.,  8.,  9.],
        [ 2.,  2.,  2.],
        [ 4.,  5.,  6.]])

© 版权所有 PyTorch 贡献者。

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

文档

PyTorch 的全面开发者文档

查看文档

教程

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

查看教程

资源

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

查看资源