简体   繁体   中英

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

I'm new to machine learning and tensorflow. I'm creating a network with Funtional API in keras and got an error.

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].

Thanks for any help, I'm a bit lost here.

My input is:

 (64,64,3)

Here is my function:

 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

See

input shapes: [1,?,64,64,8]

I think somehow you are getting an extra dimension added to your input tensor. I'm not sure why this is happening, since I have never had it happen to me.
I tried rewriting your model as a tf.keras.Sequential model and it seemed to work

    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)

After calling model.summary(), I get back:

Layer (type)---Output Shape---Param #


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


sequential (Sequential)------------(None, 64)-------------------2456


dense (Dense)----------------------(None, 6)---------------------390

Is that the shape you're looking for?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM