[02ea2d]: / man / reshape_tensor.Rd

Download this file

71 lines (62 with data), 2.3 kB

 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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/preprocess.R
\name{reshape_tensor}
\alias{reshape_tensor}
\title{Reshape tensors for set learning}
\usage{
reshape_tensor(
x,
y,
new_batch_size,
samples_per_target,
buffer_len = NULL,
reshape_mode = "time_dist",
check_y = FALSE
)
}
\arguments{
\item{x}{3D input tensor.}
\item{y}{2D target tensor.}
\item{new_batch_size}{Size of first axis of input/targets after reshaping.}
\item{samples_per_target}{How many samples to use for one target}
\item{buffer_len}{Only applies if \code{reshape_mode = "concat"}. If \code{buffer_len} is an integer, the subsequences are interspaced with \code{buffer_len} rows. The reshaped x has
new maxlen: (\code{maxlen} \eqn{*} \code{samples_per_target}) + \code{buffer_len} \eqn{*} (\code{samples_per_target} - 1).}
\item{reshape_mode}{\verb{"time_dist", "multi_input"} or \code{"concat"}
\itemize{
\item If \code{"multi_input"}, will produce \code{samples_per_target} separate inputs, each of length \code{maxlen}.
\item If \code{"time_dist"}, will produce a 4D input array. The dimensions correspond to
\verb{(new_batch_size, samples_per_target, maxlen, length(vocabulary))}.
\item If \code{"concat"}, will concatenate \code{samples_per_target} sequences of length \code{maxlen} to one long sequence
}}
\item{check_y}{Check if entries in \code{y} are consistent with reshape strategy (same label when aggregating).}
}
\value{
A list of 2 tensors.
}
\description{
Reshape input x and target y. Aggregates multiple samples from x and y into single input/target batches.
}
\examples{
\dontshow{if (reticulate::py_module_available("tensorflow")) (if (getRversion() >= "3.4") withAutoprint else force)(\{ # examplesIf}
# 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]]
\dontshow{\}) # examplesIf}
}