Search code examples
tensorflowdeep-learningtheanokeras

print out the shape of each layer in the net architecture


In Keras, we can define the network as follows. Are there any way to output the shape after each layer. For instance, I want to print out the shape of inputs after the line defining inputs, then print out the shape of conv1 after the line defining conv1, etc.

inputs = Input((1, img_rows, img_cols))
conv1 = Convolution2D(64, 3, 3, activation='relu', init='lecun_uniform', W_constraint=maxnorm(3), border_mode='same')(inputs)
conv1 = Convolution2D(64, 3, 3, activation='relu', init='lecun_uniform', W_constraint=maxnorm(3), border_mode='same')(conv1)
pool1 = MaxPooling2D(pool_size=(2, 2))(conv1)

conv2 = Convolution2D(128, 3, 3, activation='relu', init='lecun_uniform', W_constraint=maxnorm(3), border_mode='same')(pool1)
conv2 = Convolution2D(128, 3, 3, activation='relu', init='lecun_uniform', W_constraint=maxnorm(3), border_mode='same')(conv2)
pool2 = MaxPooling2D(pool_size=(2, 2))(conv2)

Solution

  • If a layer has a single node (i.e. if it isn't a shared layer), you can get its input tensor, output tensor, input shape and output shape via: layer.input_shape

    from keras.utils.layer_utils import layer_from_config
    
    config = layer.get_config()
    layer = layer_from_config(config)
    

    Source: https://keras.io/layers/about-keras-layers/

    May be this the easiest way to do:

    model.layers[layer_of_interest_index].output_shape