microsoft/qlib · error · ValueError

unknown metric `%s`

Error message

unknown metric `%s`

What it means

KRNNModel.metric_fn supports metric values in ('', 'loss') — i.e. the empty string or 'loss', both meaning 'use negative loss as the score'. Note this file uses the correct `in` check, unlike IGMTFModel's buggy tuple comparison. Any other value (e.g. 'ic') raises ValueError.

Source

Thrown at qlib/contrib/model/pytorch_krnn.py:367

    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 in ("", "loss"):
            return -self.loss_fn(pred[mask], label[mask])

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

    def get_daily_inter(self, df, shuffle=False):
        # organize the train data into daily batches
        daily_count = df.groupby(level=0, group_keys=False).size().values
        daily_index = np.roll(np.cumsum(daily_count), 1)
        daily_index[0] = 0
        if shuffle:
            # shuffle data
            daily_shuffle = list(zip(daily_index, daily_count))
            np.random.shuffle(daily_shuffle)
            daily_index, daily_count = zip(*daily_shuffle)
        return daily_index, daily_count

    def train_epoch(self, x_train, y_train):
        x_train_values = x_train.values
        y_train_values = np.squeeze(y_train.values)
        self.krnn_model.train()

View on GitHub (pinned to 79633dd950)

Solutions

  1. Use metric='' or metric='loss' for KRNNModel
  2. If you need 'ic', subclass KRNNModel and add an IC branch to metric_fn

Example fix

# before
KRNNModel(metric="ic")  # not supported by krnn

# after
KRNNModel(metric="loss")
Defensive patterns

Strategy: validation

Validate before calling

assert metric in ("", "loss"), "KRNNModel metric must be '' or 'loss' (no 'ic' support)"

Type guard

def is_supported_krnn_metric(name: str) -> bool:
    return name in ("", "loss")

Prevention

When it happens

Trigger: KRNNModel(metric='ic') or any string other than ''/'loss', then fit() — the error surfaces when the validation score is computed.

Common situations: Copying metric='ic' from HIST/IGMTF workflows where 'ic' is the default; expecting IC-based early stopping on a model that only implements loss-as-metric.

Related errors


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