{"record":{"id":"c12d85adf0f77353","repo":"microsoft/qlib","slug":"unknown-metric-s-c12d85","errorCode":null,"errorMessage":"unknown metric `%s`","messagePattern":"unknown metric `(.+?)`","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"qlib/contrib/model/pytorch_tcn.py","lineNumber":162,"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.tcn_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.tcn_model(feature)","sourceCodeStart":144,"sourceCodeEnd":180,"githubUrl":"https://github.com/microsoft/qlib/blob/79633dd9506ea689e5400dea0197717b5b3d74b7/qlib/contrib/model/pytorch_tcn.py#L144-L180","documentation":"Thrown by TCNModel.metric_fn in qlib's PyTorch TCN contribution model. During fit/predict evaluation the model converts its `metric` hyperparameter into a scoring function; only the values '' (empty string) and 'loss' (negative training loss) are implemented. Any other string reaches the terminal raise in metric_fn and aborts training with 'unknown metric `%s`'.","triggerScenarios":"Instantiating TCNModel (qlib.contrib.model.pytorch_tcn.TCNModel) and passing metric='ic', metric='auc', or any string other than '' / 'loss', then calling fit(); the first validation epoch calls metric_fn and raises.","commonSituations":"Users copy model configs from other qlib models (e.g. ALSTM or LightGBM workflows where metric='ic') into a TCN config. Others typo 'loss' as 'Loss' or 'mse' (mse is a valid loss value but NOT a valid metric value here).","solutions":["Set metric='loss' (or omit it / set to '') in the TCNModel constructor — these are the only supported values.","If you typo'd, check the exact spelling and casing; the comparison is against the lowercase strings '' and 'loss' only.","If you need a custom metric, subclass TCNModel and override metric_fn to implement it (e.g. IC) before calling super().fit()."],"exampleFix":"# before\nmodel = TCNModel(..., loss=\"mse\", metric=\"ic\")\n\n# after\nmodel = TCNModel(..., loss=\"mse\", metric=\"loss\")","handlingStrategy":"validation","validationCode":"from qlib.contrib.model.pytorch_tcn import TCNModel\nassert model_kwargs.get(\"metric\", \"\") in (\"\", \"loss\"), f\"TCNModel supports metric '' or 'loss', got {model_kwargs.get('metric')!r}\"","typeGuard":null,"tryCatchPattern":"try:\n    model.fit(dataset, evals_result)\nexcept ValueError as e:\n    if \"unknown metric\" in str(e):\n        raise ValueError(f\"Fix TCNModel.metric (only '' or 'loss'): {model.metric!r}\") from e\n    raise","preventionTips":["Keep a per-model allowlist of hyperparameters instead of sharing one dict across qlib models.","Add a constructor-level assertion for metric before fit in experiment driver code."],"tags":["qlib","pytorch","tcn","hyperparameter","validation"],"backgroundTag":null,"analyzedSha":"79633dd9506ea689e5400dea0197717b5b3d74b7","analyzedAt":"2026-08-15T07:01:27.511Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}