ggml_layer_embedding: Add Embedding Layer

View source: R/nn_layers.R

ggml_layer_embeddingR Documentation

Add Embedding Layer

Description

Looks up dense vectors for integer token indices. The input must be an integer matrix of 0-based indices in [0, vocab_size - 1] (use ggml_input(shape, dtype = "int32") in Functional mode).

Usage

ggml_layer_embedding(
  model,
  vocab_size,
  dim,
  name = NULL,
  trainable = TRUE,
  input_shape = NULL
)

Arguments

model

A ggml_sequential_model or ggml_tensor_node.

vocab_size

Number of distinct tokens (vocabulary size).

dim

Embedding dimension (vector length per token).

name

Optional layer name.

trainable

Logical; whether embedding weights are updated during training.

input_shape

Sequence length, for an embedding that is the first layer of a sequential model (its input is then a matrix of token indices, one row per sample). Ignored in the functional API, where ggml_input(dtype = "int32") gives the shape.

Value

The model with the embedding layer appended, or a new tensor node.

Axis order

The layer's output shape is c(seq_len, dim) – one row per position, as in Keras – which is the same convention a declared sequence input (ggml_input(shape = c(seq_len, features))) uses. So the output feeds ggml_layer_attention, ggml_layer_gru and ggml_layer_lstm directly, with no axis juggling in between.

The underlying tensor is [dim, seq_len, N]: ggml is column-major, so an R shape c(a, b) is the tensor [b, a, N] throughout the package. That matters only when reading raw output tensors – ggml_layer_flatten() gives the same vector either way.

Index validation

Indices must be in [0, vocab_size - 1]. Out-of-range values cause undefined behaviour inside the ggml kernel (no bounds check is performed at the R level).

Examples


inp <- ggml_input(shape = 10L, dtype = "int32")
out <- inp |>
  ggml_layer_embedding(vocab_size = 1000L, dim = 32L) |>
  ggml_layer_flatten() |>
  ggml_layer_dense(10L, activation = "softmax")
model <- ggml_model(inputs = inp, outputs = out)


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