ggml_compile: Compile a Sequential Model

View source: R/nn_model.R

ggml_compile.ggml_functional_modelR Documentation

Compile a Sequential Model

Description

Configures the model for training: infers shapes, creates backend. Weight tensors are created at training time when batch_size is known.

Usage

## S3 method for class 'ggml_functional_model'
ggml_compile(
  model,
  optimizer = "adam",
  loss = "categorical_crossentropy",
  metrics = c("accuracy"),
  backend = "auto",
  loss_weights = NULL,
  weight_decay = 0,
  predict_precision = "default"
)

ggml_compile(
  model,
  optimizer = "adam",
  loss = "categorical_crossentropy",
  metrics = c("accuracy"),
  backend = "auto",
  loss_weights = NULL,
  weight_decay = 0,
  predict_precision = "default"
)

## S3 method for class 'ggml_sequential_model'
ggml_compile(
  model,
  optimizer = "adam",
  loss = "categorical_crossentropy",
  metrics = c("accuracy"),
  backend = "auto",
  loss_weights = NULL,
  weight_decay = 0,
  predict_precision = "default"
)

Arguments

model

A ggml_sequential_model object

optimizer

Optimizer name: "adam" or "sgd"

loss

Loss function name: "categorical_crossentropy" (expects a softmax output), "mse", "mae", "huber" (smooth L1, delta 1) or "binary_crossentropy" (expects a sigmoid output, one independent label per unit). "mean_squared_error", "mean_absolute_error", "huber_loss" and "binary_cross_entropy" are accepted as aliases. For a multi-output functional model, a list or character vector with one loss per output – named after the output layers, or positional: loss = list(policy = "categorical_crossentropy", value = "mse").

metrics

Character vector of metrics to report from ggml_evaluate. "accuracy" is reported for multi-class outputs whether or not it is requested; "mae", "mse" and "rmse" (and the long forms "mean_absolute_error", "mean_squared_error") are computed only when asked for. Any other name is dropped with a warning.

backend

Backend to use: "auto" (GPU if available, else CPU), "cpu", or "vulkan"

loss_weights

Optional per-output weights for a multi-output model; the optimized total is sum(loss_weights[i] * loss_i). Named after the output layers or positional. Default: all outputs weighted 1.

weight_decay

Decoupled weight decay for the optimizer, in [0, 1]; 0 (the default) turns it off. It is applied to trainable weights only: layers frozen with ggml_freeze_weights are not decayed.

predict_precision

Matrix-multiply precision for prediction: "default" (as ggml builds it; f16 tiles on an fp16 Vulkan device) or "f32". Used by ggml_predict, a trainer's $predict() and its snapshots unless a call gives its own matmul_precision; training is unaffected. See the precision section of ggml_predict.

Value

The compiled model (invisibly).

Examples


model <- ggml_model_sequential() |>
  ggml_layer_conv_2d(32, c(3,3), activation = "relu",
                     input_shape = c(28, 28, 1)) |>
  ggml_layer_max_pooling_2d(c(2, 2)) |>
  ggml_layer_flatten() |>
  ggml_layer_dense(10, activation = "softmax")
model <- ggml_compile(model, optimizer = "adam",
                      loss = "categorical_crossentropy")


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