Load the weights from a single file or sharded files.
Source:R/model-persistence.R
load_model_weights.Rd
Weights are loaded based on the network's topology. This means the architecture should be the same as when the weights were saved. Note that layers that don't have weights are not taken into account in the topological ordering, so adding or removing layers is fine as long as they don't have weights.
Sharding
When loading sharded weights, specify a filepath
ending in
".weights.json"
(the configuration file), with the corresponding shard files
(*_xxxxx.weights.h5
) located alongside it.
Partial weight loading
If you have modified your model, for instance by adding a new layer
(with weights) or by changing the shape of the weights of a layer,
you can choose to ignore errors and continue loading
by setting skip_mismatch=TRUE
. In this case any layer with
mismatching weights will be skipped. A warning will be displayed
for each skipped layer.
Arguments
- model
A keras model.
- filepath
Path or path-like object to the weights. Accepts
.weights.h5
, legacy.h5
, or sharded weights through a.weights.json
manifest sitting alongside the shard files (*_xxxxx.weights.h5
).- skip_mismatch
Boolean, whether to skip loading of layers where there is a mismatch in the number of weights, or a mismatch in the shape of the weights.
- ...
For forward/backward compatability.
Value
This is called primarily for side effects. model
is returned,
invisibly, to enable usage with the pipe.
Examples
model |> load_model_weights("model.weights.h5")
model |> load_model_weights("model.weights.json")
See also
Other saving and loading functions: export_savedmodel.keras.src.models.model.Model()
layer_tfsm()
load_model()
register_keras_serializable()
save_model()
save_model_config()
save_model_weights()
with_custom_object_scope()