microsoft/qlib · error · ValueError
unknown loss `%s`
Error message
unknown loss `%s`
What it means
Thrown by TCNTSModel.loss_fn, which computes the training/validation loss. Only loss='mse' is implemented; every other value of the `loss` hyperparameter reaches the terminal raise. It fires on the first batch of training or validation.
Source
Thrown at qlib/contrib/model/pytorch_tcn_ts.py:155
self.fitted = False
self.TCN_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, data_loader):
self.TCN_model.train()
for data in data_loader:
data = torch.transpose(data, 1, 2)
feature = data[:, 0:-1, :].to(self.device)
label = data[:, -1, -1].to(self.device)
pred = self.TCN_model(feature.float())View on GitHub (pinned to 79633dd950)
Solutions
- Set loss='mse' (exact lowercase) in the TCNTSModel config — the only supported loss.
- If another loss is required, subclass TCNTSModel and override loss_fn (e.g. add an mae branch) while keeping the same NaN-masking behavior.
Example fix
# before model = TCNTSModel(..., loss="mae") # after model = TCNTSModel(..., loss="mse")
Defensive patterns
Strategy: validation
Validate before calling
assert model_kwargs.get("loss", "mse") == "mse", "TCNTSModel supports only loss='mse'" Try / catch
try:
model.fit(ds, valid)
except ValueError as e:
if "unknown loss" in str(e):
model_kwargs["loss"] = "mse"
model = TCNTSModel(**model_kwargs)
model.fit(ds, valid)
else:
raise Prevention
- Treat 'mse' as the only loss string for TCN-family models in shared configs.
- Normalize hyperparameter strings to lowercase on config load to avoid case mismatches.
When it happens
Trigger: Constructing TCNTSModel with loss='mae', loss='huber', or anything other than 'mse', then calling fit(); train_epoch calls loss_fn on the first batch and raises immediately.
Common situations: Copying loss settings from custom models; assuming the string used for `metric`/`loss` in other frameworks applies; typo like 'MSE' (uppercase) which fails the exact '==' comparison.
Related errors
- unknown loss `%s`
- unknown metric `%s`
- optimizer {} is not supported!
- unknown metric `%s`
- mode {} is not supported!
AI-assisted analysis of microsoft/qlib@79633dd950 (2026-08-15).
Data as JSON: /api/errors/0cd6765fc41cbc79.
Report an issue: GitHub.