{"record":{"id":"7a46820a43be44bb","repo":"microsoft/qlib","slug":"unknown-metric-s-7a4682","errorCode":null,"errorMessage":"unknown metric `%s`","messagePattern":"unknown metric `(.+?)`","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"qlib/contrib/model/pytorch_gru_ts.py","lineNumber":162,"sourceCode":"\n    def loss_fn(self, pred, label, weight=None):\n        mask = ~torch.isnan(label)\n\n        if weight is None:\n            weight = torch.ones_like(label)\n\n        if self.loss == \"mse\":\n            return self.mse(pred[mask], label[mask], weight[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, data_loader):\n        self.GRU_model.train()\n\n        for data, weight in data_loader:\n            feature = data[:, :, 0:-1].to(self.device)\n            label = data[:, -1, -1].to(self.device)\n\n            pred = self.GRU_model(feature.float())\n            loss = self.loss_fn(pred, label, weight.to(self.device))\n\n            self.train_optimizer.zero_grad()\n            loss.backward()\n            torch.nn.utils.clip_grad_value_(self.GRU_model.parameters(), 3.0)\n            self.train_optimizer.step()\n\n    def test_epoch(self, data_loader):\n        self.GRU_model.eval()","sourceCodeStart":144,"sourceCodeEnd":180,"githubUrl":"https://github.com/microsoft/qlib/blob/79633dd9506ea689e5400dea0197717b5b3d74b7/qlib/contrib/model/pytorch_gru_ts.py#L144-L180","documentation":"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.","triggerScenarios":"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.","commonSituations":"Copying benchmark configs that use metric=\"ic\" with models that do support it; assuming metric names are uniform across qlib contrib models.","solutions":["Use metric=\"\" or \"loss\".","Subclass GRUModelTS, override metric_fn to add an \"ic\" branch (negate appropriately since higher scores are treated as better)."],"exampleFix":"# before\nGRUModelTS(metric=\"ic\", ...)  # ValueError\n\n# after\nGRUModelTS(metric=\"loss\", ...)","handlingStrategy":"validation","validationCode":"assert params.get(\"metric\", \"\") in {\"\", \"loss\"}, \"GRUModelTS 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 = GRUModelTS(**params)\n        model.fit(dataset)\n    else:\n        raise","preventionTips":["Check the concrete metric_fn dispatch of each model class before configuring metrics.","Remember TS variants return -loss (higher-is-better) when adding custom metrics."],"tags":["pytorch","qlib","metrics","configuration"],"backgroundTag":null,"analyzedSha":"79633dd9506ea689e5400dea0197717b5b3d74b7","analyzedAt":"2026-08-15T07:01:27.511Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}