einops_rearrange#
- ivy.einops_rearrange(x, pattern, /, *, out=None, **axes_lengths)[source]#
Perform einops rearrange operation on input array x.
- Parameters:
x (
Union
[Array
,NativeArray
]) – Input array to be re-arranged.pattern (
str
) – Rearrangement pattern.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:
- Returns:
ret – New array with einops.rearrange having been applied.
Examples
With
ivy.Array
instance method:>>> x = ivy.array([[1, 2, 3], ... [-4, -5, -6]]) >>> y = x.einops_rearrange("height width -> width height") >>> print(y) ivy.array([[ 1, -4], [ 2, -5], [ 3, -6]])
>>> x = ivy.array([[[ 1, 2, 3], ... [ 4, 5, 6]], ... [[ 7, 8, 9], ... [10, 11, 12]]]) >>> y = x.einops_rearrange("c h w -> c (h w)") >>> print(y) ivy.array([[ 1, 2, 3, 4, 5, 6], [ 7, 8, 9, 10, 11, 12]])
>>> x = ivy.array([[1, 2, 3, 4, 5, 6], ... [7, 8, 9, 10, 11, 12]]) >>> y = ivy.zeros((4,3)) >>> x.einops_rearrange("c (h w) -> (c h) w", out=y, h=2, w=3) >>> print(y) ivy.array([[ 1, 2, 3], [ 4, 5, 6], [ 7, 8, 9], [10, 11, 12]])
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]])) >>> y = ivy.einops_rearrange(x, 'a b -> b a') >>> print(y) { a: ivy.array([[-4.46999979, 3.66000009], [0.93000001, 24.29000092], [-3.33999991, 3.6400001]]), b: ivy.array([[4.96000004, 4.36000013], [1.51999998, 13.96000004], [-10.67000008, 0.30000001]]) }
With varying pattern:
Suppose we have a set of 32 images in “h w c” format (height-width-channel) and concatenate images along height (vertical axis), 960 = 32 * 30
>>> images = ivy.asarray([ivy.random_normal(shape=(30, 40, 3)) for _ in range(32)]) >>> x = ivy.einops_rearrange(images, 'b h w c -> (b h) w c') >>> print(x.shape) (960, 40, 3)
# Concatenate images along horizontal axis, 1280 = 32 * 40
>>> images = ivy.asarray([ivy.random_normal(shape=(30, 40, 3)) for _ in range(32)]) >>> x = ivy.einops_rearrange(images, 'b h w c -> h (b w) c') >>> print(x.shape) (30, 1280, 3)
# Reorder axes to “b c h w” format for deep learning
>>> images = ivy.asarray([ivy.random_normal(shape=(30, 40, 3)) for _ in range(32)]) >>> x = ivy.einops_rearrange(images, 'b h w c -> b c h w') >>> print(x.shape) (32, 3, 30, 40)
# Flatten each image into a vector, 3600 = 30 * 40 * 3
>>> images = ivy.asarray([ivy.random_normal(shape=(30, 40, 3)) for _ in range(32)]) >>> x = ivy.einops_rearrange(images, 'b h w c -> b (c h w)') >>> print(x.shape) (32, 3600)
# Split each image into 4 smaller (top-left, top-right, bottom-left, bottom-right), # 128 = 32 * 2 * 2
>>> images = ivy.asarray([ivy.random_normal(shape=(30, 40, 3)) for _ in range(32)]) >>> x = ivy.einops_rearrange(images, 'b (h1 h) (w1 w) c -> (b h1 w1) h w c', ... h1=2, w1=2) >>> print(x.shape) (128, 15, 20, 3)
# Space-to-depth operation >>> images = ivy.asarray([ivy.random_normal(shape=(30, 40, 3)) for _ in range(32)]) >>> x = ivy.einops_rearrange(images, ‘b (h h1) (w w1) c -> b h w (c h1 w1)’, h1=2, … w1=2) >>> print(x.shape) (32, 15, 20, 12)
- Array.einops_rearrange(self, pattern, /, *, out=None, **axes_lengths)[source]#
ivy.Array instance method variant of ivy.einops_rearrange. This method simply wraps the function, and so the docstring for ivy.einops_rearrange also applies to this method with minimal changes.
- Parameters:
self (
Array
) – Input array to be re-arranged.pattern (
str
) – Rearrangement pattern.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.rearrange having been applied.
Examples
With
ivy.Array
instance method:>>> x = ivy.array([[1, 2, 3], ... [-4, -5, -6]]) >>> y = x.einops_rearrange("height width -> width height") >>> print(y) ivy.array([[ 1, -4], [ 2, -5], [ 3, -6]])
>>> x = ivy.array([[[ 1, 2, 3], ... [ 4, 5, 6]], ... [[ 7, 8, 9], ... [10, 11, 12]]]) >>> y = x.einops_rearrange("c h w -> c (h w)") >>> print(y) ivy.array([[ 1, 2, 3, 4, 5, 6], [ 7, 8, 9, 10, 11, 12]])
>>> x = ivy.array([[1, 2, 3, 4, 5, 6] ... [7, 8, 9, 10, 11, 12]]) >>> y = x.einops_rearrange("c (h w) -> (c h) w", h=2, w=3) ivy.array([[ 1, 2, 3], [ 4, 5, 6], [ 7, 8, 9], [10, 11, 12]])
- Container.einops_rearrange(self, pattern, /, *, 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_rearrange. This method simply wraps the function, and so the docstring for ivy.einops_rearrange also applies to this method with minimal changes.
- Parameters:
pattern (
Union
[str
,Container
]) – Rearrangement pattern.key_chains (
Optional
[Union
[List
[str
],Dict
[str
,str
],Container
]], default:None
) – The key-chains to apply or not apply the method to. Default isNone
.to_apply (
Union
[bool
,Container
], default:True
) – If True, the method will be applied to key_chains, otherwise key_chains will be skipped. Default isTrue
.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 isFalse
.axes_lengths (
Union
[Dict
[str
,int
],Container
]) – Any additional specifications for dimensions.**axes_lengths –
- Returns:
ivy.Container with each array having einops.rearrange applied.
Examples
>>> x = ivy.Container(a=ivy.array([[1, 2, 3], ... [-4, -5, -6]]), ... b=ivy.array([[7, 8, 9], ... [10, 11, 12]])) >>> y = x.einops_rearrange("height width -> width height") >>> print(y) { a: ivy.array([[1, -4], [2, -5], [3, -6]]), b: ivy.array([[7, 10], [8, 11], [9, 12]]) }
>>> x = ivy.Container(a=ivy.array([[[ 1, 2, 3], ... [ 4, 5, 6]], ... [[ 7, 8, 9], ... [10, 11, 12]]])) >>> y = x.einops_rearrange("c h w -> c (h w)") >>> print(y) { a: (<class ivy.data_classes.array.array.Array> shape=[2, 6]) }
>>> x = ivy.Container(a=ivy.array([[1, 2, 3, 4, 5, 6], ... [7, 8, 9, 10, 11, 12]])) >>> y = x.einops_rearrange("c (h w) -> (c h) w", h=2, w=3) >>> print(y) { a: (<class ivy.data_classes.array.array.Array> shape=[4, 3]) }