Combine multiple ranks of a tensor into a single rank, for example for use in data augmentation.

combine_ranks(x, ..., new_rank_name = NULL, .dots = NULL)

Arguments

x

the tidytensor to combine ranks for.

...

ranknames or integers to combine (quoted or unquoted).

new_rank_name

Name to give the newly combined rank; by default the new rank name is constructed from the names of the combined ranks.

.dots

character or integer vector of ranknames.

Value

a new tidytensor.

Details

If all ranks being combined have dimension names, the dimension names of the newly produced rank will be combinations of those specified.

It is only possible to combine consecutive ranks; use permute() to first organize ranks.

See also

Examples

# shape [5, 20, 26, 26] for 5 batches of 20 26x26 "images"
t <- as.tidytensor(array(rnorm(5 * 20 * 26 * 26), dim = c(5, 20, 26, 26)))
ranknames(t) <- c("batch", "image", "row", "col")

# given an image tidytensor (26x26), return a set of replicates with noise added
make_noisy_images <- function(t2) {
  res <- bind(t2,
              t2 + rnorm(length(t2)),
              t2 + rnorm(length(t2)),
              t2 + rnorm(length(t2)), new_rank_name = "replicate")
}

# augment the original data by replacing each image with a set of
# noisy replicates
t <- tt_apply(t, image, make_noisy_images)

# now t is shape (5, 20, 4, 26, 26)
# with ranknames (batch, image, replicate, row, col)
# let's set some dimension names

# setting to "1", "2", "3", ...
t <- set_dimnames_for_rank(t, image, .dots = 1:20)

# setting to "original", "rep1", "rep2", "rep3"
t <- set_dimnames_for_rank(t, replicate, original, rep1, rep2, rep3)

# to make it compatible with the original shape we
# combine images and replicates
t2 <- combine_ranks(t, image, replicate)

print(t2)
#> # Rank 4 tensor, shape: (5, 80, 26, 26), ranknames: batch, image_replicate, row, col
#> |  # Rank 3 tensor, shape: (80, 26, 26)
#> |  |  # Rank 2 tensor, shape: (26, 26)
#> |  |       -1.26   0.746    0.473  -0.399   0.609  -0.801  ... 
#> |  |        -1.1    0.75    -0.52  -0.345   -0.81    1.87  ... 
#> |  |       0.805  -0.229  -0.0231   0.896    2.16   0.548  ... 
#> |  |       0.183   0.223    -1.25    1.58  0.0841    -1.3  ... 
#> |  |      -0.859  0.0471     0.52   0.505    1.12  -0.326  ... 
#> |  |        1.62   0.187    0.594   0.364  -0.895    1.12  ... 
#> |  |         ...     ...      ...     ...     ...     ...  ... 
#> |  |  # ...
#> |  # ...

# since the combined ranks both have dimension names, the newly
# created rank does as well and we can verify contents
# here we see that the second batch, image 3, replicate 2 is indeed the same
print(t[2, "3", "rep2", , ])
#> # Rank 2 tensor, shape: (26, 26), ranknames: row, col
#>     0.334  -1.49    1.03   -2.91    1.98    1.49  ... 
#>      1.56   1.35    1.05  -0.393    1.27  -0.351  ... 
#>      3.71  0.748    1.52   0.772   -1.83  -0.199  ... 
#>     0.448  0.179   -1.44   0.395    2.97    1.03  ... 
#>      2.31   1.09  -0.211   0.271  -0.418   -5.29  ... 
#>     0.757  0.571  -0.018    -1.1    1.49    1.68  ... 
#>       ...    ...     ...     ...     ...     ...  ... 
print(t2[2, "3_rep2", , ])
#> # Rank 2 tensor, shape: (26, 26), ranknames: row, col
#>     0.334  -1.49    1.03   -2.91    1.98    1.49  ... 
#>      1.56   1.35    1.05  -0.393    1.27  -0.351  ... 
#>      3.71  0.748    1.52   0.772   -1.83  -0.199  ... 
#>     0.448  0.179   -1.44   0.395    2.97    1.03  ... 
#>      2.31   1.09  -0.211   0.271  -0.418   -5.29  ... 
#>     0.757  0.571  -0.018    -1.1    1.49    1.68  ... 
#>       ...    ...     ...     ...     ...     ...  ...