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

  1. Set loss='mse' (exact lowercase) in the TCNTSModel config — the only supported loss.
  2. 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

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


AI-assisted analysis of microsoft/qlib@79633dd950 (2026-08-15). Data as JSON: /api/errors/0cd6765fc41cbc79. Report an issue: GitHub.