tt_apply: Apply a function over lower ranks of a tidytensor

Description Usage Arguments Details Value See Also Examples

View source: R/tt_apply.tidytensor.R

Description

Applies a function over the lower ranks of a tidytensor, collecting the results into a tidytensor. For example, if FUN is a function that takes a tidytensor of shape [26, 26] and returns a tidytensor of shape [13, 13], then we could apply FUN on a tidytensor of shape [3, 100, 26, 26] starting at rank 2 to get back one with shape [3, 100, 13, 13]. If flatten = TRUE, the higher ranks are collapsed to produce shape [300, 26, 26]

Ranknames are respected for both inputs and return values.

Usage

1
tt_apply(x, rank = 1, FUN, flatten = FALSE, drop_final_1 = TRUE, ...)

Arguments

x

the tidytensor to apply over.

rank

an indicator of the rank to apply over (see details).

FUN

the function to apply

flatten

whether to preserve the higher-rank structure, or collapse into a single rank (see description).

drop_final_1

If FUN returns a rank-0 tensor (length-1 vector), should it be collapsed? E.g. if final shape is (10, 10, 1), adjusts shape to (10, 10)

...

additional arguments passed to FUN.

Details

The rank argument should specify a single rank to apply over; if ranknames(t) <- c("sample", "rows", "cols", "channels") then rank = 2, rank = "rows", and rank = c(FALSE, TRUE, FALSE, FALSE) all indicate that FUN will be called on tidytensors with ranknames c("rows", "cols", "channels").

Value

a new tidytensor.

See Also

permute

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
# shape [20, 26, 26]
t <- as.tidytensor(array(rnorm(20 * 26 * 26), dim = c(20, 26, 26)))
ranknames(t) <- c("sample", "row", "col")
print(t)

# compute the deviation from median for each sample
dev_median <- function(t) {
  return(t - median(t))
}

median_deviations <- tt_apply(t, sample, dev_median)
print(median_deviations)

oneilsh/tidytensor documentation built on Oct. 11, 2021, 11:43 p.m.