ag_save_model: Save an autograd module's state to disk

View source: R/ag_save.R

ag_save_modelR Documentation

Save an autograd module's state to disk

Description

Serializes the trainable parameters and persistent buffers of an ag_sequential (or single ag_* layer) module as a portable state dictionary of plain numeric matrices. This avoids serializing the live module (environments + closures), which is brittle across ggmlR versions and carries non-portable GPU pointers.

Usage

ag_save_model(model, path, model_fn = NULL)

Arguments

model

An ag_sequential module or a single ag_* layer exposing parameters().

path

File path to write (an RDS container).

model_fn

Optional zero-argument function that rebuilds the module architecture (fresh, untrained). If supplied, it is stored in the container so ag_load_model can rebuild without arguments. Must not capture GPU tensors in its enclosing environment.

Details

Reconstruction requires the architecture. Either pass model_fn here so it is stored in the file, or pass it later to ag_load_model.

Value

path, invisibly.

See Also

ag_load_model

Examples


build <- function() ag_sequential(ag_linear(4L, 8L), ag_linear(8L, 3L))
model <- build()
ag_save_model(model, tempfile(fileext = ".rds"), model_fn = build)


ggmlR documentation built on July 14, 2026, 1:08 a.m.