{"record":{"id":"c23f5ad5440126b1","repo":"microsoft/qlib","slug":"unknown-loss-s-c23f5a","errorCode":null,"errorMessage":"unknown loss `%s`","messagePattern":"unknown loss `(.+?)`","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"qlib/contrib/model/pytorch_sandwich.py","lineNumber":241,"sourceCode":"\n        self.fitted = False\n        self.sandwich_model.to(self.device)\n\n    @property\n    def use_gpu(self):\n        return self.device != torch.device(\"cpu\")\n\n    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        self.sandwich_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]:","sourceCodeStart":223,"sourceCodeEnd":259,"githubUrl":"https://github.com/microsoft/qlib/blob/79633dd9506ea689e5400dea0197717b5b3d74b7/qlib/contrib/model/pytorch_sandwich.py#L223-L259","documentation":"Raised by SANDWITCH model loss_fn in qlib/contrib/model/pytorch_sandwich.py:241 when self.loss is not 'mse'. The sandwich model implements exactly one supervised loss — masked MSE over non-NaN labels — so any other loss string is a ValueError. The loss attribute comes from the constructor's loss kwarg.","triggerScenarios":"Passing loss='mae', loss='huber', etc. to the sandwich model and calling fit(); the first train/valid epoch evaluation calls loss_fn and raises. Note metric in ('', 'loss') also funnels into loss_fn, so a bad loss breaks metric evaluation too.","commonSituations":"Reusing kwargs from DNNModelPytorch (which accepts 'binary') or from other contrib models with richer loss menus; expecting symmetric naming with LightGBM's objective strings.","solutions":["Set loss: 'mse' in the sandwich model kwargs (currently the only option).","If another loss is required, subclass and override loss_fn() (keep the NaN mask) rather than modifying the library."],"exampleFix":"# before\nkwargs:\n  loss: mae\n\n# after\nkwargs:\n  loss: mse","handlingStrategy":"validation","validationCode":"assert config.get(\"loss\", \"mse\") == \"mse\", \"SANDWITCH model only supports loss='mse'\"","typeGuard":"def is_supported_sandwich_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(\"Set loss='mse'; other losses require overriding loss_fn()\") from e\n    raise","preventionTips":["Do not copy loss names between qlib models; supported sets differ per class.","Write per-model config templates validated against the class source."],"tags":["qlib","pytorch","loss-function","config","sandwich"],"backgroundTag":null,"analyzedSha":"79633dd9506ea689e5400dea0197717b5b3d74b7","analyzedAt":"2026-08-15T07:01:27.511Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}