ggml_fit_opt: Fit model with R-side epoch loop and callbacks

View source: R/optimizer.R

ggml_fit_optR Documentation

Fit model with R-side epoch loop and callbacks

Description

Trains a model epoch by epoch in R, allowing callbacks for early stopping and learning rate scheduling. Optimizer state (momentum) is preserved across all epochs.

Usage

ggml_fit_opt(
  sched,
  ctx_compute,
  inputs,
  outputs,
  dataset,
  loss_type = ggml_opt_loss_type_mse(),
  optimizer = ggml_opt_optimizer_type_adamw(),
  nepoch = 10L,
  nbatch_logical = 32L,
  val_split = 0,
  shuffle = TRUE,
  shuffle_all = shuffle,
  callbacks = list(),
  silent = FALSE,
  loss_mask_ne0 = NULL,
  weight_decay = 0
)

Arguments

sched

Backend scheduler

ctx_compute

Compute context (for temporary tensors)

inputs

Input tensor with shape [ne_datapoint, batch_size]

outputs

Output tensor with shape [ne_label, batch_size]

dataset

Dataset created with 'ggml_opt_dataset_init()'

loss_type

Loss type (default: MSE)

optimizer

Optimizer type (default: AdamW)

nepoch

Number of epochs

nbatch_logical

Logical batch size (for gradient accumulation)

val_split

Fraction of data for validation (0.0 to 1.0)

shuffle

Shuffle the training portion each epoch (default TRUE). Set to FALSE for time series or to make a run exactly reproducible.

shuffle_all

Shuffle the whole dataset once before the train/validation split is taken, so the validation portion is a random sample rather than the tail of the input. Defaults to shuffle. Callers that append an explicit validation set to the data pass FALSE, since there the split is positional and must be preserved.

callbacks

List of callback lists. Each element may have 'on_epoch_begin(epoch, logs, state)' and/or 'on_epoch_end(epoch, logs, state)'. Built-in factories: 'ggml_callback_early_stopping()', 'ggml_schedule_step_decay()', 'ggml_schedule_cosine_decay()', 'ggml_schedule_reduce_on_plateau()'. 'state' is a mutable environment with fields: 'stop' (set TRUE to stop training), 'lr_ud', 'nepoch'.

silent

Whether to suppress per-epoch progress output

loss_mask_ne0

Width of the weighted-MSE weight tensor: NULL or 1 for per-datapoint weights, or the output width for a per-output mask. Ignored by every other loss type.

weight_decay

Decoupled weight decay in [0, 1], as for ggml_opt_init_for_fit; 0 turns it off.

Value

Data frame with columns epoch, train_loss, train_accuracy, val_loss, val_accuracy

See Also

Other optimization: ggml_fit_opt_gen(), ggml_fit_opt_multi(), ggml_opt_alloc(), ggml_opt_context_optimizer_type(), ggml_opt_dataset_data(), ggml_opt_dataset_free(), ggml_opt_dataset_get_batch(), ggml_opt_dataset_get_batch_head(), ggml_opt_dataset_init(), ggml_opt_dataset_labels(), ggml_opt_dataset_ndata(), ggml_opt_dataset_shuffle(), ggml_opt_dataset_weights(), ggml_opt_default_params(), ggml_opt_epoch(), ggml_opt_eval(), ggml_opt_fit(), ggml_opt_fit_multi(), ggml_opt_free(), ggml_opt_get_lr(), ggml_opt_grad_acc(), ggml_opt_init(), ggml_opt_init_for_fit(), ggml_opt_init_for_fit_multi(), ggml_opt_inputs(), ggml_opt_labels(), ggml_opt_loss(), ggml_opt_loss_i(), ggml_opt_loss_type_binary_cross_entropy(), ggml_opt_loss_type_cross_entropy(), ggml_opt_loss_type_huber(), ggml_opt_loss_type_mae(), ggml_opt_loss_type_mean(), ggml_opt_loss_type_mse(), ggml_opt_loss_type_sum(), ggml_opt_loss_type_weighted_mse(), ggml_opt_n_loss(), ggml_opt_ncorrect(), ggml_opt_optimizer_name(), ggml_opt_optimizer_type_adamw(), ggml_opt_optimizer_type_sgd(), ggml_opt_outputs(), ggml_opt_outputs_i(), ggml_opt_pred(), ggml_opt_prepare_alloc(), ggml_opt_reset(), ggml_opt_result_accuracy(), ggml_opt_result_free(), ggml_opt_result_init(), ggml_opt_result_loss(), ggml_opt_result_loss_i(), ggml_opt_result_n_loss(), ggml_opt_result_ndata(), ggml_opt_result_pred(), ggml_opt_result_reset(), ggml_opt_set_lr(), ggml_opt_static_graphs(), ggml_snapshot_free(), ggml_trainer()

Examples

if (FALSE) {
history <- ggml_fit_opt(sched, ctx_compute, inputs, outputs, dataset,
  nepoch = 50, val_split = 0.2,
  callbacks = list(
    ggml_callback_early_stopping(monitor = "val_loss", patience = 5),
    ggml_schedule_cosine_decay()
  ))
}

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