microsoft/qlib · error · ValueError

unknown loss `%s`

Error message

unknown loss `%s`

What it means

Raised by HIST.loss_fn when self.loss is not "mse". HIST implements plain MSE only; any other loss name reaches the raise on the first training batch. The raise can also surface via metric_fn when the metric falls into the loss branch.

Source

Thrown at qlib/contrib/model/pytorch_hist.py:160

        self.fitted = False
        self.HIST_model.to(self.device)

    @property
    def use_gpu(self):
        return self.device != torch.device("cpu")

    def mse(self, pred, label):
        loss = (pred - label) ** 2
        return torch.mean(loss)

    def loss_fn(self, pred, label):
        mask = ~torch.isnan(label)

        if self.loss == "mse":
            return self.mse(pred[mask], label[mask])

        raise ValueError("unknown loss `%s`" % self.loss)

    def metric_fn(self, pred, label):
        mask = torch.isfinite(label)

        if self.metric == "ic":
            x = pred[mask]
            y = label[mask]

            vx = x - torch.mean(x)
            vy = y - torch.mean(y)
            return torch.sum(vx * vy) / (torch.sqrt(torch.sum(vx**2)) * torch.sqrt(torch.sum(vy**2)))

        if self.metric == ("", "loss"):
            return -self.loss_fn(pred[mask], label[mask])

        raise ValueError("unknown metric `%s`" % self.metric)

    def get_daily_inter(self, df, shuffle=False):

View on GitHub (pinned to 79633dd950)

Solutions

  1. Set loss="mse".
  2. Subclass HIST and override loss_fn to add branches before the raise for a custom loss.

Example fix

# before
HIST(loss="mae", ...)

# after
HIST(loss="mse", ...)
Defensive patterns

Strategy: validation

Validate before calling

assert params["loss"] == "mse", "HIST supports only loss='mse'"

Type guard

def is_supported_loss(loss: str) -> bool:
    return loss == "mse"

Try / catch

try:
    model.fit(dataset)
except ValueError as e:
    if "unknown loss" in str(e):
        raise ValueError("HIST only supports loss='mse'") from e
    raise

Prevention

When it happens

Trigger: HIST(loss="mae"/"huber"/"cross_entropy", ...) then fit() on stock daily-batch data; fires at train_epoch -> loss_fn on the first daily batch.

Common situations: Reusing hyper-parameter configs across different qlib contrib models; custom-loss experiments attempted via config only.

Related errors


AI-assisted analysis of microsoft/qlib@79633dd950 (2026-08-15). Data as JSON: /api/errors/4f7e4bb906485f31. Report an issue: GitHub.