This model has not yet been built error on model.summary() This model has not yet been built error on model.summary() python python

This model has not yet been built error on model.summary()


The error says what to do:

This model has not yet been built. Build the model first by calling build()

model.build(input_shape) # `input_shape` is the shape of the input data                         # e.g. input_shape = (None, 32, 32, 3)model.summary()


There is a very big difference between keras subclassed model and other keras models (Sequential and Functional).

Sequential models and Functional models are datastructures that represent a DAG of layers. In simple words, Functional or Sequential model are static graphs of layers built by stacking one on top of each other like LEGO. So when you provide input_shape to first layer, these (Functional and Sequential) models can infer shape of all other layers and build a model. Then you can print input/output shapes using model.summary().

On the other hand, subclassed model is defined via the body (a call method) of Python code. For subclassed model, there is no graph of layers here. We cannot know how layers are connected to each other (because that's defined in the body of call, not as an explicit data structure), so we cannot infer input / output shapes. So for a subclass model, the input/output shape is unknown to us until it is first tested with proper data. In the compile() method, we will do a deferred compile and wait for a proper data. In order for it to infer shape of intermediate layers, we need to run with a proper data and then use model.summary(). Without running the model with a data, it will throw an error as you noticed. Please check GitHub gist for complete code.

The following is an example from Tensorflow website.

import tensorflow as tffrom tensorflow import kerasfrom tensorflow.keras import layersclass ThreeLayerMLP(keras.Model):  def __init__(self, name=None):    super(ThreeLayerMLP, self).__init__(name=name)    self.dense_1 = layers.Dense(64, activation='relu', name='dense_1')    self.dense_2 = layers.Dense(64, activation='relu', name='dense_2')    self.pred_layer = layers.Dense(10, name='predictions')  def call(self, inputs):    x = self.dense_1(inputs)    x = self.dense_2(x)    return self.pred_layer(x)def get_model():  return ThreeLayerMLP(name='3_layer_mlp')model = get_model()(x_train, y_train), (x_test, y_test) = keras.datasets.mnist.load_data()x_train = x_train.reshape(60000, 784).astype('float32') / 255x_test = x_test.reshape(10000, 784).astype('float32') / 255model.compile(loss=keras.losses.SparseCategoricalCrossentropy(from_logits=True),              optimizer=keras.optimizers.RMSprop())model.summary() # This will throw an error as follows# ValueError: This model has not yet been built. Build the model first by calling `build()` or calling `fit()` with some data, or specify an `input_shape` argument in the first layer(s) for automatic build.# Need to run with real data to infer shape of different layershistory = model.fit(x_train, y_train,                    batch_size=64,                    epochs=1)model.summary()

Thanks!


Another method is to add the attribute input_shape() like this:

model = Sequential()model.add(Bidirectional(LSTM(n_hidden,return_sequences=False, dropout=0.25, recurrent_dropout=0.1),input_shape=(n_steps,dim_input)))