This model has not yet been built error on model.summary
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
In the realm of machine learning and neural network development, Keras, a high-level neural networks API, simplifies the process of creating and working with deep learning models. A common issue that developers encounter is the "This model has not yet been built" error when attempting to run model.summary(). This article delves into the underlying reasons for this error, provides solutions, and discusses related concepts to facilitate a deeper understanding.
Understanding the Error
What is model.summary()?
model.summary() is a Keras function that provides a summary of the model architecture. It displays information such as:
- Layer types
- Output shapes
- Number of parameters (trainable and non-trainable)
For this method to output meaningful information, the model needs to be fully defined.
The Error Explained
The error "This model has not yet been built" typically arises when:
- The model has layers with undefined input shapes.
- The model has not been compiled with necessary input and output configurations.
- The layers of the model have not been connected together, usually due to the use of the Functional API or Sequential API incorrectly.
Code Example
Consider the following example which leads to the error:
In the above code, the first Dense layer does not have an input shape defined. As a result, model.summary() throws an error because the model lacks the shape specification necessary for building.
Resolving the Error
Specify Input Shapes
Ensuring that the input shape of the first layer is specified solves the error:
In this corrected example, input_shape=(64,) tells Keras the expected shape of the input data which allows the model to be built.
Build the Model Explicitly
For more dynamic models using the Functional API, the model should be explicitly built:
Here, the model is constructed with defined inputs and outputs, allowing model.summary() to work correctly.
Key Points Summary
| Topic | Key Details |
| Model Building | Necessary for running model.summary(). |
| Input Shape | Must be specified for the first layer. |
| Sequential API | Define input shape during the first layer setup. |
| Functional API | Ensure explicit building with input and output. |
| Error Resolution | Specify input or build model explicitly. |
Additional Considerations
Custom Layers and Models
When defining custom layers or models, ensure that their input shape expectations are clearly defined. Use super(your_custom_class, self).__init__() properly to initialize base classes.
Model Compilation
Ensure that the model is compiled appropriately using model.compile(optimizer, loss, metrics) as part of the setup, though not directly related to the summary() error, it is crucial for training sessions.
Debugging Workflow
- Utilize
model.build(input_shape)if a model is planned to be created dynamically. - Implement logging around model input definitons or summaries to catch potential oversights.
Understanding these components reduces complications when working with complex architectures. By resolving the "This model has not yet been built" error, developers can focus on refining their model's performance and leveraging Keras more effectively.
Related reading
- This TensorFlow binary is optimized with IntelR MKL-DNN to use the following CPU instructions in performance critical
- This version of TensorFlow Probability requires TensorFlow version 2.3
- thresholds in roc_curve in scikit learn
- TicTacToe AI Making Incorrect Decisions
- Thread window is empty
- ''threading'' object has no attribute ''Thread''
- Tied weights in Autoencoder
- Time cost of training with pytorch DDP with multi-GPUs
.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 courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.