{"record":{"id":"58a543b3687b9d1d","repo":"microsoft/qlib","slug":"the-length-of-sub-weights-should-be-equal-to-num-m","errorCode":null,"errorMessage":"The length of sub_weights should be equal to num_models.","messagePattern":"The length of sub_weights should be equal to num_models\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"qlib/contrib/model/double_ensemble.py","lineNumber":53,"sourceCode":"    ):\n        self.base_model = base_model  # \"gbm\" or \"mlp\", specifically, we use lgbm for \"gbm\"\n        self.num_models = num_models  # the number of sub-models\n        self.enable_sr = enable_sr\n        self.enable_fs = enable_fs\n        self.alpha1 = alpha1\n        self.alpha2 = alpha2\n        self.bins_sr = bins_sr\n        self.bins_fs = bins_fs\n        self.decay = decay\n        if sample_ratios is None:  # the default values for sample_ratios\n            sample_ratios = [0.8, 0.7, 0.6, 0.5, 0.4]\n        if sub_weights is None:  # the default values for sub_weights\n            sub_weights = [1] * self.num_models\n        if not len(sample_ratios) == bins_fs:\n            raise ValueError(\"The length of sample_ratios should be equal to bins_fs.\")\n        self.sample_ratios = sample_ratios\n        if not len(sub_weights) == num_models:\n            raise ValueError(\"The length of sub_weights should be equal to num_models.\")\n        self.sub_weights = sub_weights\n        self.epochs = epochs\n        self.logger = get_module_logger(\"DEnsembleModel\")\n        self.logger.info(\"Double Ensemble Model...\")\n        self.ensemble = []  # the current ensemble model, a list contains all the sub-models\n        self.sub_features = []  # the features for each sub model in the form of pandas.Index\n        self.params = {\"objective\": loss}\n        self.params.update(kwargs)\n        self.loss = loss\n        self.early_stopping_rounds = early_stopping_rounds\n\n    def fit(self, dataset: DatasetH):\n        df_train, df_valid = dataset.prepare(\n            [\"train\", \"valid\"], col_set=[\"feature\", \"label\"], data_key=DataHandlerLP.DK_L\n        )\n        if df_train.empty or df_valid.empty:\n            raise ValueError(\"Empty data from dataset, please check your dataset config.\")\n        x_train, y_train = df_train[\"feature\"], df_train[\"label\"]","sourceCodeStart":35,"sourceCodeEnd":71,"githubUrl":"https://github.com/microsoft/qlib/blob/79633dd9506ea689e5400dea0197717b5b3d74b7/qlib/contrib/model/double_ensemble.py#L35-L71","documentation":"Thrown by DEnsembleModel's __init__ when len(sub_weights) != num_models. Each of the num_models sub-models in the ensemble gets one weight used to combine predictions (pred = sum(w_k * pred_k) / sum(w)), so the weights list must have exactly one entry per sub-model. The default is [1]*num_models.","triggerScenarios":"Passing DEnsembleModel(num_models=5, sub_weights=[1, 2]) or any explicit sub_weights list shorter/longer than num_models; changing num_models while keeping a hand-written sub_weights.","commonSituations":"Hyperparameter sweeps that vary num_models but fix sub_weights; manually weighting sub-models and miscounting.","solutions":["Set sub_weights to None (default, all ones) unless per-model weighting is required","Provide exactly num_weights entries: sub_weights=[1]*num_models or your weights list of that length"],"exampleFix":"# before\nmodel = DEnsembleModel(num_models=5, sub_weights=[1, 2, 3])\n\n# after\nmodel = DEnsembleModel(num_models=5, sub_weights=[1, 2, 3, 2, 1])","handlingStrategy":"validation","validationCode":"if sub_weights is None:\n    sub_weights = [1] * num_models\nassert len(sub_weights) == num_models, \"sub_weights length must match num_models\"","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Leave sub_weights as None unless you explicitly weight sub-models","In hyperparameter sweeps over num_models, generate sub_weights dynamically"],"tags":["double-ensemble","hyperparameters","config-validation","qlib"],"backgroundTag":null,"analyzedSha":"79633dd9506ea689e5400dea0197717b5b3d74b7","analyzedAt":"2026-08-15T07:01:27.511Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}