Description Usage Arguments Details Value See Also Examples
View source: R/combine_ranks.R
Combine multiple ranks of a tensor into a single rank, for example for use in data augmentation.
1 | combine_ranks(x, ..., new_rank_name = NULL, .dots = NULL)
|
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. |
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.
a new tidytensor.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | # 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)
# 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", , ])
print(t2[2, "3_rep2", , ])
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.