Learn R Programming

ggmlR (version 0.6.1)

ag_dataloader: Create a mini-batch data loader

Description

Returns an iterator environment. Each call to $next_batch() returns a named list list(x, y) with ag_tensor objects of shape [features, batch_size] / [labels, batch_size]. After the last batch, $has_next() returns FALSE; call $reset() (or start a new epoch via $epoch()) to reshuffle and restart.

Usage

ag_dataloader(x, y = NULL, batch_size = 32L, shuffle = TRUE, col_major = TRUE)

Value

An ag_dataloader environment

Arguments

x

Feature matrix [features, n_samples] or [n_samples, features] — see col_major.

y

Label matrix with the same convention.

batch_size

Integer batch size.

shuffle

Logical; if TRUE (default) shuffle at each reset().

col_major

Logical; if TRUE (default) x and y are already [features, n] (ggml/ag convention). Set FALSE for row-major [n, features] (R/Keras convention) — they will be transposed automatically.

Examples

Run this code
# \donttest{
n  <- 128L
x  <- matrix(runif(4 * n), 4, n)   # [4, 128] col-major
y  <- matrix(runif(2 * n), 2, n)
dl <- ag_dataloader(x, y, batch_size = 32L)
dl$reset()
while (dl$has_next()) {
  batch <- dl$next_batch()
  # batch$x: [4, 32],  batch$y: [2, 32]
}
# }

Run the code above in your browser using DataLab