簡體   English   中英

Tensorflow: ValueError: Shape must be rank 4 but is rank 5

[英]Tensorflow: ValueError: Shape must be rank 4 but is rank 5

我是機器學習和 tensorflow 的新手。 我正在 keras 中使用功能 API 創建網絡並出現錯誤。

ValueError: Shape must be rank 4 but is rank 5 for '{{node max_pooling2d_13/MaxPool}} = MaxPool[T=DT_FLOAT, data_format="NHWC", ksize=[1, 8, 8, 1], padding="SAME", strides=[1, 8, 8, 1]](max_pooling2d_13/MaxPool/input)' with input shapes: [1,?,64,64,8].

感謝您的幫助,我在這里有點迷茫。

我的輸入是:

 (64,64,3)

這是我的功能:

 def convolutional_model(input_shape):
        """
        Implements the forward propagation for the model:
        CONV2D -> RELU -> MAXPOOL -> CONV2D -> RELU -> MAXPOOL -> FLATTEN -> DENSE
        """
        input_img = tf.keras.Input(shape=input_shape)
        
        print(input_img)
        ## CONV2D: 8 filters 4x4, stride of 1, padding 'SAME'
        # Z1 = None
        ## RELU
        # A1 = None
        ## MAXPOOL: window 8x8, stride 8, padding 'SAME'
        # P1 = None
        ## CONV2D: 16 filters 2x2, stride 1, padding 'SAME'
        # Z2 = None
        ## RELU
        # A2 = None
        ## MAXPOOL: window 4x4, stride 4, padding 'SAME'
        # P2 = None
        ## FLATTEN
        # F = None
        ## Dense layer
        ## 6 neurons in output layer. 
        # outputs = None
        
        
        Z1 = tfl.Conv2D(8, 4 ,strides = (1, 1) , padding='same')(input_img),    
        A1 = tfl.ReLU()(Z1), 
        P1 = tfl.MaxPool2D(pool_size=(8, 8), strides=(8, 8), padding='same')(A1),
        Z2 = tfl.Conv2D(16, (2, 2), strides = (1, 1), padding ="same")(P1),
        A2 = tfl.ReLU()(Z2), 
        P2 = tfl.MaxPool2D(pool_size = (4, 4), strides=(4, 4) , padding='same')(A2),
        F  = tfl.Flatten()(P2), 
        outputs = tfl.Dense(units= 6 , activation='softmax')(F),
        
        
        model = tf.keras.Model(inputs=input_img, outputs=outputs)
        
        return model

輸入形狀:[1,?,64,64,8]

我認為不知何故,您在輸入張量中添加了一個額外的維度。 我不確定為什么會發生這種情況,因為我從來沒有遇到過這種情況。
我嘗試將您的模型重寫為 tf.keras.Sequential 模型,它似乎有效

    result = tf.keras.Sequential()
    result.add(tf.keras.layers.Conv2D(8, 4 ,strides = 1 , padding='same'))
    result.add(tf.keras.layers.ReLU())
    result.add(tf.keras.layers.MaxPool2D(pool_size=(8, 8), strides=8, padding='same'))
    result.add(tf.keras.layers.Conv2D(16, 4 ,strides = 1 , padding='same'))
    result.add(tf.keras.layers.ReLU())
    result.add(tf.keras.layers.MaxPool2D(pool_size=(4, 4), strides=4, padding='same'))
    result.add(tf.keras.layers.Flatten())
    x=result(input_img)
    outputs = tf.keras.layers.Dense(units= 6 , activation='softmax')(x),
    model = tf.keras.Model(inputs=input_img, outputs=outputs)

調用model.summary()后,我回來了:

層(類型)---輸出形狀---參數#


input_1 (InputLayer)-------------[(None, 64, 64, 3)]--------------0


順序(Sequential)------------(無,64)-----------2456


密集(Dense)----------------------(無,6)------------------- --390

這是您要找的形狀嗎?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM