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

PyTorch torch.scatter_reduce function

PyTorch torch.scatter_reduce function


PyTorch torch reference manual PyTorch torch reference manual

torch.scatter_reduce is a function in PyTorch used to aggregate values from a source tensor at specified positions in a specified way. It supports multiple aggregation modes such as sum, product, maximum, minimum, and so on.

Function definition

torch.scatter_reduce(input, dim, index, src, reduce='sum', *, include_self=True)

Parameters:

Return Value:


Usage examples

Example

import

torch

Create an input tensor

input

=

torch.

ones

(

3

,

5

)

Create the indices and source

index

=

torch.

tensor

(

[

[

0

,

1

,

2

,

0

,

0

]

,

                     

[

1

,

2

,

0

,

1

,

2

]

,

                     

[

2

,

0

,

1

,

2

,

0

]

]

)

src

=

torch.

tensor

(

[

[

1

,

1

,

1

,

1

,

1

]

,

                   

[

2

,

2

,

2

,

2

,

2

]

,

                   

[

3

,

3

,

3

,

3

,

3

]

]

)

Aggregate using sum

output

=

torch.

scatter_reduce

(

input

,

dim

=

0

,

index

=

index

,

src

=

src

,

reduce

=

'sum'

)

print

(

"Input:"

)

print

(

input

)

print

(

"nAggregation mode: sum"

)

print

(

"Result:"

)

print

(

output

)

The output is:

输入:

tensor([[1., 1., 1., 1., 1.],

        [1., 1., 1., 1., 1.],

        [1., 1., 1., 1., 1.]])



聚合方式: sum

结果:

tensor([[4., 2., 3., 4., 4.],

        [2., 2., 2., 2., 2.],

        [3., 3., 2., 3., 3.]])

Example

import

torch

Test different aggregation modes

input

=

torch.

zeros

(

3

,

3

)

index

=

torch.

tensor

(

[

[

0

,

0

,

0

]

,

                     

[

1

,

1

,

1

]

,

                     

[

2

,

2

,

2

]

]

)

src

=

torch.

tensor

(

[

[

2

,

3

,

4

]

,

                   

[

5

,

6

,

7

]

,

                   

[

8

,

9

,

10

]

]

)

Use prod (product)

output_prod

=

torch.

scatter_reduce

(

input

,

0

,

index

,

src

,

reduce

=

'prod'

)

print

(

"prod aggregation:"

)

print

(

output_prod

)

Use amax (maximum)

output_max

=

torch.

scatter_reduce

(

input

,

0

,

index

,

src

,

reduce

=

'amax'

)

print

(

"namax aggregation:"

)

print

(

output_max

)

Use amin (minimum)

output_min

=

torch.

scatter_reduce

(

input

,

0

,

index

,

src

,

reduce

=

'amin'

)

print

(

"namin aggregation:"

)

print

(

output_min

)

The output is:

prod 聚合:

tensor([[ 2.,  3.,  4.],

        [ 5.,  6.,  7.],

        [ 8.,  9., 10.]])



amax 聚合:

tensor([[ 2.,  3.,  4.],

        [ 5.,  6.,  7.],

        [ 8.,  9., 10.]])



amin 聚合:

tensor([[ 2.,  3.,  4.],

        [ 5.,  6.,  7.],

        [ 8.,  9., 10.]])

Example

import

torch

Use the include_self parameter

input

=

torch.

tensor

(

[

1.0

,

2.0

,

3.0

]

)

index

=

torch.

tensor

(

[

0

,

0

,

0

]

)

src

=

torch.

tensor

(

[

10.0

,

20.0

,

30.0

]

)

Include self values (default)

output1

=

torch.

scatter_reduce

(

input

,

0

,

index

,

src

,

reduce

=

'sum'

,

include_self

=

True

)

print

(

"include_self=True:"

,

output1

)

Do not include self values

output2

=

torch.

scatter_reduce

(

input

,

0

,

index

,

src

,

reduce

=

'sum'

,

include_self

=

False

)

print

(

"include_self=False:"

,

output2

)

The output is:

include_self=True: tensor([66.,  2.,  3.])

include_self=False: tensor([60.,  2.,  3.])


Note:torch.scatter_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, such as avoiding double-counting a node's own features in graph neural networks.


PyTorch torch reference manual PyTorch torch reference manual