{"record":{"id":"40efdb660eef689d","repo":"microsoft/qlib","slug":"unknown-metric-s-40efdb","errorCode":null,"errorMessage":"unknown metric `%s`","messagePattern":"unknown metric `(.+?)`","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"qlib/contrib/model/pytorch_gru.py","lineNumber":154,"sourceCode":"    def mse(self, pred, label):\n        loss = (pred - label) ** 2\n        return torch.mean(loss)\n\n    def loss_fn(self, pred, label):\n        mask = ~torch.isnan(label)\n\n        if self.loss == \"mse\":\n            return self.mse(pred[mask], label[mask])\n\n        raise ValueError(\"unknown loss `%s`\" % self.loss)\n\n    def metric_fn(self, pred, label):\n        mask = torch.isfinite(label)\n\n        if self.metric in (\"\", \"loss\"):\n            return -self.loss_fn(pred[mask], label[mask])\n\n        raise ValueError(\"unknown metric `%s`\" % self.metric)\n\n    def train_epoch(self, x_train, y_train):\n        x_train_values = x_train.values\n        y_train_values = np.squeeze(y_train.values)\n\n        self.gru_model.train()\n\n        indices = np.arange(len(x_train_values))\n        np.random.shuffle(indices)\n\n        for i in range(len(indices))[:: self.batch_size]:\n            if len(indices) - i < self.batch_size:\n                break\n\n            feature = torch.from_numpy(x_train_values[indices[i : i + self.batch_size]]).float().to(self.device)\n            label = torch.from_numpy(y_train_values[indices[i : i + self.batch_size]]).float().to(self.device)\n\n            pred = self.gru_model(feature)","sourceCodeStart":136,"sourceCodeEnd":172,"githubUrl":"https://github.com/microsoft/qlib/blob/79633dd9506ea689e5400dea0197717b5b3d74b7/qlib/contrib/model/pytorch_gru.py#L136-L172","documentation":"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().","triggerScenarios":"GRUModel(metric=\"ic\") followed by fit() with a validation segment; any string other than \"\"/\"loss\" triggers it on the first validation batch.","commonSituations":"Assuming all qlib pytorch models accept metric=\"ic\" because benchmarks use it; porting configs from ALSTM/GRU_TS where \"ic\" exists.","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."],"exampleFix":"# before\nGRUModel(metric=\"ic\", ...)  # ValueError: unknown metric `ic`\n\n# after\nGRUModel(metric=\"loss\", ...)","handlingStrategy":"validation","validationCode":"assert params.get(\"metric\", \"\") in {\"\", \"loss\"}, \"GRUModel metric must be '' or 'loss'\"","typeGuard":"def is_supported_metric(metric: str) -> bool:\n    return metric in {\"\", \"loss\"}","tryCatchPattern":"try:\n    model.fit(dataset)\nexcept ValueError as e:\n    if \"unknown metric\" in str(e):\n        params[\"metric\"] = \"loss\"\n        model = GRUModel(**params)\n        model.fit(dataset)\n    else:\n        raise","preventionTips":["Do not assume metric='ic' works everywhere; check metric_fn of the concrete class.","Default to loss-based early stopping when unsure."],"tags":["pytorch","qlib","metrics","configuration"],"backgroundTag":null,"analyzedSha":"79633dd9506ea689e5400dea0197717b5b3d74b7","analyzedAt":"2026-08-15T07:01:27.511Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}