C2_W2_Assignment Practice Lab error: This model has not yet been built

I’ve finished all the assignments and the grader says it’s all correct. But the line “model.summary()” shows the error:

ValueError: This model has not yet been built. Build the model first by calling 'build()' or by calling the model on a batch of data.

Have you done model.compile() and model.fit()!

I’m just filling the code between "### START CODE HERE ### " and "### END CODE HERE ### ". After the “# UNQ_C2 # GRADED CELL: Sequential model” goes the cell with “model.summary()”. Don’t think I could add it accidentally. I don’t know how to clean all my changes and start from scratch, so can’t check if this line was there from the start.

Now it looks like this:

# UNQ_C2
# GRADED CELL: Sequential model
tf.random.set_seed(1234) # for consistent results
model = Sequential(
        ### START CODE HERE ### 
        # My code here       
        ### END CODE HERE ### 
    ], name = "my_model" 

Then this cell:

Update: I’ve started from scratch and the above is how it looks from the start.

Try running all of the cells in the notebook.

I’ve tried. The same result.


@slava.t Have you tried adding a tf.keras.Input layer to your model? or adding an input_shape argument to the first layer of the model?

Without information about the input TensorFlow has no way to know what the Shape of the input data is going to look like and cannot create the model until it gets the first piece of data and infers the shape from there.

See the Keras Sequential Documentation by Francois Chollet The Sequential model

Hope this helps!


Thanks. Adding a tf.keras.Input layer helped.

It’s kind of confusing, that the previous labs say that this layer is optional and in this lab this layer is necessary to prevent an error in the next cell. I somehow missed the line “We will include that here to illuminate some model sizing.”

This worked to resolve the error, but I agree that this step was not clear at all. It also required adding “Input” to the import from Keras models line in the first executable cell in the notebook.

1 Like

alternatively one can add the named argument “input_shape=(400,)” to the first layer of the model instead of adding “Input” to the import from Keras models line in the first executable cell in the notebook.

1 Like

Hard-coding the size of the input shape is usually not a good idea. But it’s done in this part of the assignment to force the model.summary() function to have some information to display.

It’s weakly mentioned in this text from the notebook: