einops
pip install einopsbash
operations
from einops import rearrange, reduce
from einops.layers.torch import Rearrange, Reduce
y = rearrange(x, 'b c h w -> b h w c')
y = x.permute(0, 2, 3, 1).contiguous()
y = rearrange(x, 'b h w c -> (b h) w c')
y = x.view(B*H, W, C)
y = rearrange(x, '(b h) w c -> b h w c')
y = x.view(B, H, W, C)
y = rearrange(x, '(b1 b2) h w c -> h (b1 b2 w) c')
y = rearrange(x, '(b1 b2) h w c -> h (b2 b1 w) c')
y = reduce(x, 'b h w c -> h w c', 'mean')
y = x.mean(dim=0)
y = reduce(x, 'b (h h2) (w w2) c -> b h w c', 'min', h2=2, w2=2)
x = rearrange(xs, 'b h w c -> b h w c')
x = torch.stack(xs, dim=0)
x = rearrange(xs, 'b h w c -> h w c b')
x = torch.stack(xs, dim=-1)
x = rearrange(x, 'h w c -> 1 h w c')
x = x.unsqueeze(0)
x = rearrange(x, '1 h w c -> h w c')
x = x.squeeze(0)
python