model building
model.summary
error handling
machine learning
debugging

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.

Practice ML system design

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:

python
1from keras.models import Sequential
2from keras.layers import Dense
3
4model = Sequential()
5model.add(Dense(32))
6# Attempting to print a summary without building the model
7model.summary()

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:

python
model = Sequential()
model.add(Dense(32, input_shape=(64,)))
model.summary()

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:

python
1from keras.models import Model
2from keras.layers import Input, Dense
3
4input_tensor = Input(shape=(64,))
5output_tensor = Dense(10)(input_tensor)
6model = Model(inputs=input_tensor, outputs=output_tensor)
7model.summary()

Here, the model is constructed with defined inputs and outputs, allowing model.summary() to work correctly.


Key Points Summary

TopicKey Details
Model BuildingNecessary for running model.summary().
Input ShapeMust be specified for the first layer.
Sequential APIDefine input shape during the first layer setup.
Functional APIEnsure explicit building with input and output.
Error ResolutionSpecify 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
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track 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.

Practice ML system design

All Rights Reserved.