Skip to contents

Reshape input x and target y. Aggregates multiple samples from x and y into single input/target batches.

Usage

reshape_tensor(
  x,
  y,
  new_batch_size,
  samples_per_target,
  buffer_len = NULL,
  reshape_mode = "time_dist",
  check_y = FALSE
)

Arguments

x

3D input tensor.

y

2D target tensor.

new_batch_size

Size of first axis of input/targets after reshaping.

samples_per_target

How many samples to use for one target

buffer_len

Only applies if reshape_mode = "concat". If buffer_len is an integer, the subsequences are interspaced with buffer_len rows. The reshaped x has new maxlen: (maxlen \(*\) samples_per_target) + buffer_len \(*\) (samples_per_target - 1).

reshape_mode

"time_dist", "multi_input" or "concat"

  • If "multi_input", will produce samples_per_target separate inputs, each of length maxlen.

  • If "time_dist", will produce a 4D input array. The dimensions correspond to (new_batch_size, samples_per_target, maxlen, length(vocabulary)).

  • If "concat", will concatenate samples_per_target sequences of length maxlen to one long sequence

check_y

Check if entries in y are consistent with reshape strategy (same label when aggregating).

Value

A list of 2 tensors.

Examples

if (FALSE) { # reticulate::py_module_available("tensorflow")
# create dummy data
batch_size <- 8
maxlen <- 11
voc_len <- 4 
x <- sample(0:(voc_len-1), maxlen*batch_size, replace = TRUE)
x <- keras::to_categorical(x, num_classes = voc_len)
x <- array(x, dim = c(batch_size, maxlen, voc_len))
y <- rep(0:1, each = batch_size/2)
y <- keras::to_categorical(y, num_classes = 2)
y

# reshape data for multi input model
reshaped_data <- reshape_tensor(
  x = x,
  y = y,
  new_batch_size = 2,
  samples_per_target = 4,
  reshape_mode = "multi_input")

length(reshaped_data[[1]])
dim(reshaped_data[[1]][[1]])
reshaped_data[[2]]
}