gather#

ivy.gather(params, indices, /, *, axis=-1, batch_dims=0, out=None)[source]#

Gather slices from params at axis according to indices.

Parameters:
  • params (Union[Array, NativeArray]) – The array from which to gather values.

  • indices (Union[Array, NativeArray]) – The array which indicates the indices that will be gathered along the specified axis.

  • axis (int, default: -1) – Optional int, the axis from which to gather from. Default is -1.

  • batch_dims (int, default: 0) – Optional int, lets you gather different items from each element of a batch. Default is 0.

  • out (Optional[Union[Array, NativeArray]], default: None) – Optional array, for writing the result to. It must have a shape that the inputs broadcast to.

Return type:

Union[Array, NativeArray]

Returns:

ret – New array with the values gathered at the specified indices along the specified axis.

Both the description and the type hints above assumes an array input for simplicity, but this function is nestable, and therefore also accepts ivy.Container instances in place of any of the arguments.

Examples

With ivy.Array input:

>>> x = ivy.array([0., 1., 2.])
>>> y = ivy.array([1, 2])
>>> print(ivy.gather(x, y))
ivy.array([1., 2.])
>>> x = ivy.array([[0., 1., 2.],[3., 4., 5.]])
>>> y = ivy.array([[0, 1],[1, 2]])
>>> z = ivy.zeros((2, 2, 2))
>>> ivy.gather(x, y, out=z)
>>> print(z)
ivy.array([[[0., 1.],[1., 2.]],[[3., 4.],[4., 5.]]])
>>> x = ivy.array([[[0., 1.], [2., 3.]],
...                [[8., 9.], [10., 11.]]])
>>> y = ivy.array([[0, 1]])
>>> z = ivy.zeros((1, 2, 2, 2))
>>> ivy.gather(x, y, axis=0, out=z)
>>> print(z)
ivy.array(
    [[[[ 0.,  1.],
       [ 2.,  3.]],
      [[ 8.,  9.],
       [10., 11.]]]])
>>> x = ivy.array([[0, 10, 20, 0, 0],
...                [0, 0, 0, 30, 40],
...                [0, 10, 0, 0, 40]])
>>> y = ivy.array([[1, 2],[3, 4],[1, 4]])
>>> z = ivy.gather(x, y, batch_dims=1)
>>> print(z)
ivy.array([[10, 20], [30, 40],[10, 40]])

With ivy.Container input:

>>> x = ivy.Container(a = ivy.array([0., 1., 2.]),
...                   b = ivy.array([4., 5., 6.]))
>>> y = ivy.Container(a = ivy.array([0, 1]),
...                   b = ivy.array([1, 2]))
>>> print(ivy.gather(x, y))
{
    a: ivy.array([0., 1.]),
    b: ivy.array([5., 6.])
}

With a mix of ivy.Array and ivy.Container inputs:

>>> x = ivy.Container(a = ivy.array([0., 1., 2.]),
...                   b = ivy.array([4., 5., 6.]))
>>> y = ivy.array([0, 1])
>>> print(ivy.gather(x, y))
{
    a: ivy.array([0., 1.]),
    b: ivy.array([4., 5.])
}
Array.gather(self, indices, /, *, axis=-1, batch_dims=0, out=None)[source]#

ivy.Array instance method variant of ivy.gather. This method simply wraps the function, and so the docstring for ivy.gather also applies to this method with minimal changes.

Parameters:
  • self (Array) – The array from which to gather values.

  • indices (Union[Array, NativeArray]) – The array which indicates the indices that will be gathered along the specified axis.

  • axis (int, default: -1) – The axis from which the indices will be gathered. Default is -1.

  • batch_dims (int, default: 0) – Optional int, lets you gather different items from each element of a batch. Default is 0.

  • out (Optional[Array], default: None) – Optional array, for writing the result to. It must have a shape that the inputs broadcast to.

Return type:

Array

Returns:

ret – New array with the values gathered at the specified indices along the specified axis.

Examples

>>> x = ivy.array([0., 1., 2.])
>>> y = ivy.array([0, 1])
>>> gather = x.gather(y)
>>> print(gather)
ivy.array([0., 1.])
>>> x = ivy.array([[0., 1., 2.],[3., 4., 5.]])
>>> y = ivy.array([[0, 1],[1, 2]])
>>> z = ivy.zeros((2, 2, 2))
>>> gather = x.gather(y, out=z)
>>> print(z)
ivy.array([[[0., 1.],[1., 2.]],[[3., 4.],[4., 5.]]])
>>> x = ivy.array([[[0., 1.], [2., 3.]],
...                [[8., 9.], [10., 11.]]])
>>> y = ivy.array([[0, 1]])
>>> z = ivy.zeros((1, 2, 2, 2))
>>> gather = x.gather(y, axis=0, out=z)
>>> print(z)
ivy.array(
    [[[[ 0.,  1.],
    [ 2.,  3.]],
    [[ 8.,  9.],
    [10., 11.]]]])
>>> x = ivy.array([[0, 10, 20, 0, 0],
...                [0, 0, 0, 30, 40],
...                [0, 10, 0, 0, 40]])
>>> y = ivy.array([[1, 2],[3, 4],[1, 4]])
>>> gather = x.gather(y, batch_dims=1)
>>> print(gather)
ivy.array([[10, 20], [30, 40],[10, 40]])
Container.gather(self, indices, /, *, axis=-1, batch_dims=0, key_chains=None, to_apply=True, prune_unapplied=False, map_sequences=False, out=None)[source]#

ivy.Container instance method variant of ivy.gather. This method simply wraps the function, and so the docstring for ivy.gather also applies to this method with minimal changes.

Parameters:
  • self (Container) – The container from which to gather values.

  • indices (Union[Container, Array, NativeArray]) – The container or array which indicates the indices that will be gathered along the specified axis.

  • axis (Union[int, Container], default: -1) – The axis from which the indices will be gathered. Default is -1.

  • batch_dims (Union[int, Container], default: 0) – optional int, lets you gather different items from each element of a batch.

  • key_chains (Optional[Union[List[str], Dict[str, str], Container]], default: None) – The key-chains to apply or not apply the method to. Default is None.

  • to_apply (Union[bool, Container], default: True) – If True, the method will be applied to key_chains, otherwise key_chains will be skipped. Default is True.

  • prune_unapplied (Union[bool, Container], default: False) – Whether to prune key_chains for which the function was not applied. Default is False.

  • map_sequences (Union[bool, Container], default: False) – Whether to also map method to sequences (lists, tuples). Default is False.

  • out (Optional[Container], default: None) – Optional array, for writing the result to. It must have a shape that the inputs broadcast to.

Return type:

Container

Returns:

ret – New container with the values gathered at the specified indices along the specified axis.

Examples

>>> x = ivy.Container(a = ivy.array([0., 1., 2.]),
...                   b = ivy.array([4., 5., 6.]))
>>> y = ivy.Container(a = ivy.array([0, 1]),
...                   b = ivy.array([1, 2]))
>>> z = x.gather(y)
>>> print(z)
{
    a: ivy.array([0., 1.]),
    b: ivy.array([5., 6.])
}