microsoft/qlib · error · ValueError

unknown metric `%s`

Error message

unknown metric `%s`

What it means

Raised by GRUModelTS.metric_fn when self.metric is not "" or "loss". Validation scoring for the TS GRU is the negated training loss only; "ic" and other standard qlib metrics are not implemented in this class. Fires during the first validation epoch.

Source

Thrown at qlib/contrib/model/pytorch_gru_ts.py:162

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

        if weight is None:
            weight = torch.ones_like(label)

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

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

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

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

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

    def train_epoch(self, data_loader):
        self.GRU_model.train()

        for data, weight in data_loader:
            feature = data[:, :, 0:-1].to(self.device)
            label = data[:, -1, -1].to(self.device)

            pred = self.GRU_model(feature.float())
            loss = self.loss_fn(pred, label, weight.to(self.device))

            self.train_optimizer.zero_grad()
            loss.backward()
            torch.nn.utils.clip_grad_value_(self.GRU_model.parameters(), 3.0)
            self.train_optimizer.step()

    def test_epoch(self, data_loader):
        self.GRU_model.eval()

View on GitHub (pinned to 79633dd950)

Solutions

  1. Use metric="" or "loss".
  2. Subclass GRUModelTS, override metric_fn to add an "ic" branch (negate appropriately since higher scores are treated as better).

Example fix

# before
GRUModelTS(metric="ic", ...)  # ValueError

# after
GRUModelTS(metric="loss", ...)
Defensive patterns

Strategy: validation

Validate before calling

assert params.get("metric", "") in {"", "loss"}, "GRUModelTS metric must be '' or 'loss'"

Type guard

def is_supported_metric(metric: str) -> bool:
    return metric in {"", "loss"}

Try / catch

try:
    model.fit(dataset)
except ValueError as e:
    if "unknown metric" in str(e):
        params["metric"] = "loss"
        model = GRUModelTS(**params)
        model.fit(dataset)
    else:
        raise

Prevention

When it happens

Trigger: GRUModelTS(metric="ic") followed by fit() with a valid segment; also note the returned score is -loss_fn(...), i.e. higher-is-better convention, which matters when adding custom metrics.

Common situations: Copying benchmark configs that use metric="ic" with models that do support it; assuming metric names are uniform across qlib contrib models.

Related errors


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