braincore.math.ein_reduce#
- braincore.math.ein_reduce(tensor, pattern, reduction, **axes_lengths)#
ein_reduceprovides combination of reordering and reduction using reader-friendly notation.Examples for reduce operation:
```python >>> x = np.random.randn(100, 32, 64)
# perform max-reduction on the first axis >>> y = ein_reduce(x, ‘t b c -> b c’, ‘max’)
# same as previous, but with clearer axes meaning >>> y = ein_reduce(x, ‘time batch channel -> batch channel’, ‘max’)
>>> x = np.random.randn(10, 20, 30, 40)
# 2d max-pooling with kernel size = 2 * 2 for image processing >>> y1 = ein_reduce(x, ‘b c (h1 h2) (w1 w2) -> b c h1 w1’, ‘max’, h2=2, w2=2)
# if one wants to go back to the original height and width, depth-to-space trick can be applied >>> y2 = ein_rearrange(y1, ‘b (c h2 w2) h1 w1 -> b c (h1 h2) (w1 w2)’, h2=2, w2=2) >>> assert ein_shape(x, ‘b _ h w’) == ein_shape(y2, ‘b _ h w’)
# Adaptive 2d max-pooling to 3 * 4 grid >>> ein_reduce(x, ‘b c (h1 h2) (w1 w2) -> b c h1 w1’, ‘max’, h1=3, w1=4).shape (10, 20, 3, 4)
# Global average pooling >>> ein_reduce(x, ‘b c h w -> b c’, ‘mean’).shape (10, 20)
# Subtracting mean over batch for each channel >>> y = x - ein_reduce(x, ‘b c h w -> () c () ()’, ‘mean’)
# Subtracting per-image mean for each channel >>> y = x - ein_reduce(x, ‘b c h w -> b c () ()’, ‘mean’)
- Parameters:
tensor (
Union[Array,ndarray,bool_,number,bool,int,float,complex,List[Union[Array,ndarray,bool_,number,bool,int,float,complex]]]) – tensor: 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 shapepattern (
str) – string, reduction patternreduction (
Union[str,Callable[[Union[Array,ndarray,bool_,number,bool,int,float,complex],Tuple[int,...]],Union[Array,ndarray,bool_,number,bool,int,float,complex]]]) – one of available reductions (‘min’, ‘max’, ‘sum’, ‘mean’, ‘prod’), case-sensitive alternatively, a callable f(tensor, reduced_axes) -> tensor can be provided. This allows using various reductions, examples: np.max, tf.reduce_logsumexp, torch.var, etc.axes_lengths (
int) – any additional specifications for dimensions
- Return type:
Union[Array,ndarray,bool_,number,bool,int,float,complex]- Returns:
tensor of the same type as input