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'
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With