Saves all weights to a single file or sharded files.
Source:R/model-persistence.R
save_model_weights.Rd
By default, the weights are saved in a single .weights.h5
file. Enable
sharding via max_shard_size
to split weights across multiple files (in GB)
and produce a .weights.json
manifest that tracks shard metadata.
The saved sharded files contain:
*.weights.json
: configuration file containingmetadata
andweight_map
entries.*_xxxxxx.weights.h5
: weight shards limited bymax_shard_size
.
model <-
keras_model_sequential(input_shape = 2) |>
layer_dense(4)
path_h5 <- tempfile(fileext = ".weights.h5")
path_json <- tempfile(fileext = ".weights.json")
model |> save_model_weights(path_h5)
model |> save_model_weights(path_json, max_shard_size = 0.01)
model |> load_model_weights(path_h5)
model |> load_model_weights(path_json)
Arguments
- model
A keras Model object.
- filepath
Path where the weights will be saved. Accepts
.weights.h5
, or when sharding is enabled, a.weights.json
manifest path. If.weights.h5
is provided while sharding, the filename will be overridden to end in.weights.json
.- overwrite
Whether to overwrite any existing weights at the target location, or instead ask the user via an interactive prompt.
- max_shard_size
Numeric size in GB for each sharded file. Use
NULL
to disable sharding.
Value
This is called primarily for side effects. model
is returned,
invisibly, to enable usage with the pipe.
See also
Other saving and loading functions: export_savedmodel.keras.src.models.model.Model()
layer_tfsm()
load_model()
load_model_weights()
register_keras_serializable()
save_model()
save_model_config()
with_custom_object_scope()