AttributeError 'NoneType' object has no attribute 'fit_generator'
Data Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.
Introduction
This error means you are calling fit_generator on a variable that is actually None. In Keras and TensorFlow code, that usually happens because the model-building function forgot to return the model, a variable was overwritten, or old tutorial code still uses fit_generator even though modern Keras expects fit. The fix is to identify which object is None and then check why it never became a valid model or generator.
What The Error Is Really Saying
Python raises:
when you do something like this:
So the debugging question is not "why does fit_generator fail" but "why is model equal to None here?"
The Most Common Cause: Missing return
A very common mistake is to build a model inside a helper function but forget to return it.
The fix is simple:
Modern Keras Usually Uses fit, Not fit_generator
Another source of confusion is API age. In modern TensorFlow Keras, you usually call fit even when the input is a generator or a Sequence.
If you are following old code samples, replacing fit_generator with fit may be part of the cleanup.
Check Which Variable Is Actually None
Do not guess. Print or assert the objects involved right before the failing line.
If the generator itself might be the issue, inspect that too.
This immediately tells you whether the model, the data source, or some earlier factory function is broken.
Watch For Variable Overwrites
Sometimes the model was created correctly but got overwritten later.
This is why small, well-named variables and explicit checks matter in training scripts. Machine-learning code often grows quickly, and silent reassignments are easy to miss.
A Good Minimal Debugging Pattern
A reliable workflow is:
- build the model
- print the model summary
- verify the training data object
- call
fit
That keeps the failure close to the real cause instead of surfacing later as an attribute error.
Common Pitfalls
- Forgetting to
return modelfrom the model-construction function. - Following older tutorials and assuming
fit_generatoris still the standard API entry point. - Printing stack traces without checking which variable is actually
None. - Accidentally overwriting
modelor the generator variable later in the script. - Debugging the Keras API first when the real issue is ordinary Python control flow.
Summary
- The error means you called
fit_generatoronNone, not on a valid Keras model. - The most common cause is a helper function that forgot to return the model.
- In modern TensorFlow Keras,
fitusually replacesfit_generator. - Add assertions and prints immediately before the training call to see which object is
None. - Treat this as a Python object-lifecycle bug first and a Keras API problem second.
Related reading
- AttributeError 'SMOTE' object has no attribute 'fit_sample
- AttributeError 'str' object has no attribute 'decode' while Loading a Keras Saved Model
- AttributeError 'Tensor' object has no attribute 'numpy
- AttributeError 'Tensor' object has no attribute 'numpy' in Tensorflow 2.1
- AttributeError The layer has never been called and thus has no defined input shape
- AttributeError The layer has never been called and thus has no defined output shape
- AttributeError 'tuple' object has no attribute 'rank' when calling fit on a Keras model with custom generator
- Authorization Credentials Stripped --- django, elastic beanstalk, oauth
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseNo course covers this one yet
Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.
Build my courseData Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.