microsoft/qlib · error · ValueError
unknown loss `%s`
Error message
unknown loss `%s`
What it means
KRNNModel.loss_fn supports only 'mse' (masked mean squared error over non-NaN labels). Any other loss string raises ValueError on the first loss computation during fit().
Source
Thrown at qlib/contrib/model/pytorch_krnn.py:359
self.fitted = False
self.krnn_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 get_daily_inter(self, df, shuffle=False):
# organize the train data into daily batches
daily_count = df.groupby(level=0, group_keys=False).size().values
daily_index = np.roll(np.cumsum(daily_count), 1)
daily_index[0] = 0
if shuffle:
# shuffle data
daily_shuffle = list(zip(daily_index, daily_count))
np.random.shuffle(daily_shuffle)
View on GitHub (pinned to 79633dd950)
Solutions
- Set loss='mse'
- Subclass and override loss_fn for custom losses
Example fix
# before KRNNModel(loss="huber") # after KRNNModel(loss="mse")
Defensive patterns
Strategy: validation
Validate before calling
assert loss == "mse", "KRNNModel supports only loss='mse'"
Type guard
def is_supported_krnn_loss(name: str) -> bool:
return name == "mse" Prevention
- Pin loss='mse' in KRNN configs
- Subclass for custom losses instead of passing unsupported names
When it happens
Trigger: KRNNModel(loss=anything != 'mse'), then fit(); also triggered by a hyperparameter-search grid containing unsupported loss values.
Common situations: Shared hyperparameter configs across model types where some models accept other losses; typos.
Related errors
- unknown loss `%s`
- optimizer {} is not supported!
- unknown metric `%s`
- unknown loss `%s`
- This type of input {rtype} is not supported
AI-assisted analysis of microsoft/qlib@79633dd950 (2026-08-15).
Data as JSON: /api/errors/a0f7e3b6f8625170.
Report an issue: GitHub.