Вот один из способов использования advanced indexing
и определения открытой сетки с помощью np.ogrid
для индексации output
и добавления values
соответственно:
batch, sequence, kb = values.shape
i,j,_ = np.ogrid[:batch, :sequence, :kb]
output[i,j,indexer] += values
Проверка и сроки -
def adv_ix(out, values, indexer):
batch, sequence, kb = values.shape
i,j,k = np.ogrid[:batch, :sequence, :kb]
out[i,j,indexer] += values
return out
def current_app(out, values, indexer):
batch, sequence, kb = values.shape
for x in range(batch):
for y in range(sequence):
for z in range(kb):
out[x,y,indexer[x,y,z]] += values[x,y,z]
return out
np.allclose(adv_ix(output, values, indexer), current_app(output, values, indexer))
# True
%timeit adv_ix(output, values, indexer)
# 28.2 µs ± 341 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)
%timeit current_app(output, values, indexer)
#1.49 ms ± 120 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)