[英]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.我对使用 tensorflow 还是很陌生,非常感谢您对此的帮助。 I am training an autoencoder and I am trying to load the data input with the tensorflow.data pipeline.
我正在训练一个自动编码器,我正在尝试使用 tensorflow.data 管道加载数据输入。 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) datatrain1.shape 是 (18820, 16, 256, 1)
This is how I defined the dataset dataset = tf.data.Dataset.from_tensor_slices((datatrain1, datatrain1))
这就是我定义数据集
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.compile("adam", loss="mse")
autoencoder.fit(dataset,epochs=1, shuffle=True)
The above call to fit gives me the error:上面的 fit 调用给了我错误:
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:这是model定义代码:
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总结:
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.您不需要 Flatten 层,因为输入是 2D 的。
Remove flat = Flatten()(x)
移除
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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.