braincore.math.ein_repeat

Contents

braincore.math.ein_repeat#

braincore.math.ein_repeat(tensor, pattern, **axes_lengths)#

ein_repeat allows reordering elements and repeating them in arbitrary combinations. This operation includes functionality of repeat, tile, broadcast functions.

Examples for repeat operation:

```python # a grayscale image (of shape height x width) >>> image = np.random.randn(30, 40)

# change it to RGB format by repeating in each channel >>> ein_repeat(image, ‘h w -> h w c’, c=3).shape (30, 40, 3)

# repeat image 2 times along height (vertical axis) >>> ein_repeat(image, ‘h w -> (repeat h) w’, repeat=2).shape (60, 40)

# repeat image 2 time along height and 3 times along width >>> ein_repeat(image, ‘h w -> (h2 h) (w3 w)’, h2=2, w3=3).shape (60, 120)

# convert each pixel to a small square 2x2. Upsample image by 2x >>> ein_repeat(image, ‘h w -> (h h2) (w w2)’, h2=2, w2=2).shape (60, 80)

# pixelate image first by downsampling by 2x, then upsampling >>> downsampled = ein_reduce(image, ‘(h h2) (w w2) -> h w’, ‘mean’, h2=2, w2=2) >>> ein_repeat(downsampled, ‘h w -> (h h2) (w w2)’, h2=2, w2=2).shape (30, 40)

```

When composing axes, C-order enumeration used (consecutive elements have different last axis) Find more examples in einops tutorial.

Parameters:
  • tensor (Union[Array, ndarray, bool_, number, bool, int, float, complex, List[Union[Array, ndarray, bool_, number, bool, int, float, complex]]]) – tensor of any supported library (e.g. numpy.ndarray, tensorflow, pytorch). list of tensors is also accepted, those should be of the same type and shape

  • pattern (str) – string, rearrangement pattern

  • axes_lengths – any additional specifications for dimensions

Return type:

Union[Array, ndarray, bool_, number, bool, int, float, complex]

Returns:

Tensor of the same type as input. If possible, a view to the original tensor is returned.