microsoft/qlib · error · ValueError
unknown loss `%s`
Error message
unknown loss `%s`
What it means
Raised by GRUModelTS.loss_fn when self.loss is not "mse". The TS variant supports only weighted MSE (it accepts an optional weight tensor for reweighter support); all other loss names raise on the first training batch.
Source
Thrown at qlib/contrib/model/pytorch_gru_ts.py:154
@property
def use_gpu(self):
return self.device != torch.device("cpu")
def mse(self, pred, label, weight):
loss = weight * (pred - label) ** 2
return torch.mean(loss)
def loss_fn(self, pred, label, weight=None):
mask = ~torch.isnan(label)
if weight is None:
weight = torch.ones_like(label)
if self.loss == "mse":
return self.mse(pred[mask], label[mask], weight[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.GRU_model.train()
for data, weight in data_loader:
feature = data[:, :, 0:-1].to(self.device)
label = data[:, -1, -1].to(self.device)
pred = self.GRU_model(feature.float())
loss = self.loss_fn(pred, label, weight.to(self.device))View on GitHub (pinned to 79633dd950)
Solutions
- Set loss="mse".
- Subclass GRUModelTS and extend loss_fn for a custom loss.
Example fix
# before GRUModelTS(loss="huber", ...) # after GRUModelTS(loss="mse", ...)
Defensive patterns
Strategy: validation
Validate before calling
assert params["loss"] == "mse", "GRUModelTS supports only loss='mse'"
Type guard
def is_supported_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("GRUModelTS only supports loss='mse'") from e
raise Prevention
- Pin loss='mse' in shared config templates for the TS model family.
- Subclass to add losses; do not try to pass custom names through config.
When it happens
Trigger: GRUModelTS(loss=<anything but "mse">) then fit(); the loss_fn is also invoked by metric_fn when metric is ""/"loss" during validation.
Common situations: Hyper-parameter search sweeps that include unsupported loss values; configs copied from other frameworks.
Related errors
AI-assisted analysis of microsoft/qlib@79633dd950 (2026-08-15).
Data as JSON: /api/errors/6c36ad7851ec926e.
Report an issue: GitHub.