rearrange | R Documentation |
This operation includes functionality of transpose (axes permutation), reshape (view), squeeze, unsqueeze, stack, concatenate and other operations.
rearrange(x, expr, ..., .row_major = getOption("einops_row_major", FALSE))
einops.rearrange(
x,
expr,
...,
.row_major = getOption("einops_row_major", FALSE)
)
When composing axes, C-order enumeration is used (consecutive elements have different last axis). Find more examples in the vignettes.
tensor of the same type as input, with dimensions according to output pattern
if (requireNamespace("abind", quietly = TRUE)) {
# suppose we have a set of 32 images in "h w c" format (height-width-channel)
images <- lapply(1:32, function(i) {
as_image_tensor(array(rnorm(30*40*3), dim = c(30, 40, 3)))
})
# stacked and reordered axes to "b c h w" format
y <- rearrange(images, 'b h w c -> b c h w')
# concatenate images along height (vertical axis), 960 = 32 * 30
y <- rearrange(images, 'b h w c -> (b h) w c')
# concatenated images along horizontal axis, 1280 = 32 * 40
y <- rearrange(images, 'b h w c -> h (b w) c')
# flattened each image into a vector, 3600 = 30 * 40 * 3
y <- rearrange(images, 'b h w c -> b (c h w)')
# split each image into 4 smaller quadrants, 128 = 32 * 2 * 2
y <- rearrange(
images, 'b (h1 h) (w1 w) c -> (b h1 w1) h w c', h1 = 2, w1 = 2
)
# space-to-depth operation
y <- rearrange(
images, 'b (h h1) (w w1) c -> b h w (c h1 w1)', h1 = 2, w1 = 2
)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.