Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keras: How to get layer shapes in a Sequential model

I would like to access the layer size of all the layers in a Sequential Keras model. My code:

model = Sequential() model.add(Conv2D(filters=32,                 kernel_size=(3,3),                 input_shape=(64,64,3)         )) model.add(MaxPooling2D(pool_size=(3,3), strides=(2,2))) 

Then I would like some code like the following to work

for layer in model.layers:     print(layer.get_shape()) 

.. but it doesn't. I get the error: AttributeError: 'Conv2D' object has no attribute 'get_shape'

like image 528
Toke Faurby Avatar asked May 02 '17 17:05

Toke Faurby


People also ask

How do you get the output shape of a layer in keras?

You can get the output shape with 'output. shape[1:]' command. It will get the shape of output layer and can be used for other purposes.

What are the layers in sequential model?

The Sequential API is a framework for creating models based on instances of the sequential() class. The model has one input variable, a hidden layer with two neurons, and an output layer with one binary output. Additional layers can be created and added to the model.

What is sequential () in keras?

Sequential. The core idea of Sequential API is simply arranging the Keras layers in a sequential order and so, it is called Sequential API. Most of the ANN also has layers in sequential order and the data flows from one layer to another layer in the given order until the data finally reaches the output layer.

How do you find the input shape of a keras model?

We can use the “. shape” function to find the shape of the image. It returns a tuple of integers that represent the shape and color mode of the image. In the above output, the first two integer values represent the shape of the image.


1 Answers

If you want the output printed in a fancy way:

model.summary() 

If you want the sizes in an accessible form

for layer in model.layers:     print(layer.get_output_at(0).get_shape().as_list()) 

There are probably better ways to access the shapes than this. Thanks to Daniel for the inspiration.

like image 153
Toke Faurby Avatar answered Sep 29 '22 19:09

Toke Faurby