PyTorch相当于index_add_,取而代之的是最大值

时间:2018-05-30 12:51:16

标签: python pytorch

在PyTorch中,Tensor的git remote set-url origin git@gitlab.example.com:name.surname/project.git 方法使用提供的索引张量进行求和:

index_add_

前四个子值总和为父[0],后两个子进入父[1],结果为idx = torch.LongTensor([0,0,0,0,1,1]) child = torch.FloatTensor([1, 3, 5, 10, 8, 1]) parent = torch.FloatTensor([0, 0]) parent.index_add_(0, idx, child)

但是,我需要改为tensor([ 19., 9.]),而API中并不存在。有没有办法有效地做到这一点(无需循环或分配更多内存)?一个(坏的)循环解决方案是:

index_max_

这会产生for i in range(max(idx)+1): parent[i] = torch.max(child[idx == i]) 的预期结果,但速度很慢。

1 个答案:

答案 0 :(得分:1)

使用索引的解决方案:

def index_max(child, idx, num_partitions): 
    # Building a num_partition x num_samples matrix `idx_tiled`:
    partition_idx = torch.range(0, num_partitions - 1, dtype=torch.long)
    partition_idx = partition_idx.view(-1, 1).expand(num_partitions, idx.shape[0])
    idx_tiled = idx.view(1, -1).repeat(num_partitions, 1)
    idx_tiled = (idx_tiled == partition_idx).float()
    # i.e. idx_tiled[i,j] == 1 if idx[j] == i, else 0

    parent = idx_tiled * child
    parent, _ = torch.max(parent, dim=1)
    return parent

基准:

import timeit

setup = '''
import torch

def index_max_v0(child, idx, num_partitions):
    parent = torch.zeros(num_partitions)
    for i in range(max(idx) + 1):
        parent[i] = torch.max(child[idx == i])
    return parent

def index_max(child, idx, num_partitions):

    # Building a num_partition x num_samples matrix `idx_tiled` 
    # containing for each row indices of
    partition_idx = torch.range(0, num_partitions - 1, dtype=torch.long)
    partition_idx = partition_idx.view(-1, 1).expand(num_partitions, idx.shape[0])
    idx_tiled = idx.view(1, -1).repeat(num_partitions, 1)
    idx_tiled = (idx_tiled == partition_idx).float()

    parent = idx_tiled * child
    parent, _ = torch.max(parent, dim=1)
    return parent

idx = torch.LongTensor([0,0,0,0,1,1])
child = torch.FloatTensor([1, 3, 5, 10, 8, 1])
num_partitions = torch.unique(idx).shape[0]

'''
print(min(timeit.Timer('index_max_v0(child, idx, num_partitions)', setup=setup).repeat(5, 1000)))
# > 0.05308796599274501
print(min(timeit.Timer('index_max(child, idx, num_partitions)', setup=setup).repeat(5, 1000)))
# > 0.024736385996220633