Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

PyTorch torch.index_reduce function

PyTorch torch.index_reduce function


PyTorch torch reference manual PyTorch torch reference manual

torch.index_reduce A function in PyTorch for aggregating the values of a source tensor at specified index positions in a specified way. It aggregates along a specified dimension dim, in index at the specified index positions in the specified way source value.

Function definition

torch.index_reduce(input, dim, index, source, reduce='mean', *, include_self=True)

Parameters:

Return Value:


Usage examples

Example

import

torch

Create an input tensor

input

=

torch.

randn

(

3

,

3

)

Create the indices and source

index

=

torch.

tensor

(

[

0

,

0

,

0

]

)

source

=

torch.

tensor

(

[

1.0

,

2.0

,

3.0

]

)

Aggregate using mean

output

=

torch.

index_reduce

(

input

,

dim

=

0

,

index

=

index

,

source

=

source

,

reduce

=

'mean'

)

print

(

"Input:"

)

print

(

input

)

print

(

"nIndices:"

,

index

)

print

(

"Source:"

,

source

)

print

(

"nmean aggregation result:"

)

print

(

output

)

The output is:

输入:

tensor([[ 0.3456, -0.1234,  0.5678],

        [-0.5678,  0.1234, -0.6789],

        [ 0.7890, -0.3456,  0.1234]])



索引: tensor([0, 0, 0])

源: tensor([1., 2., 3.])



mean aggregated result:

tensor([[ 2.5237,  0.2931,  0.3374],

        [-0.5678,  0.1234, -0.6789],

        [ 0.7890, -0.3456,  0.1234]])

Example

import

torch

Test different aggregation modes

input

=

torch.

ones

(

3

)

index

=

torch.

tensor

(

[

0

,

0

,

0

]

)

source

=

torch.

tensor

(

[

2.0

,

4.0

,

6.0

]

)

prod aggregation

output_prod

=

torch.

index_reduce

(

input

,

0

,

index

,

source

,

reduce

=

'prod'

)

print

(

"prod aggregation:"

,

output_prod

)

amax aggregation

output_max

=

torch.

index_reduce

(

input

,

0

,

index

,

source

,

reduce

=

'amax'

)

print

(

"amax aggregation:"

,

output_max

)

amin aggregation

output_min

=

torch.

index_reduce

(

input

,

0

,

index

,

source

,

reduce

=

'amin'

)

print

(

"amin aggregation:"

,

output_min

)

The output is:

prod 聚合: tensor([48.,  1.,  1.])

amax 聚合: tensor([7., 1., 1.])

amin 聚合: tensor([3., 1., 1.])

Example

import

torch

The include_self parameter

input

=

torch.

tensor

(

[

1.0

,

10.0

,

100.0

]

)

index

=

torch.

tensor

(

[

0

,

0

]

)

source

=

torch.

tensor

(

[

5.0

,

5.0

]

)

Include self values (default)

output1

=

torch.

index_reduce

(

input

,

0

,

index

,

source

,

reduce

=

'mean'

,

include_self

=

True

)

print

(

"include_self=True:"

,

output1

)

Do not include self values

output2

=

torch.

index_reduce

(

input

,

0

,

index

,

source

,

reduce

=

'mean'

,

include_self

=

False

)

print

(

"include_self=False:"

,

output2

)

The output is:

include_self=True: tensor([ 3.6667, 10.0000, 100.0000])

include_self=False: tensor([ 5., 10., 100.])

Example

import

torch

Application to 2D tensors

input

=

torch.

zeros

(

3

,

4

)

index

=

torch.

tensor

(

[

0

,

2

,

2

]

)

source

=

torch.

randn

(

3

,

4

)

mean aggregation

output

=

torch.

index_reduce

(

input

,

dim

=

0

,

index

=

index

,

source

=

source

,

reduce

=

'mean'

)

print

(

"Input shape:"

,

input

.

shape

)

print

(

"Indices:"

,

index

)

print

(

"Source shape:"

,

source.

shape

)

print

(

"Result shape:"

,

output.

shape

)

print

(

"nResult:"

)

print

(

output

)


Note:torch.index_reduce Does not modify the original input tensor; instead, it returns a new tensor. Multiple indices can point to the same position, and values are aggregated in the specified way.include_self=False Useful in some scenarios, where you can exclude the original value and aggregate only the newly added values.


PyTorch torch reference manual PyTorch torch reference manual