microsoft/qlib · error · ValueError
model is not fitted yet!
Error message
model is not fitted yet!
What it means
Raised by GRUModelTS.predict when called before a completed fit(). The `fitted` flag flips True only after training, best-parameter restore, and checkpoint save; predict refuses to run otherwise because the GRU weights are random.
Source
Thrown at qlib/contrib/model/pytorch_gru_ts.py:283
stop_steps = 0
best_epoch = step
best_param = copy.deepcopy(self.GRU_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.GRU_model.load_state_dict(best_param)
torch.save(best_param, save_path)
if self.use_gpu:
torch.cuda.empty_cache()
def predict(self, dataset):
if not self.fitted:
raise ValueError("model is not fitted yet!")
dl_test = dataset.prepare("test", col_set=["feature", "label"], data_key=DataHandlerLP.DK_I)
dl_test.config(fillna_type="ffill+bfill")
test_loader = DataLoader(dl_test, batch_size=self.batch_size, num_workers=self.n_jobs)
self.GRU_model.eval()
preds = []
for data in test_loader:
feature = data[:, :, 0:-1].to(self.device)
with torch.no_grad():
pred = self.GRU_model(feature.float()).detach().cpu().numpy()
preds.append(pred)
return pd.Series(np.concatenate(preds), index=dl_test.get_index())
View on GitHub (pinned to 79633dd950)
Solutions
- Complete fit() before predict().
- For inference-only sessions, load the saved state dict into GRU_model and set model.fitted = True before predict.
- Fail the pipeline loudly on fit errors so predict is never reached.
Example fix
# before model = GRUModelTS(**params) model.predict(dataset) # ValueError # after model = GRUModelTS(**params) model.fit(dataset) model.predict(dataset)
Defensive patterns
Strategy: validation
Validate before calling
assert model.fitted, "fit GRUModelTS (or restore its checkpoint) before predict"
Type guard
def is_fitted(model) -> bool:
return bool(getattr(model, "fitted", False)) Try / catch
try:
model.predict(dataset)
except ValueError as e:
if "not fitted" in str(e):
model.fit(dataset)
model.predict(dataset)
else:
raise Prevention
- Gate predict on the fitted flag.
- For inference-only sessions, load the saved state dict and set model.fitted = True.
When it happens
Trigger: Calling predict(dataset) on a newly constructed GRUModelTS, or after fit() aborted (empty data, NaN collapse, early exception) leaving fitted False.
Common situations: Inference scripts that re-instantiate the model rather than loading the checkpoint; workflow backtest stage running after a silent training failure.
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/098dbced400bb268.
Report an issue: GitHub.