microsoft/qlib · error · ValueError

unknown loss `%s`

Error message

unknown loss `%s`

What it means

IGMTFModel.loss_fn implements exactly one loss: 'mse', computed over non-NaN labels via a mask. If self.loss is any other string, loss_fn raises this ValueError the first time the training loop tries to compute loss.

Source

Thrown at qlib/contrib/model/pytorch_igmtf.py:153

        self.fitted = False
        self.igmtf_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' (this is the only supported value)
  2. For custom losses, subclass IGMTFModel and extend loss_fn

Example fix

# before
IGMTFModel(loss="mean_squared_error")

# after
IGMTFModel(loss="mse")
Defensive patterns

Strategy: validation

Validate before calling

assert loss == "mse", f"IGMTFModel supports only loss='mse', got {loss!r}"

Type guard

def is_supported_igmtf_loss(name: str) -> bool:
    return name == "mse"

Prevention

When it happens

Trigger: Constructing IGMTFModel(loss='mse') variants misspelled, or loss='cross_entropy'/'huber'/None, then calling fit(): the error surfaces on the first train epoch batch.

Common situations: Copying a loss name supported by a different qlib model (some support more losses); typo 'msee'; leaving a placeholder value in a shared hyperparameter search grid that includes unsupported losses.

Related errors


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