| ggml_layer_embedding | R Documentation |
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).
ggml_layer_embedding(
model,
vocab_size,
dim,
name = NULL,
trainable = TRUE,
input_shape = NULL
)
model |
A |
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
|
The model with the embedding layer appended, or a new tensor node.
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.
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).
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)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.