microsoft/qlib · error · ValueError

unknown loss `%s`

Error message

unknown loss `%s`

What it means

GATsTSModel.loss_fn implements a single branch, self.loss == 'mse'; any other value raises ValueError the first time the training loop computes the loss in fit(). This mirrors the non-ts GATs model: MSE is the only supported objective.

Source

Thrown at qlib/contrib/model/pytorch_gats_ts.py:174

        self.fitted = False
        self.GAT_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

  1. Keep loss='mse' (the default).
  2. Audit the loss hyperparameter for typos and cross-model copy-paste.
  3. Subclass GATsTSModel and add loss branches in loss_fn for custom objectives.

Example fix

# before
model = GATsTSModel(loss='mae')

# after
model = GATsTSModel(loss='mse')
Defensive patterns

Strategy: validation

Validate before calling

assert model.loss == 'mse', f"GATsTSModel only supports loss='mse', got {model.loss!r}"

Type guard

def is_supported_gats_loss(loss: str) -> bool:
    return loss == 'mse'

Try / catch

try:
    model.fit(dataset)
except ValueError as e:
    if 'unknown loss' in str(e):
        model.loss = 'mse'
        model.fit(dataset)
    else:
        raise

Prevention

When it happens

Trigger: GATsTSModel(loss='mae') or any non-'mse' string followed by fit(); the first batch triggers loss_fn and raises.

Common situations: Hyperparameter sweeps that vary loss across models; configs copied from other contrib models; blank or default-mismatched loss values in workflow YAML.

Related errors


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