【发布时间】:2021-07-06 00:57:24
【问题描述】:
我对使用 tensorflow 还是很陌生,非常感谢您对此提供一些帮助。我正在训练自动编码器,并尝试使用 tensorflow.data 管道加载数据输入。但是,这样做之后,我一直遇到输入形状等问题。有谁知道如何解决这个问题?非常感谢!!!
datatrain1.shape 为 (18820, 16, 256, 1)
这就是我定义数据集的方式
dataset = tf.data.Dataset.from_tensor_slices((datatrain1, datatrain1))
数据集的形状如下:<TensorSliceDataset shapes: ((16, 256, 1), (16, 256, 1)), types: (tf.float32, tf.float32)>
自动编码器代码
autoencoder.compile("adam", loss="mse") autoencoder.fit(dataset,epochs=1, shuffle=True)
上面的 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]
这是模型定义代码:
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())
模型总结:
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
【问题讨论】:
标签: python tensorflow machine-learning keras neural-network