C1W4 Lab 2 - calling summary() on an instance of ResNet


I have a question about the ResNet class in the example code of Lab2 of Course1, Week 4.

After instantiating and compiling our custom ResNet class, when I call the keras summary() method on the model object, it returns the following ValueError:

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.

This makes sense: if we create our model as a custom class, we do not use an Input layer (as opposed to the Functional API) so at this point the program has no way of knowing the input shape of the data we will use. The error message and the TensorFlow documentation say that we can pass an input_shape argument to the first layer of the model in order to pass that information, so that the computation of the subsequent output shapes by the summary() method will work.

But when I do this, i.e., I pass the input_shape=(28,28,1) extra argument to the first Conv2D layer in the ResNet class, I still get the exact same error as above.

What am I doing wrong?

Best regards,

The input_shape parameter works in the case of Sequential and Functional API as these construct static graphs of layers.

However, when the model is defined using model subclassing, you either need to build or fit the model before getting the model summary. Passing the input_shape parameter to the first layer won’t help in this case.

Thanks – the error message made me assume that summary() would also build the model when the input shape was explicitly passed in an argument. After I have explicitly called build(input_shape=(28,28,1)) or fit() on the object, I could then call summary() without error.

However, I noticed another thing. In the output of summary(), the output shapes of the various layers in the model will all be specified as ‘multiple’ instead of tuples of integers. That is not very informative. Does TensorFlow give this output because of the skip connections?