{"record":{"id":"3d0ce0b437fcbdfb","repo":"microsoft/qlib","slug":"unknown-loss-s-3d0ce0","errorCode":null,"errorMessage":"unknown loss `%s`","messagePattern":"unknown loss `(.+?)`","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"qlib/contrib/model/pytorch_gru.py","lineNumber":146,"sourceCode":"\n        self.fitted = False\n        self.gru_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\n        self.gru_model.train()\n\n        indices = np.arange(len(x_train_values))\n        np.random.shuffle(indices)\n","sourceCodeStart":128,"sourceCodeEnd":164,"githubUrl":"https://github.com/microsoft/qlib/blob/79633dd9506ea689e5400dea0197717b5b3d74b7/qlib/contrib/model/pytorch_gru.py#L128-L164","documentation":"Raised by GRUModel.loss_fn when self.loss is not the literal \"mse\". Like the general NN model, GRU only implements plain MSE (no weighting) and dispatches via a single if; every other value reaches the raise on the first training batch.","triggerScenarios":"GRUModel(loss=\"huber\") or any non-\"mse\" string, then fit() -> train_epoch -> loss_fn on the first batch. Also reached indirectly through metric_fn when metric is \"\"/\"loss\".","commonSituations":"Copying hyper-parameter blocks between qlib model classes where supported loss names differ; attempting to use a custom loss by name without subclassing.","solutions":["Set loss=\"mse\" (the only supported value for GRUModel).","Subclass GRUModel and override loss_fn/mse to add your loss before the raise."],"exampleFix":"# before\nGRUModel(loss=\"mae\", ...)\n\n# after\nGRUModel(loss=\"mse\", ...)","handlingStrategy":"validation","validationCode":"assert params[\"loss\"] == \"mse\", \"GRUModel 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(\"GRUModel only supports loss='mse'\") from e\n    raise","preventionTips":["Treat 'mse' as the default and only loss for GRU/LSTM-family qlib models unless you subclass.","Centralize model hyper-parameter validation in your experiment harness."],"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"}