microsoft/qlib · error · ValueError
unknown metric `%s`
Error message
unknown metric `%s`
What it means
Raised by GRUModel.metric_fn when self.metric is not "" or "loss". GRU's validation metric is restricted to the (negated) training loss; unlike GRUTS/pytorch_hist, "ic" is not implemented here. The raise occurs during the first validation pass of fit().
Source
Thrown at qlib/contrib/model/pytorch_gru.py:154
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 train_epoch(self, x_train, y_train):
x_train_values = x_train.values
y_train_values = np.squeeze(y_train.values)
self.gru_model.train()
indices = np.arange(len(x_train_values))
np.random.shuffle(indices)
for i in range(len(indices))[:: self.batch_size]:
if len(indices) - i < self.batch_size:
break
feature = torch.from_numpy(x_train_values[indices[i : i + self.batch_size]]).float().to(self.device)
label = torch.from_numpy(y_train_values[indices[i : i + self.batch_size]]).float().to(self.device)
pred = self.gru_model(feature)View on GitHub (pinned to 79633dd950)
Solutions
- Use metric="" or metric="loss" for GRUModel.
- Subclass GRUModel and extend metric_fn (copy the IC computation from pytorch_hist.GRUModel's sibling class) if you need IC-based early stopping.
Example fix
# before GRUModel(metric="ic", ...) # ValueError: unknown metric `ic` # after GRUModel(metric="loss", ...)
Defensive patterns
Strategy: validation
Validate before calling
assert params.get("metric", "") in {"", "loss"}, "GRUModel 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 = GRUModel(**params)
model.fit(dataset)
else:
raise Prevention
- Do not assume metric='ic' works everywhere; check metric_fn of the concrete class.
- Default to loss-based early stopping when unsure.
When it happens
Trigger: GRUModel(metric="ic") followed by fit() with a validation segment; any string other than ""/"loss" triggers it on the first validation batch.
Common situations: Assuming all qlib pytorch models accept metric="ic" because benchmarks use it; porting configs from ALSTM/GRU_TS where "ic" exists.
Related errors
AI-assisted analysis of microsoft/qlib@79633dd950 (2026-08-15).
Data as JSON: /api/errors/40efdb660eef689d.
Report an issue: GitHub.