{"record":{"id":"0cd6765fc41cbc79","repo":"microsoft/qlib","slug":"unknown-loss-s-0cd676","errorCode":null,"errorMessage":"unknown loss `%s`","messagePattern":"unknown loss `(.+?)`","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"qlib/contrib/model/pytorch_tcn_ts.py","lineNumber":155,"sourceCode":"\n        self.fitted = False\n        self.TCN_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, data_loader):\n        self.TCN_model.train()\n\n        for data in data_loader:\n            data = torch.transpose(data, 1, 2)\n            feature = data[:, 0:-1, :].to(self.device)\n            label = data[:, -1, -1].to(self.device)\n\n            pred = self.TCN_model(feature.float())","sourceCodeStart":137,"sourceCodeEnd":173,"githubUrl":"https://github.com/microsoft/qlib/blob/79633dd9506ea689e5400dea0197717b5b3d74b7/qlib/contrib/model/pytorch_tcn_ts.py#L137-L173","documentation":"Thrown by TCNTSModel.loss_fn, which computes the training/validation loss. Only loss='mse' is implemented; every other value of the `loss` hyperparameter reaches the terminal raise. It fires on the first batch of training or validation.","triggerScenarios":"Constructing TCNTSModel with loss='mae', loss='huber', or anything other than 'mse', then calling fit(); train_epoch calls loss_fn on the first batch and raises immediately.","commonSituations":"Copying loss settings from custom models; assuming the string used for `metric`/`loss` in other frameworks applies; typo like 'MSE' (uppercase) which fails the exact '==' comparison.","solutions":["Set loss='mse' (exact lowercase) in the TCNTSModel config — the only supported loss.","If another loss is required, subclass TCNTSModel and override loss_fn (e.g. add an mae branch) while keeping the same NaN-masking behavior."],"exampleFix":"# before\nmodel = TCNTSModel(..., loss=\"mae\")\n\n# after\nmodel = TCNTSModel(..., loss=\"mse\")","handlingStrategy":"validation","validationCode":"assert model_kwargs.get(\"loss\", \"mse\") == \"mse\", \"TCNTSModel supports only loss='mse'\"","typeGuard":null,"tryCatchPattern":"try:\n    model.fit(ds, valid)\nexcept ValueError as e:\n    if \"unknown loss\" in str(e):\n        model_kwargs[\"loss\"] = \"mse\"\n        model = TCNTSModel(**model_kwargs)\n        model.fit(ds, valid)\n    else:\n        raise","preventionTips":["Treat 'mse' as the only loss string for TCN-family models in shared configs.","Normalize hyperparameter strings to lowercase on config load to avoid case mismatches."],"tags":["qlib","pytorch","tcn","loss-function","hyperparameter"],"backgroundTag":null,"analyzedSha":"79633dd9506ea689e5400dea0197717b5b3d74b7","analyzedAt":"2026-08-15T07:01:27.511Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}