microsoft/qlib · error · ValueError
unknown loss `%s`
Error message
unknown loss `%s`
What it means
Raised by SANDWITCH model loss_fn in qlib/contrib/model/pytorch_sandwich.py:241 when self.loss is not 'mse'. The sandwich model implements exactly one supervised loss — masked MSE over non-NaN labels — so any other loss string is a ValueError. The loss attribute comes from the constructor's loss kwarg.
Source
Thrown at qlib/contrib/model/pytorch_sandwich.py:241
self.fitted = False
self.sandwich_model.to(self.device)
@property
def use_gpu(self):
return self.device != torch.device("cpu")
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 train_epoch(self, x_train, y_train):
x_train_values = x_train.values
y_train_values = np.squeeze(y_train.values)
self.sandwich_model.train()
indices = np.arange(len(x_train_values))
np.random.shuffle(indices)
for i in range(len(indices))[:: self.batch_size]:View on GitHub (pinned to 79633dd950)
Solutions
- Set loss: 'mse' in the sandwich model kwargs (currently the only option).
- If another loss is required, subclass and override loss_fn() (keep the NaN mask) rather than modifying the library.
Example fix
# before kwargs: loss: mae # after kwargs: loss: mse
Defensive patterns
Strategy: validation
Validate before calling
assert config.get("loss", "mse") == "mse", "SANDWITCH model only supports loss='mse'" Type guard
def is_supported_sandwich_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("Set loss='mse'; other losses require overriding loss_fn()") from e
raise Prevention
- Do not copy loss names between qlib models; supported sets differ per class.
- Write per-model config templates validated against the class source.
When it happens
Trigger: Passing loss='mae', loss='huber', etc. to the sandwich model and calling fit(); the first train/valid epoch evaluation calls loss_fn and raises. Note metric in ('', 'loss') also funnels into loss_fn, so a bad loss breaks metric evaluation too.
Common situations: Reusing kwargs from DNNModelPytorch (which accepts 'binary') or from other contrib models with richer loss menus; expecting symmetric naming with LightGBM's objective strings.
Related errors
- unknown loss `%s`
- unknown loss `%s`
- unknown loss `%s`
- loss {} is not supported!
- optimizer {} is not supported!
AI-assisted analysis of microsoft/qlib@79633dd950 (2026-08-15).
Data as JSON: /api/errors/c23f5ad5440126b1.
Report an issue: GitHub.