简体   繁体   中英

ValueError: Input 0 of layer dense_24 is incompatible: expected axis -1 of input shape to have value 1024 but received input with shape [16, 512]

I am quite new to using tensorflow and I would really appreciate some help on this. I am training an autoencoder and I am trying to load the data input with the tensorflow.data pipeline. However, after doing this, I've been having problems with the input shape etc. Does anyone know how to fix this? Thank you very much!!!

datatrain1.shape is (18820, 16, 256, 1)
This is how I defined the dataset dataset = tf.data.Dataset.from_tensor_slices((datatrain1, datatrain1))
The dataset has shape of the following:
<TensorSliceDataset shapes: ((16, 256, 1), (16, 256, 1)), types: (tf.float32, tf.float32)>

Code for autoencoder autoencoder.compile("adam", loss="mse")
autoencoder.fit(dataset,epochs=1, shuffle=True)
The above call to fit gives me the error:

ValueError: Input 0 of layer dense_24 is incompatible with the layer: expected axis -1 of input shape to have value 1024 but received input with shape [16, 512]

This is the model definition code:

n_clusters = 32

input_img = Input(shape=(16, 256, 1))
x = res_conv_block(input_img, 64, 2)

pool_1 = MaxPooling2D((1, 2), padding="same")(x)
x = res_conv_block(pool_1, 64, 2)

pool_2 = MaxPooling2D((2, 2), padding="same")(x)
x = res_conv_block(pool_2, 64, 2)

x = Conv2D(8, (3, 3), activation="relu", padding="same")(x)
x = MaxPooling2D((2, 2), padding="same")(x)


flat = Flatten()(x)
x = Dense(256, activation='relu')(flat)
encoded = Dense(64, activation='relu', name="encoded")(x)
x = Dense(256, activation='relu')(encoded)
x = Dense(1024, activation='relu')(x)
x = Reshape((4, 32, 8))(x)

x = UpSampling2D((2, 2))(x)
up_1 = Conv2DTranspose(64, (3, 3), activation="relu", padding="same")(x)

x = res_deconv_block(up_1, 64, 2)
up_2 = UpSampling2D((2, 2))(x)

x = res_deconv_block(up_2, 64, 2)
up_3 = UpSampling2D((1, 2))(x)

x = res_deconv_block(up_3, 64, 2)
decoded = Conv2DTranspose(1, (3, 3), padding="same", name="decoded")(x)

autoencoder= Model(inputs=input_img, outputs=decoded, name="autoencoder")
encoder = Model(inputs=input_img, outputs=encoded, name="encoder")
clustering_layer = ClusteringLayer(n_clusters, name='clustering_layer')(encoder.output)
# SOM_layer = 
idec = Model(inputs=autoencoder.input, outputs=[clustering_layer, decoded], name="res-idec")
#SOM_layer = model.add(SOM_Layer())

Summary of the model:

Model: "res-idec"
__________________________________________________________________________________________________
Layer (type)                    Output Shape         Param #     Connected to                     
==================================================================================================
input_11 (InputLayer)           [(None, 16, 256, 1)] 0                                            
__________________________________________________________________________________________________
conv2d_58 (Conv2D)              (None, 16, 256, 64)  640         input_11[0][0]                   
__________________________________________________________________________________________________
conv2d_59 (Conv2D)              (None, 16, 256, 64)  36928       conv2d_58[0][0]                  
__________________________________________________________________________________________________
add_48 (Add)                    (None, 16, 256, 64)  0           conv2d_59[0][0]                  
                                                                 input_11[0][0]                   
__________________________________________________________________________________________________
max_pooling2d_24 (MaxPooling2D) (None, 16, 128, 64)  0           add_48[0][0]                     
__________________________________________________________________________________________________
conv2d_60 (Conv2D)              (None, 16, 128, 64)  36928       max_pooling2d_24[0][0]           
__________________________________________________________________________________________________
conv2d_61 (Conv2D)              (None, 16, 128, 64)  36928       conv2d_60[0][0]                  
__________________________________________________________________________________________________
add_49 (Add)                    (None, 16, 128, 64)  0           conv2d_61[0][0]                  
                                                                 max_pooling2d_24[0][0]           
__________________________________________________________________________________________________
max_pooling2d_25 (MaxPooling2D) (None, 8, 64, 64)    0           add_49[0][0]                     
__________________________________________________________________________________________________
conv2d_62 (Conv2D)              (None, 8, 64, 64)    36928       max_pooling2d_25[0][0]           
__________________________________________________________________________________________________
conv2d_63 (Conv2D)              (None, 8, 64, 64)    36928       conv2d_62[0][0]                  
__________________________________________________________________________________________________
add_50 (Add)                    (None, 8, 64, 64)    0           conv2d_63[0][0]                  
                                                                 max_pooling2d_25[0][0]           
