microsoft/qlib · error · ValueError
Unsupported reweighter type.
Error message
Unsupported reweighter type.
What it means
DNNModelPytorch.fit() builds sample weights per segment: reweighter=None gives uniform ones; a qlib.dataset.common.Reweighter instance has its reweight(df) called; anything else raises ValueError('Unsupported reweighter type.') while preparing train/valid data, before training starts.
Source
Thrown at qlib/contrib/model/pytorch_nn.py:216
has_valid = "valid" in dataset.segments
segments = ["train", "valid"]
vars = ["x", "y", "w"]
all_df = defaultdict(dict) # x_train, x_valid y_train, y_valid w_train, w_valid
all_t = defaultdict(dict) # tensors
for seg in segments:
if seg in dataset.segments:
# df_train df_valid
df = dataset.prepare(
seg, col_set=["feature", "label"], data_key=self.valid_key if seg == "valid" else DataHandlerLP.DK_L
)
all_df["x"][seg] = df["feature"]
all_df["y"][seg] = df["label"].copy() # We have to use copy to remove the reference to release mem
if reweighter is None:
all_df["w"][seg] = pd.DataFrame(np.ones_like(all_df["y"][seg].values), index=df.index)
elif isinstance(reweighter, Reweighter):
all_df["w"][seg] = pd.DataFrame(reweighter.reweight(df))
else:
raise ValueError("Unsupported reweighter type.")
# get tensors
for v in vars:
all_t[v][seg] = torch.from_numpy(all_df[v][seg].values).float()
# if seg == "valid": # accelerate the eval of validation
all_t[v][seg] = all_t[v][seg].to(self.device) # This will consume a lot of memory !!!!
evals_result[seg] = []
# free memory
del df
del all_df["x"]
gc.collect()
save_path = get_or_create_path(save_path)
stop_steps = 0
train_loss = 0
best_loss = np.inf
# trainView on GitHub (pinned to 79633dd950)
Solutions
- Subclass qlib.data.dataset.Reweighter and implement reweight(self, data_frame) returning per-sample weights; pass that instance.
- Pass reweighter=None (or omit) when you don't need weighting.
- For builtin weighting schemes, check qlib's existing Reweighter implementations and reuse them.
Example fix
# before
model.fit(dataset, reweighter=lambda df: df["label"] ** 2) # ValueError: Unsupported reweighter type.
# after
from qlib.data.dataset import Reweighter
class AbsLabelReweighter(Reweighter):
def reweight(self, data_frame):
return data_frame["label"].abs().values
model.fit(dataset, reweighter=AbsLabelReweighter()) Defensive patterns
Strategy: type-guard
Validate before calling
from qlib.data.dataset import Reweighter
if reweighter is not None and not isinstance(reweighter, Reweighter):
raise TypeError("reweighter must be None or a qlib Reweighter instance")
model.fit(dataset, reweighter=reweighter) Type guard
from qlib.data.dataset import Reweighter
def is_valid_reweighter(rw) -> bool:
return rw is None or isinstance(rw, Reweighter) Try / catch
try:
model.fit(dataset, reweighter=rw)
except ValueError as e:
if "Unsupported reweighter" in str(e):
raise TypeError("Wrap weighting logic in a qlib Reweighter subclass") from e
raise Prevention
- Wrap any weighting scheme in a Reweighter subclass with reweight(df) returning per-sample weights.
- The check is isinstance-based; duck typing is rejected by design.
- Pass None explicitly when uniform weights are intended.
When it happens
Trigger: model.fit(dataset, reweighter=X) with X not None and not an instance of qlib.dataset.common.Reweighter — e.g. a weight array, pandas Series, lambda, or duck-typed custom class lacking the subclass relationship.
Common situations: Hand-rolling sample weights as arrays/callables; using a reweighter class copied from another project that doesn't subclass qlib's Reweighter; misunderstanding that the API is type-based, not duck-typed.
Related errors
- Unsupported reweighter type.
- Unsupported reweighter type.
- optimizer {} is not supported!
- loss {} is not supported!
- optimizer {} is not supported!
AI-assisted analysis of microsoft/qlib@79633dd950 (2026-08-15).
Data as JSON: /api/errors/43cb2d9ce3fa0bff.
Report an issue: GitHub.