einops_reduce#

ivy.einops_reduce(x, pattern, reduction, /, *, out=None, **axes_lengths)[source]#

Perform einops reduce operation on input array x.

Parameters:
  • x (Union[Array, NativeArray]) – Input array to be reduced.

  • pattern (str) – Reduction pattern.

  • reduction (Union[str, Callable]) – One of available reductions (‘min’, ‘max’, ‘sum’, ‘mean’, ‘prod’), or callable.

  • axes_lengths (Dict[str, int]) – Any additional specifications for dimensions.

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

Return type:

Array

Returns:

  • ret – New array with einops.reduce having been applied.

  • This function is *nestable*, and therefore also accepts (code:’ivy.Container’)

  • instance in place of the argument.

Examples

With ivy.Array input:

>>> x = ivy.array([[-4.47, 0.93, -3.34],
...                [3.66, 24.29, 3.64]])
>>> reduced = ivy.einops_reduce(x, 'a b -> b', 'mean')
>>> print(reduced)
ivy.array([-0.40499985, 12.61000061, 0.1500001 ])

With ivy.Container input:

>>> x = ivy.Container(a=ivy.array([[-4.47, 0.93, -3.34],
...                                [3.66, 24.29, 3.64]]),
...                   b=ivy.array([[4.96, 1.52, -10.67],
...                                [4.36, 13.96, 0.3]]))
>>> reduced = ivy.einops_reduce(x, 'a b -> a', 'mean')
>>> print(reduced)
{
    a: ivy.array([-2.29333329, 10.53000069]),
    b: ivy.array([-1.39666676, 6.20666695])
}
Array.einops_reduce(self, pattern, reduction, /, *, out=None, **axes_lengths)[source]#

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

Parameters:
  • self (Array) – Input array to be reduced.

  • pattern (str) – Reduction pattern.

  • reduction (Union[str, Callable]) – One of available reductions (‘min’, ‘max’, ‘sum’, ‘mean’, ‘prod’), or callable.

  • axes_lengths (Dict[str, int]) – Any additional specifications for dimensions.

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

Return type:

Array

Returns:

ret – New array with einops.reduce having been applied.

Examples

With ivy.Array inputs:

>>> x = ivy.array([[[5, 4],
...                 [11, 2]],
...                [[3, 5],
...                 [9, 7]]])
>>> reduced = x.einops_reduce('a b c -> b c', 'max')
>>> print(reduced)
ivy.array([[ 5,  5],
           [11,  7]])

With ivy.Array inputs:

>>> x = ivy.array([[[5, 4, 3],
...                 [11, 2, 9]],
...                [[3, 5, 7],
...                 [9, 7, 1]]])
>>> reduced = x.einops_reduce('a b c -> a () c', 'min')
>>> print(reduced)
ivy.array([[[5, 2, 3]],
           [[3, 5, 1]]])
Container.einops_reduce(self, pattern, reduction, /, *, key_chains=None, to_apply=True, prune_unapplied=False, map_sequences=False, out=None, **axes_lengths)[source]#

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

Parameters:
  • self (Container) – Input container to be reduced.

  • pattern (Union[str, Container]) – Reduction pattern.

  • reduction (Union[str, Callable, Container]) – One of available reductions (‘min’, ‘max’, ‘sum’, ‘mean’, ‘prod’), or callable.

  • 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 output container, for writing the result to. It must have a shape that the inputs broadcast to.

  • axes_lengths (Union[Dict[str, int], Container]) – Any additional specifications for dimensions.

Return type:

Container

Returns:

ret – New container with einops.reduce having been applied.

Examples

>>> x = ivy.Container(a=ivy.array([[[5, 4, 3],
...                                 [11, 2, 9]],
...                                [[3, 5, 7],
...                                 [9, 7, 1]]]),
...                    b=ivy.array([[[9,7,6],
...                                  [5,2,1]],
...                                 [[4,1,2],
...                                  [2,3,6]],
...                                 [[1, 9, 6],
...                                  [0, 2, 1]]]))
>>> reduced = x.einops_reduce('a b c -> a b', 'sum')
>>> print(reduced)
{
    a: ivy.array([[12, 22],
                [15, 17]]),
    b: ivy.array([[22, 8],
                [7, 11],
                [16, 3]])
}