Repository: keras-team/keras

Issue:
Enhance error text for Model.summary()
For following python script,

```
from keras.models import Sequential
from keras.layers import Dense,Conv2D, Flatten

model=Sequential()
model.add(Conv2D(10,(3,3)))
model.add(Conv2D(10,(3,3)))
model.add(Flatten())
model.add(Dense(20))
model.add(Dense(10))

model.compile(loss='categorical_crossentropy',
              optimizer='sgd',
              metrics=['accuracy'])

model.summary()

```

In the keras version [2.2.2](https://github.com/keras-team/keras/releases/tag/2.2.2), the error message is like following

```
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-83-30ace2d20b7d> in <module>()
     13               metrics=['accuracy'])
     14 
---> 15 model.summary()

/opt/conda/lib/python3.6/site-packages/keras/engine/network.py in summary(self, line_length, positions, print_fn)
   1245         if not self.built:
   1246             raise ValueError(
-> 1247                 'This model has never been called, thus its weights '
   1248                 'have not yet been created, so no summary can be displayed. '
   1249                 'Build the model first '

ValueError: This model has never been called, thus its weights have not yet been created, so no summary can be displayed. Build the model first (e.g. by calling it on some test data).
```

But in the keras version [2.1.6](https://github.com/keras-team/keras/releases/tag/2.1.6), it is like following
```
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-8-30ace2d20b7d> in <module>()
      3 
      4 model=Sequential()
----> 5 model.add(Conv2D(10,(3,3)))
      6 model.add(Conv2D(10,(3,3)))
      7 model.add(Flatten())

/usr/local/lib/python3.6/dist-packages/keras/models.py in add(self, layer)
    482                     # know about its input shape. Otherwise, that's an error.
    483                     if not hasattr(layer, 'batch_input_shape'):
--> 484                         raise ValueError('The first layer in a '
    485                                          'Sequential model must '
    486                                          'get an `input_shape` or '

ValueError: The first layer in a Sequential model must get an `input_shape` or `batch_input_shape` argument.
```

**The difference here is that version 2.1.6 doesn't allow to add the first layer if input_shape is missing and version 2.2.2 allows to build the model even if input shape is missing**. I am new to this repository so, I am not sure about it but I think that keras 2.2.2 will figure out input_shape when fit() is called for the first time, and that is being suggested by error msg in version 2.2.2. From looking at the code I can say that it is checking whether or not the model is built or not before calling summary utility.

But error message seems misleading, it asks to fit the model before calling summary() but many times we need to see the number of parameters and output shapes (especially CNN) before actually training. And it is very likely that such error will occur due to **not specifying "input_shape" by mistake. And in such scenario debugging the error will be very difficult.**

So i am suggesting following error message

```
ValueError: This model has not yet been built, so no summary can be displayed. Build the model first (e.g. by calling it on some test data) or specify the 'input_shape' in the first layer
```

**Above suggested error message would help in debugging if "input_shape" is not specified due to a mistake and creating a model which computes input shape on the go is not the intention**

 **I will be happy to send PR to fix this issue, but I want to discuss it with the community first.**


Issue Guidelines List : 
- [X] Check that you are up-to-date with the master branch of Keras. You can update with:
pip install git+git://github.com/keras-team/keras.git --upgrade --no-deps

- [X] If running on TensorFlow, check that you are up-to-date with the latest version. The installation instructions can be found [here](https://www.tensorflow.org/get_started/os_setup).
   
    _Used keras 2.2.2 and 2.1.6_

- [X] If running on Theano, check that you are up-to-date with the master branch of Theano. You can update with:
pip install git+git://github.com/Theano/Theano.git --upgrade --no-deps

     _I am using tensorflow backend_

- [X] Provide a link to a GitHub Gist of a Python script that can reproduce your issue (or just copy the script here if it is short).
     
   _Provided above_



Discussion:
Thank you for the detailed report. You can see in the changelog that the structure of `Sequential` has changed (now a subclass of `Model`). But it would make sense in a UX perspective to throw the error when adding the first layer without specifying the input shape. We should get the old error message back since I would call this a regression. 

PR welcome. If you need help for the PR, you can ask here.
To be clear, we should get back to the old behavior, which is, throwing an error when adding the first layer without specifying the input shape.
@gabrieldemarmiesse  okay got it!! I will work on that and send a PR. 
I looked into some code and I found these lines of comments in [sequential.py](https://github.com/keras-team/keras/blob/c4b8049ce41a34d9b1133795d34fb8fa439939c0/keras/engine/sequential.py#L48-#L50). It seems that `input_shape` is kept optional intentionally.
Also, the current code does check that whether or not `batch_input_shape` exists or not but it doesn't do anything if found absent. According to me rather than throwing an exception if `batch_input_shape` is not found, it should display a warning such as 
```
Warning: input_shape or batch_input_shape is not provided so the model will not be built automatically. To build the model call the model.build(input_shape) or call the model.fit() on some data
```
@gabrieldemarmiesse What is your view on this?

Notes: 

1. [the line](https://github.com/blue-atom/keras/blob/c4b8049ce41a34d9b1133795d34fb8fa439939c0/keras/engine/sequential.py#L154) in sequential.py which checks for the "input_shape" or "batch_input_shape"
2. In base_layes.py "input_shape" and "batch_input_shape" are rendered same in [this segment](https://github.com/blue-atom/keras/blob/c4b8049ce41a34d9b1133795d34fb8fa439939c0/keras/engine/base_layer.py#L141-#L147) of code
This is something that changed recently. Thanks for bringing it to my attention.
I wasn't aware of the delayed building feature. In regard of this, I think we should go with your first idea which was to change the error message to 
```
ValueError: This model has not yet been built, so no summary can be displayed. Build the model first (e.g. by calling it on some test data) or specify the 'input_shape' in the first layer
```

Resolving patch:
diff --git a/keras/engine/network.py b/keras/engine/network.py
--- a/keras/engine/network.py
+++ b/keras/engine/network.py
@@ -1240,10 +1240,9 @@ def summary(self, line_length=None, positions=None, print_fn=None):
         """
         if not self.built:
             raise ValueError(
-                'This model has never been called, thus its weights '
-                'have not yet been created, so no summary can be displayed. '
-                'Build the model first '
-                '(e.g. by calling it on some test data).')
+                'This model has not yet been built. '
+                'Build the model first by calling build() or calling fit() with some data. '
+                'Or specify input_shape or batch_input_shape in the first layer for automatic build. ')
         return print_layer_summary(self,
                                    line_length=line_length,
                                    positions=positions,


Test patch: