keras/src/saving/saving_lib.py/save_weights_only
def save_weights_only(model, filepath, objects_to_skip=None):
    """Save only the weights of a model to a target filepath (.weights.h5).

    Note: only supports h5 for now.
    """
    # TODO: if h5 filepath is remote, create the file in a temporary directory
    # then upload it
    filepath = str(filepath)
    if not filepath.endswith(".weights.h5"):
        raise ValueError(
            "Invalid `filepath` argument: expected a `.weights.h5` extension. "
            f"Received: filepath={filepath}"
        )
    weights_store = H5IOStore(filepath, mode="w")
    if objects_to_skip is not None:
        visited_saveables = set(id(o) for o in objects_to_skip)
    else:
        visited_saveables = set()
    _save_state(
        model,
        weights_store=weights_store,
        assets_store=None,
        inner_path="",
        visited_saveables=visited_saveables,
    )
    weights_store.close()