microsoft/qlib · error · ValueError

unknown loss `%s`

Error message

unknown loss `%s`

What it means

Raised by TCN model loss_fn in qlib/contrib/model/pytorch_tcn.py:154 when self.loss is not 'mse'. The TCN model implements a single supervised loss — MSE over non-NaN (masked) labels; any other value raises ValueError. metric_fn (''/'loss') delegates to loss_fn, so the bad value also breaks early-stopping evaluation.

Source

Thrown at qlib/contrib/model/pytorch_tcn.py:154

        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, x_train, y_train):
        x_train_values = x_train.values
        y_train_values = np.squeeze(y_train.values)

        self.tcn_model.train()

        indices = np.arange(len(x_train_values))
        np.random.shuffle(indices)

View on GitHub (pinned to 79633dd950)

Solutions

  1. Set loss: 'mse' (only supported value).
  2. Subclass the TCN model and override loss_fn(), keeping the NaN mask semantics.

Example fix

# before
kwargs:
  loss: binary

# after
kwargs:
  loss: mse
Defensive patterns

Strategy: validation

Validate before calling

assert config.get("loss", "mse") == "mse", "TCN model only supports loss='mse'"

Type guard

def is_supported_tcn_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("TCN supports only loss='mse'; subclass to change") from e
    raise

Prevention

When it happens

Trigger: loss='mae', 'huber', 'binary', etc. in TCN kwargs, then fit(); the first loss evaluation in train_epoch raises.

Common situations: Copying kwargs from DNNModelPytorch configs (which allow 'binary'); case typos like 'MSE'.

Related errors


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