microsoft/qlib · error · ValueError
unknown loss `%s`
Error message
unknown loss `%s`
What it means
Raised by SFM model loss_fn in qlib/contrib/model/pytorch_sfm.py:426 when self.loss is not 'mse'. The SFM model supports only masked MSE (NaN labels are masked out); any other loss string is a ValueError thrown on the first loss evaluation in fit(). The metric_fn ('', 'loss') also delegates to loss_fn, so a bad loss breaks both.
Source
Thrown at qlib/contrib/model/pytorch_sfm.py:426
break
self.logger.info("best score: %.6lf @ %d" % (best_score, best_epoch))
self.sfm_model.load_state_dict(best_param)
torch.save(best_param, save_path)
if self.device != "cpu":
torch.cuda.empty_cache()
def mse(self, pred, label):
loss = (pred - label) ** 2
return torch.mean(loss)
def loss_fn(self, pred, label):
mask = ~torch.isnan(label)
if self.loss == "mse":
return self.mse(pred[mask], label[mask])
raise ValueError("unknown loss `%s`" % self.loss)
def metric_fn(self, pred, label):
mask = torch.isfinite(label)
if self.metric in ("", "loss"):
return -self.loss_fn(pred[mask], label[mask])
raise ValueError("unknown metric `%s`" % self.metric)
def predict(self, dataset: DatasetH, segment: Union[Text, slice] = "test"):
if not self.fitted:
raise ValueError("model is not fitted yet!")
x_test = dataset.prepare(segment, col_set="feature", data_key=DataHandlerLP.DK_I)
index = x_test.index
self.sfm_model.eval()
x_values = x_test.values
sample_num = x_values.shape[0]View on GitHub (pinned to 79633dd950)
Solutions
- Set loss: 'mse' (only supported value).
- Subclass the SFM model and override loss_fn(), preserving the NaN mask, for custom losses.
Example fix
# before kwargs: loss: huber # after kwargs: loss: mse
Defensive patterns
Strategy: validation
Validate before calling
assert config.get("loss", "mse") == "mse", "SFM model only supports loss='mse'" Type guard
def is_supported_sfm_loss(loss: str) -> bool:
return loss == "mse" Try / catch
try:
model.fit(dataset)
except ValueError as e:
if "unknown loss" in str(e):
raise ValueError("SFM supports only loss='mse'") from e
raise Prevention
- Per-model config validation: the accepted loss set differs between qlib models.
- Default loss to 'mse' and omit from configs unless intentionally changed.
When it happens
Trigger: Setting loss to anything except 'mse' in SFM model kwargs and calling fit(); the first train_epoch/test_epoch evaluation triggers the raise.
Common situations: Copying loss names from other frameworks or other qlib models; hand-editing a workflow YAML and introducing a typo like 'msae' or 'MSE'.
Related errors
AI-assisted analysis of microsoft/qlib@79633dd950 (2026-08-15).
Data as JSON: /api/errors/cc40b8883e2c0b0f.
Report an issue: GitHub.