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

PyTorch torch.slice_scatter function

PyTorch torch.slice_scatter function


PyTorch torch reference manual PyTorch torch reference manual

torch.slice_scatter is a function in PyTorch used to scatter values from a source tensor into slice positions of an input tensor.

Its effect is equivalent to input[dim][start:end:step] = src, but it does not modify the original tensor; instead, it returns a new tensor.

Function definition

torch.slice_scatter The complete function definition is as follows:



torch.slice_scatter(input, src, dim=0, start=None, end=None, step=1)

Parameter Description

The following table lists the torch.slice_scatter The various parameters:

Parameters Type Required Default value Description
input Tensor Required — The input tensor, that is, the tensor to be modified.
src Tensor Required — The source tensor, the values to be scattered into the slices of input,its size must match the size after slicing.
dim int Optional 0 The dimension along which to scatter.
start int Optional None Start index (Containsthis index), starting from 0 by default.
end int Optional None End index (does not containthis index), ending at the end by default.
step int Optional 1 Step size, with the same meaning as step in Python slicing.

Return Value

Returns the new tensor after scattering (torch.Tensor), the original input will not be modified.


Usage examples

The following example demonstrates torch.slice_scatter common usage.

Example

import

torch

Create the input tensor and source tensor

input

=

torch.

zeros

(

8

,

4

)

src

=

torch.

ones

(

2

,

4

)

Scatter src into the first two rows of input

output

=

torch.

slice_scatter

(

input

,

src

,

dim

=

0

,

end

=

2

)

print

(

"Input tensor:"

)

print

(

input

)

print

(

"\nSource tensor:"

)

print

(

src

)

print

(

"\nScatter result:"

)

print

(

output

)

The output is:



Input tensor:

tensor([[0., 0., 0., 0.],

        [0., 0., 0., 0.],

        [0., 0., 0., 0.],

        [0., 0., 0., 0.],

        [0., 0., 0., 0.],

        [0., 0., 0., 0.],

        [0., 0., 0., 0.],

        [0., 0., 0., 0.]])



Source Tensor:

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

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



Dissemination results:

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

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

        [0., 0., 0., 0.],

        [0., 0., 0., 0.],

        [0., 0., 0., 0.],

        [0., 0., 0., 0.],

        [0., 0., 0., 0.],

        [0., 0., 0., 0.]])

Example

import

torch

Use start and end to specify the range

input

=

torch.

zeros

(

10

)

src

=

torch.

tensor

(

[

1

,

2

,

3

]

)

Scatter src into positions 2-4 of input

output

=

torch.

slice_scatter

(

input

,

src

,

dim

=

0

,

start

=

2

,

end

=

5

)

print

(

"Input:"

,

input

)

print

(

"Source:"

,

src

)

print

(

"Result:"

,

output

)

The output is:



输入: tensor([0., 0., 0., 0., 0., 0., 0., 0., 0., 0.])

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

结果: tensor([0., 0., 1., 2., 3., 0., 0., 0., 0., 0.])

Example

import

torch

Use the step parameter

input

=

torch.

zeros

(

10

)

src

=

torch.

tensor

(

[

1

,

2

]

)

With a step of 2, starting at index 0 and ending at 4 (not including 4), the actual writes are at indices 0 and 2

output

=

torch.

slice_scatter

(

input

,

src

,

dim

=

0

,

start

=

0

,

end

=

4

,

step

=

2

)

print

(

"Input:"

,

input

)

print

(

"Source:"

,

src

)

print

(

"Result with step 2:"

,

output

)

The output is:



输入: tensor([0., 0., 0., 0., 0., 0., 0., 0., 0., 0.])

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

步长为2的结果: tensor([1., 0., 2., 0., 0., 0., 0., 0., 0., 0.])


Note:torch.slice_scatter The original input tensor is not modified; instead, a new tensor is returned.

This function is an alias of the torch.slice inverse operation.


PyTorch torch reference manual PyTorch torch reference manual