{"record":{"id":"33a520639cb84b5b","repo":"microsoft/qlib","slug":"unsupported-reweighter-type-33a520","errorCode":null,"errorMessage":"Unsupported reweighter type.","messagePattern":"Unsupported reweighter type\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"qlib/contrib/model/pytorch_alstm_ts.py","lineNumber":228,"sourceCode":"        save_path=None,\n        reweighter=None,\n    ):\n        dl_train = dataset.prepare(\"train\", col_set=[\"feature\", \"label\"], data_key=DataHandlerLP.DK_L)\n        dl_valid = dataset.prepare(\"valid\", col_set=[\"feature\", \"label\"], data_key=DataHandlerLP.DK_L)\n        if dl_train.empty or dl_valid.empty:\n            raise ValueError(\"Empty data from dataset, please check your dataset config.\")\n\n        dl_train.config(fillna_type=\"ffill+bfill\")  # process nan brought by dataloader\n        dl_valid.config(fillna_type=\"ffill+bfill\")  # process nan brought by dataloader\n\n        if reweighter is None:\n            wl_train = np.ones(len(dl_train))\n            wl_valid = np.ones(len(dl_valid))\n        elif isinstance(reweighter, Reweighter):\n            wl_train = reweighter.reweight(dl_train)\n            wl_valid = reweighter.reweight(dl_valid)\n        else:\n            raise ValueError(\"Unsupported reweighter type.\")\n\n        train_loader = DataLoader(\n            ConcatDataset(dl_train, wl_train),\n            batch_size=self.batch_size,\n            shuffle=True,\n            num_workers=self.n_jobs,\n            drop_last=True,\n        )\n        valid_loader = DataLoader(\n            ConcatDataset(dl_valid, wl_valid),\n            batch_size=self.batch_size,\n            shuffle=False,\n            num_workers=self.n_jobs,\n            drop_last=True,\n        )\n\n        save_path = get_or_create_path(save_path)\n","sourceCodeStart":210,"sourceCodeEnd":246,"githubUrl":"https://github.com/microsoft/qlib/blob/79633dd9506ea689e5400dea0197717b5b3d74b7/qlib/contrib/model/pytorch_alstm_ts.py#L210-L246","documentation":"ALSTMTSModel.fit() accepts an optional reweighter used to compute per-sample weights for the training and validation loaders. It only handles two cases: reweighter is None (uniform weights of ones) or reweighter is an instance of qlib's Reweighter class. Anything else (a function, lambda, numpy array, dict) raises ValueError.","triggerScenarios":"Passing fit(dataset, reweighter=my_func), a raw numpy weight array, or a custom reweighting object that does not subclass qlib.data.dataset.Reweighting.Reweighter.","commonSituations":"Coming from sklearn-style APIs where sample_weight arrays are accepted directly; writing a bespoke reweighting callable instead of subclassing Reweighter; version drift where the Reweighter import path moved and the isinstance check silently fails.","solutions":["Pass None if you do not need sample reweighting.","Subclass qlib.data.dataset.Reweighting.Reweighter and implement reweight(dataset) returning a pandas Series; pass that instance.","Check the actual import path of Reweighter in your qlib version and make sure your class inherits from it."],"exampleFix":"# before\nmodel.fit(dataset, reweighter=lambda df: df['volume'])\n\n# after\nfrom qlib.data.dataset import Reweighter\nclass VolReweighter(Reweighter):\n    def reweight(self, df):\n        return df['volume'] / df['volume'].mean()\nmodel.fit(dataset, reweighter=VolReweighter())","handlingStrategy":"type-guard","validationCode":"from qlib.data.dataset import Reweighter\nif reweighter is not None and not isinstance(reweighter, Reweighter):\n    raise TypeError('reweighter must be None or a qlib Reweighter instance')","typeGuard":"from qlib.data.dataset import Reweighter\n\ndef is_valid_reweighter(rw) -> bool:\n    return rw is None or isinstance(rw, Reweighter)","tryCatchPattern":"try:\n    model.fit(dataset, reweighter=rw)\nexcept ValueError as e:\n    if 'Unsupported reweighter' in str(e):\n        rw = None  # or wrap logic in a Reweighter subclass\n        model.fit(dataset, reweighter=rw)\n    else:\n        raise","preventionTips":["Subclass qlib's Reweighter for custom weighting schemes instead of passing callables or arrays.","Check isinstance(reweighter, Reweighter) before calling fit() with reweighting enabled.","Remember this model has no sklearn-style sample_weight support."],"tags":["qlib","reweighter","type-validation","alstm","training"],"backgroundTag":null,"analyzedSha":"79633dd9506ea689e5400dea0197717b5b3d74b7","analyzedAt":"2026-08-15T07:01:27.511Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}