ggml_layer_dense: Add Dense (Fully Connected) Layer

View source: R/nn_layers.R

ggml_layer_denseR Documentation

Add Dense (Fully Connected) Layer

Description

Add Dense (Fully Connected) Layer

Usage

ggml_layer_dense(
  model,
  units,
  activation = NULL,
  input_shape = NULL,
  name = NULL,
  trainable = TRUE,
  time_distributed = FALSE
)

Arguments

model

A ggml_sequential_model object

units

Number of output units

activation

Activation function name: "relu", "sigmoid", "tanh", "softmax", or NULL

input_shape

Integer or integer vector specifying the input shape (only needed for the first layer)

name

Optional character name for the layer.

trainable

Logical; whether the layer weights are updated during training.

time_distributed

Logical; apply the kernel per position of a sequence input rather than flattening it (default FALSE). See Time-distributed application.

Value

The model object with the dense layer appended (invisibly).

Time-distributed application

By default a dense layer flattens its input, so a sequence node of shape c(seq_len, features) becomes a single vector of units and the sequence axis is gone. With time_distributed = TRUE the same kernel is applied independently at every position instead, sharing one set of weights across the sequence, and the output keeps its length: c(seq_len, units). This is the semantics of Keras' TimeDistributed, and it is what the position-wise feed-forward sublayer of a transformer block needs – without it, a dense layer after ggml_layer_attention would collapse every position into one vector.

It is computed as a single batched ggml_mul_mat() over the combined position/batch axes, not as a loop over positions.

Functional API only: a sequential model carries a flat running shape, so there is no sequence axis to distribute over.

Examples


model <- ggml_model_sequential() |>
  ggml_layer_conv_2d(32, c(3,3), activation = "relu",
                     input_shape = c(28, 28, 1)) |>
  ggml_layer_flatten() |>
  ggml_layer_dense(128, activation = "relu")

# Position-wise feed-forward, as in a transformer block.
x  <- ggml_input(shape = c(10L, 32L))
ff <- x |> ggml_layer_dense(64L, activation = "relu", time_distributed = TRUE)


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