keras-team/keras · error · ValueError

Weight count mismatch for top-level weights of model. Model

Error message

Weight count mismatch for top-level weights of model. Model expects {len(symbolic_weights)} top-level weight(s). Received {len(weight_values)} saved top-level weight(s)

What it means

Error "Weight count mismatch for top-level weights of model. Model expects {len(symbolic_weights)} top-level weight(s). Received {len(weight_values)} saved top-level weight(s)" thrown in keras-team/keras.

Source

Thrown at keras/src/legacy/saving/legacy_h5_format.py:560

        symbolic_weights = (
            model._trainable_variables + model._non_trainable_variables
        )
        weight_values = load_subset_weights_from_hdf5_group(
            safe_get_h5_group(group, "top_level_model_weights")
        )

        if len(weight_values) != len(symbolic_weights):
            if skip_mismatch:
                warnings.warn(
                    "Skipping loading top-level weights for model due to "
                    "mismatch in number of weights. "
                    f"Model expects {len(symbolic_weights)} "
                    "top-level weight(s). "
                    f"Received {len(weight_values)} saved top-level weight(s)",
                    stacklevel=2,
                )
            else:
                raise ValueError(
                    "Weight count mismatch for top-level weights of model. "
                    f"Model expects {len(symbolic_weights)} "
                    "top-level weight(s). "
                    f"Received {len(weight_values)} saved top-level weight(s)"
                )
        else:
            _set_weights(
                model,
                symbolic_weights,
                weight_values,
                skip_mismatch=skip_mismatch,
                name="top-level model",
            )


def load_subset_weights_from_hdf5_group(group):
    """Load layer weights of a model from hdf5.

View on GitHub (pinned to 7a34a03db6)

When it happens

Trigger: Thrown at keras/src/legacy/saving/legacy_h5_format.py:560 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of keras-team/keras@7a34a03db6 (2026-08-25). Data as JSON: /api/errors/167e3dd75d6456e4. Report an issue: GitHub.