{"record":{"id":"6c36ad7851ec926e","repo":"microsoft/qlib","slug":"unknown-loss-s-6c36ad","errorCode":null,"errorMessage":"unknown loss `%s`","messagePattern":"unknown loss `(.+?)`","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"qlib/contrib/model/pytorch_gru_ts.py","lineNumber":154,"sourceCode":"\n    @property\n    def use_gpu(self):\n        return self.device != torch.device(\"cpu\")\n\n    def mse(self, pred, label, weight):\n        loss = weight * (pred - label) ** 2\n        return torch.mean(loss)\n\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))","sourceCodeStart":136,"sourceCodeEnd":172,"githubUrl":"https://github.com/microsoft/qlib/blob/79633dd9506ea689e5400dea0197717b5b3d74b7/qlib/contrib/model/pytorch_gru_ts.py#L136-L172","documentation":"Raised by GRUModelTS.loss_fn when self.loss is not \"mse\". The TS variant supports only weighted MSE (it accepts an optional weight tensor for reweighter support); all other loss names raise on the first training batch.","triggerScenarios":"GRUModelTS(loss=<anything but \"mse\">) then fit(); the loss_fn is also invoked by metric_fn when metric is \"\"/\"loss\" during validation.","commonSituations":"Hyper-parameter search sweeps that include unsupported loss values; configs copied from other frameworks.","solutions":["Set loss=\"mse\".","Subclass GRUModelTS and extend loss_fn for a custom loss."],"exampleFix":"# before\nGRUModelTS(loss=\"huber\", ...)\n\n# after\nGRUModelTS(loss=\"mse\", ...)","handlingStrategy":"validation","validationCode":"assert params[\"loss\"] == \"mse\", \"GRUModelTS supports only loss='mse'\"","typeGuard":"def is_supported_loss(loss: str) -> bool:\n    return loss == \"mse\"","tryCatchPattern":"try:\n    model.fit(dataset)\nexcept ValueError as e:\n    if \"unknown loss\" in str(e):\n        raise ValueError(\"GRUModelTS only supports loss='mse'\") from e\n    raise","preventionTips":["Pin loss='mse' in shared config templates for the TS model family.","Subclass to add losses; do not try to pass custom names through config."],"tags":["pytorch","qlib","loss-function","configuration"],"backgroundTag":null,"analyzedSha":"79633dd9506ea689e5400dea0197717b5b3d74b7","analyzedAt":"2026-08-15T07:01:27.511Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}