microsoft/qlib · error · ValueError
model is not fitted yet!
Error message
model is not fitted yet!
What it means
Thrown by TransformerModel.predict when the `fitted` flag is False. fitted is set only after a successful fit loop (best weights reloaded, checkpoint saved), so prediction on an untrained or failed-to-train Transformer model is blocked.
Source
Thrown at qlib/contrib/model/pytorch_transformer.py:217
stop_steps = 0
best_epoch = step
best_param = copy.deepcopy(self.model.state_dict())
else:
stop_steps += 1
if stop_steps >= self.early_stop:
self.logger.info("early stop")
break
self.logger.info("best score: %.6lf @ %d" % (best_score, best_epoch))
self.model.load_state_dict(best_param)
torch.save(best_param, save_path)
if self.use_gpu:
torch.cuda.empty_cache()
def predict(self, dataset: DatasetH, segment: Union[Text, slice] = "test"):
if not self.fitted:
raise ValueError("model is not fitted yet!")
x_test = dataset.prepare(segment, col_set="feature", data_key=DataHandlerLP.DK_I)
index = x_test.index
self.model.eval()
x_values = x_test.values
sample_num = x_values.shape[0]
preds = []
for begin in range(sample_num)[:: self.batch_size]:
if sample_num - begin < self.batch_size:
end = sample_num
else:
end = begin + self.batch_size
x_batch = torch.from_numpy(x_values[begin:end]).float().to(self.device)
with torch.no_grad():
pred = self.model(x_batch).detach().cpu().numpy()
View on GitHub (pinned to 79633dd950)
Solutions
- Call model.fit(dataset, evals_result) successfully first, then predict(dataset, segment='test').
- Guard evaluation stages on fit success (try/except around fit, skip predict on failure) and fix the root fit error.
- To restore a trained model: torch.load(save_path) → model.load_state_dict(...) → set model.fitted = True before predict.
Example fix
# before model = TransformerModel(d_feat=6) model.predict(dataset) # ValueError # after model = TransformerModel(d_feat=6) model.fit(dataset, evals_result) model.predict(dataset)
Defensive patterns
Strategy: validation
Validate before calling
if not getattr(model, "fitted", False):
raise RuntimeError("TransformerModel not fitted — call fit(dataset, evals_result) first") Type guard
def transformer_ready(model) -> bool:
return getattr(model, "fitted", False) and getattr(model, "model", None) is not None Try / catch
try:
preds = model.predict(dataset)
except ValueError as e:
if "not fitted" in str(e):
model.fit(dataset, evals_result)
preds = model.predict(dataset)
else:
raise Prevention
- Gate rolling-refit evaluation on fit success per fold.
- On checkpoint restore: load_state_dict(torch.load(path)) and set fitted=True.
When it happens
Trigger: TransformerModel.predict(dataset) without a prior successful fit(dataset, evals_result); or after a fit that errored mid-training (OOM, NaN loss, early crash) leaving fitted=False.
Common situations: Rolling-refit scripts where one fold's fit fails but predict still runs; notebook re-execution of only the predict cell; expecting a saved .bin checkpoint alone to make a fresh model instance predict-ready.
Related errors
- model is not fitted yet!
- model is not fitted yet!
- model is not fitted yet!
- model is not fitted yet!
- model is not fitted yet!
AI-assisted analysis of microsoft/qlib@79633dd950 (2026-08-15).
Data as JSON: /api/errors/39a9d33bf6df58ef.
Report an issue: GitHub.