ggml_layer_reshape: Add Reshape Layer

View source: R/nn_layers.R

ggml_layer_reshapeR Documentation

Add Reshape Layer

Description

Reinterpret a sample's shape without moving any data. The batch axis is not part of shape and is never reshaped – shape describes one sample, exactly as input_shape does.

Usage

ggml_layer_reshape(
  model,
  shape,
  input_shape = NULL,
  name = NULL,
  trainable = TRUE
)

Arguments

model

A ggml_sequential_model object, or a ggml_tensor_node for the functional API.

shape

Integer vector, the new per-sample shape. At most one entry may be -1 to have it inferred. Must not include the batch axis.

input_shape

Input shape, required for the first layer only.

name

Optional character name for the layer.

trainable

Logical; reserved for API consistency (no weights).

Value

The model object with the reshape layer appended (invisibly), or a ggml_tensor_node in the functional API.

Inferred axis

One entry of shape may be -1, in which case it is computed from the element count: ggml_layer_reshape(c(-1, 32)) on a c(4, 8, 16) input gives c(16, 32). At most one axis may be -1, and the remaining sizes must divide the element count exactly.

Reshape versus permute

Reshape only relabels the axes; the elements keep their memory order. To actually reorder axes – swapping c(a, b) into c(b, a), say – use ggml_layer_permute. Using reshape for that silently interleaves the data instead.

Non-contiguous input

ggml_reshape_*() requires contiguous data, and a permuted tensor is not contiguous. The layer inserts ggml_cont when needed, so it works after a permute; the cost is one copy.

See Also

ggml_layer_permute to reorder axes, ggml_layer_flatten to collapse them all.

Examples


# Split a flat feature vector into a sequence of 32-wide steps.
model <- ggml_model_sequential() |>
  ggml_layer_dense(512, input_shape = 128) |>
  ggml_layer_reshape(c(-1, 32))


ggmlR documentation built on Oct. 23, 2026, 1:07 a.m.