首页 > 解决方案 > ValueError: 层 max_pooling2d_12 的输入 0 与层不兼容:预期 ndim=4,发现 ndim=0。收到的完整形状:[]

问题描述

我该如何解决这个错误?

ValueError: Input 0 of layer max_pooling2d_12 is incompatible with the layer: expected ndim=4, found ndim=0. Full shape received: []

我已经尝试了所有值,例如 conv 、 inputs 和 ....

# UNQ_C1
# GRADED FUNCTION: conv_block
def conv_block(inputs=(96,128,3), n_filters=32, dropout_prob=0, max_pooling=True):
    """
    Convolutional downsampling block
    
    Arguments:
        inputs -- Input tensor
        n_filters -- Number of filters for the convolutional layers
        dropout_prob -- Dropout probability
        max_pooling -- Use MaxPooling2D to reduce the spatial dimensions of the output volume
    Returns: 
        next_layer, skip_connection --  Next layer and skip connection outputs
    """

    ### START CODE HERE
    conv = Conv2D(32, # Number of filters
                  3,   # Kernel size   
                  activation='relu',
                  padding='same',
                  kernel_initializer='he_normal')(inputs)
    conv = Conv2D(32, # Number of filters
                  3,   # Kernel size
                  activation='relu',
                  padding='same',
                  kernel_initializer='he_normal')(conv)
    ### END CODE HERE
    
    # if dropout_prob > 0 add a dropout layer, with the variable dropout_prob as parameter
    if dropout_prob > 0:
         ### START CODE HERE
        conv = dropout_prob
         ### END CODE HERE
         
        
    # if max_pooling is True add a MaxPooling2D with 2x2 pool_size
    if max_pooling:
        ### START CODE HERE
        next_layer = tf.keras.layers.MaxPooling2D(pool_size=(2,2))(conv)
        ### END CODE HERE
        
    else:
        next_layer = conv
        
    skip_connection = conv
    
    return next_layer, skip_connection

标签: python

解决方案


我认为问题在于您正在分配

conv = dropout_prob

所以 conv 是 tensorflow 的一个实例,而 dropout_prob 是一个数字,问题是你必须添加一个 dropout 层,变量 dropout_prob 作为参数。不设置它等于参数。

右行是:

conv = tf.keras.layers.Dropout(dropout_prob)(conv)

推荐阅读