slice_scatter

paddle. slice_scatter ( x, value, axes, starts, ends, strides, name=None ) [source]

Embeds the value tensor into x along multiple axes. Returns a new tensor instead of a view. The size of axes must be equal to starts , ends and strides.

Parameters
  • x (Tensor) – The input Tensor. Supported data types are bool, float16, float32, float64, uint8, int8, int16, int32, int64, bfloat16, complex64, complex128.

  • value (Tensor) – The tensor to embed into x. Supported data types are bool, float16, float32, float64, uint8, int8, int16, int32, int64, bfloat16, complex64, complex128.

  • axes (list|tuple) – the dimensions to insert the value.

  • starts (list|tuple) – the start indices of where to insert.

  • ends (list|tuple) – the stop indices of where to insert.

  • strides (list|tuple) – the steps for each insert.

  • name (str, optional) – Name for the operation (optional, default is None).

Returns

Tensor, same dtype and shape with x

Examples

>>> import paddle

>>> x = paddle.zeros((3, 9))
>>> value = paddle.ones((3, 2))
>>> res = paddle.slice_scatter(x, value, axes=[1], starts=[2], ends=[6], strides=[2])
>>> print(res)
Tensor(shape=[3, 9], dtype=float32, place=Place(cpu), stop_gradient=True,
[[0., 0., 1., 0., 1., 0., 0., 0., 0.],
 [0., 0., 1., 0., 1., 0., 0., 0., 0.],
 [0., 0., 1., 0., 1., 0., 0., 0., 0.]])

>>> # broadcast `value` got the same result
>>> x = paddle.zeros((3, 9))
>>> value = paddle.ones((3, 1))
>>> res = paddle.slice_scatter(x, value, axes=[1], starts=[2], ends=[6], strides=[2])
>>> print(res)
Tensor(shape=[3, 9], dtype=float32, place=Place(cpu), stop_gradient=True,
[[0., 0., 1., 0., 1., 0., 0., 0., 0.],
 [0., 0., 1., 0., 1., 0., 0., 0., 0.],
 [0., 0., 1., 0., 1., 0., 0., 0., 0.]])

>>> # broadcast `value` along multiple axes
>>> x = paddle.zeros((3, 3, 5))
>>> value = paddle.ones((1, 3, 1))
>>> res = paddle.slice_scatter(x, value, axes=[0, 2], starts=[1, 0], ends=[3, 4], strides=[1, 2])
>>> print(res)
Tensor(shape=[3, 3, 5], dtype=float32, place=Place(cpu), stop_gradient=True,
[[[0., 0., 0., 0., 0.],
  [0., 0., 0., 0., 0.],
  [0., 0., 0., 0., 0.]],
 [[1., 0., 1., 0., 0.],
  [1., 0., 1., 0., 0.],
  [1., 0., 1., 0., 0.]],
 [[1., 0., 1., 0., 0.],
  [1., 0., 1., 0., 0.],
  [1., 0., 1., 0., 0.]]])