__________________________________________________________________________________________________
conv2d_64 (Conv2D)              (None, 8, 64, 8)     4616        add_50[0][0]                     
__________________________________________________________________________________________________
max_pooling2d_26 (MaxPooling2D) (None, 4, 32, 8)     0           conv2d_64[0][0]                  
__________________________________________________________________________________________________
flatten_8 (Flatten)             (None, 1024)         0           max_pooling2d_26[0][0]           
__________________________________________________________________________________________________
dense_24 (Dense)                (None, 256)          262400      flatten_8[0][0]                  
__________________________________________________________________________________________________
encoded (Dense)                 (None, 64)           16448       dense_24[0][0]                   
__________________________________________________________________________________________________
dense_25 (Dense)                (None, 256)          16640       encoded[0][0]                    
__________________________________________________________________________________________________
dense_26 (Dense)                (None, 1024)         263168      dense_25[0][0]                   
__________________________________________________________________________________________________
reshape_8 (Reshape)             (None, 4, 32, 8)     0           dense_26[0][0]                   
__________________________________________________________________________________________________
up_sampling2d_24 (UpSampling2D) (None, 8, 64, 8)     0           reshape_8[0][0]                  
__________________________________________________________________________________________________
conv2d_transpose_56 (Conv2DTran (None, 8, 64, 64)    4672        up_sampling2d_24[0][0]           
__________________________________________________________________________________________________
conv2d_transpose_57 (Conv2DTran (None, 8, 64, 64)    36928       conv2d_transpose_56[0][0]        
__________________________________________________________________________________________________
conv2d_transpose_58 (Conv2DTran (None, 8, 64, 64)    36928       conv2d_transpose_57[0][0]        
__________________________________________________________________________________________________
add_51 (Add)                    (None, 8, 64, 64)    0           conv2d_transpose_58[0][0]        
                                                                 conv2d_transpose_56[0][0]        
__________________________________________________________________________________________________
up_sampling2d_25 (UpSampling2D) (None, 16, 128, 64)  0           add_51[0][0]                     
__________________________________________________________________________________________________
conv2d_transpose_59 (Conv2DTran (None, 16, 128, 64)  36928       up_sampling2d_25[0][0]           
__________________________________________________________________________________________________
conv2d_transpose_60 (Conv2DTran (None, 16, 128, 64)  36928       conv2d_transpose_59[0][0]        
__________________________________________________________________________________________________
add_52 (Add)                    (None, 16, 128, 64)  0           conv2d_transpose_60[0][0]        
                                                                 up_sampling2d_25[0][0]           
__________________________________________________________________________________________________
up_sampling2d_26 (UpSampling2D) (None, 16, 256, 64)  0           add_52[0][0]                     
__________________________________________________________________________________________________
conv2d_transpose_61 (Conv2DTran (None, 16, 256, 64)  36928       up_sampling2d_26[0][0]           
__________________________________________________________________________________________________
conv2d_transpose_62 (Conv2DTran (None, 16, 256, 64)  36928       conv2d_transpose_61[0][0]        
__________________________________________________________________________________________________
add_53 (Add)                    (None, 16, 256, 64)  0           conv2d_transpose_62[0][0]        
                                                                 up_sampling2d_26[0][0]           
__________________________________________________________________________________________________
clustering_layer (ClusteringLay (None, 32)           2048        encoded[0][0]                    
__________________________________________________________________________________________________
decoded (Conv2DTranspose)       (None, 16, 256, 1)   577         add_53[0][0]                     
==================================================================================================
Total params: 977,417
Trainable params: 977,417
Non-trainable params: 0

You don't need Flatten layer, since input is 2D.

Remove flat = Flatten()(x)

x = Dense(256, activation='relu')(x)
encoded = Dense(64, activation='relu', name="encoded")(x)
x = Dense(256, activation='relu')(encoded)
x = Dense(1024, activation='relu')(x)
x = Reshape((4, 32, 8))(x)

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.

Related Question ValueError: Input 0 of layer dense is incompatible with the layer: expected axis -1 to have value 8 but received input with shape [None, 1] ValueError: Input 0 of layer sequential_16 is incompatible with the layer: expected axis -1 of input shape to have value 24 but Input 0 of layer dense is incompatible with the layer: expected axis -1 of input shape to have value 3 but received input with shape (None, 1) ValueError: Input 0 of layer sequential is incompatible with the layer: expected axis -1 of input shape to have value 3 but received input with shape 'ValueError: Input 0 of layer sequential is incompatible with the layer: expected axis -1 of input shape to have value 3 but received input ValueError: ...incompatible with the layer: expected axis -1 of input shape to have value 20 but received input with shape (None, 20, 637) “ValueError: …incompatible with the layer: expected axis -1 of input shape to have value 8 but received input with shape (None, 7, 169)” Input 0 of layer dense is incompatible with the layer: expected axis -1 of input shape to have value 2700 but received input with shape [None, 900] Input 0 of layer dense is incompatible with the layer: expected axis -1 of input shape to have value 8192 but received input with shape (None, 61608) Input 0 of layer dense_18 is incompatible with the layer: expected axis -1 of input shape to have value 3500 but received input with shape [None, 7]
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM