microsoft/qlib · error · ValueError
unknown loss `%s`
Error message
unknown loss `%s`
What it means
LOCALTransformerModel.loss_fn() supports exactly one training loss: 'mse' (masked mean squared error that ignores NaN labels). If self.loss is any other value it raises ValueError("unknown loss `%s`"). The loss value is not validated in __init__, so this error surfaces mid-fit, inside train_epoch/validation, after data loading has already run.
Source
Thrown at qlib/contrib/model/pytorch_localformer_ts.py:95
self.fitted = False
self.model.to(self.device)
@property
def use_gpu(self):
return self.device != torch.device("cpu")
def mse(self, pred, label):
loss = (pred.float() - label.float()) ** 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 train_epoch(self, data_loader):
self.model.train()
for data in data_loader:
feature = data[:, :, 0:-1].to(self.device)
label = data[:, -1, -1].to(self.device)
pred = self.model(feature.float()) # .float()
loss = self.loss_fn(pred, label)
View on GitHub (pinned to 79633dd950)
Solutions
- Set loss='mse' in the model kwargs — it is the only supported option.
- For a custom loss, subclass and override loss_fn(self, pred, label); keep NaN masking via mask = ~torch.isnan(label).
- Validate loss in __init__ of your subclass so misconfiguration fails fast instead of after data loading.
Example fix
# before model = LOCALTransformerModel(..., loss="mae") model.fit(dataset) # ValueError: unknown loss `mae` on first batch # after model = LOCALTransformerModel(..., loss="mse") model.fit(dataset)
Defensive patterns
Strategy: validation
Validate before calling
loss = "mse" assert loss == "mse", "LOCALTransformerModel supports only loss='mse'" model = LOCALTransformerModel(..., loss=loss)
Type guard
def is_supported_loss(name: str) -> bool:
return isinstance(name, str) and name == "mse" Try / catch
try:
model.fit(dataset)
except ValueError as e:
if "unknown loss" in str(e):
raise ValueError("Only loss='mse' is supported; fix model kwargs and re-fit") from e
raise Prevention
- The loss kwarg is not validated at __init__ — check it yourself before fit() to avoid wasted data prep.
- Subclass and add an __init__ assertion if you manage many configs.
- Keep model-family-specific option sets documented next to your config files.
When it happens
Trigger: Calling model.fit(...) with loss set to anything other than 'mse' (e.g. 'mae', 'huber', 'binary'). The constructor accepts the string silently; the ValueError fires on the first batch of the first training epoch.
Common situations: Porting a config from a model with more losses (e.g. XGBoost/LightGBM params like 'reg:absoluteerror'); attempting MAE or Huber for robust regression; assuming the library auto-detects the loss from the label type.
Related errors
- optimizer {} is not supported!
- unknown metric `%s`
- unknown loss `%s`
- unknown loss `%s`
- loss {} is not supported!
AI-assisted analysis of microsoft/qlib@79633dd950 (2026-08-15).
Data as JSON: /api/errors/f2578e01eff96a2c.
Report an issue: GitHub.