How to get the output shape of a layer in Keras?

后端 未结 1 1254
一生所求
一生所求 2021-02-07 03:31

I have the following code in Keras (Basically I am modifying this code for my use) and I get this error:

\'ValueError: Error when checking target: expected conv3d_3 to h

相关标签:
1条回答
  • 2021-02-07 03:36

    You can get the output shape of a layer by layer.output_shape.

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

    Gives you:

    (None, None, 64, 64, 40)
    (None, None, 64, 64, 40)
    (None, None, 64, 64, 40)
    (None, None, 64, 64, 40)
    (None, None, 64, 64, 40)
    (None, None, 64, 64, 40)
    (None, None, 64, 64, 40)
    (None, None, 64, 64, 40)
    (None, None, 64, 64, 1)
    

    Alternatively you can pretty print the model using model.summary:

    model.summary()
    

    Gives you the details about the number of parameters and output shapes of each layer and an overall model structure in a pretty format:

    _________________________________________________________________
    Layer (type)                 Output Shape              Param #   
    =================================================================
    conv_lst_m2d_1 (ConvLSTM2D)  (None, None, 64, 64, 40)  59200     
    _________________________________________________________________
    batch_normalization_1 (Batch (None, None, 64, 64, 40)  160       
    _________________________________________________________________
    conv_lst_m2d_2 (ConvLSTM2D)  (None, None, 64, 64, 40)  115360    
    _________________________________________________________________
    batch_normalization_2 (Batch (None, None, 64, 64, 40)  160       
    _________________________________________________________________
    conv_lst_m2d_3 (ConvLSTM2D)  (None, None, 64, 64, 40)  115360    
    _________________________________________________________________
    batch_normalization_3 (Batch (None, None, 64, 64, 40)  160       
    _________________________________________________________________
    conv_lst_m2d_4 (ConvLSTM2D)  (None, None, 64, 64, 40)  115360    
    _________________________________________________________________
    batch_normalization_4 (Batch (None, None, 64, 64, 40)  160       
    _________________________________________________________________
    conv3d_1 (Conv3D)            (None, None, 64, 64, 1)   1081      
    =================================================================
    Total params: 407,001
    Trainable params: 406,681
    Non-trainable params: 320
    _________________________________________________________________
    

    If you want to access information about a specific layer only, you can use name argument when constructing that layer and then call like this:

    ...
    model.add(ConvLSTM2D(..., name='conv3d_0'))
    ...
    
    model.get_layer('conv3d_0')
    

    EDIT: For reference sake it will always be same as layer.output_shape and please don't actually use Lambda or custom layers for this. But you can use Lambda layer to echo the shape of a passing tensor.

    ...
    def print_tensor_shape(x):
        print(x.shape)
        return x
    model.add(Lambda(print_tensor_shape))
    ...
    

    Or write a custom layer and print the shape of the tensor on call().

    class echo_layer(Layer):
    ...
        def call(self, x):
            print(x.shape)
            return x
    ...
    
    model.add(echo_layer())
    
    0 讨论(0)
提交回复
热议问题