【问题标题】:ValueError when training Autoencoder in Keras for unsupervised learning在 Keras 中训练自动编码器以进行无监督学习时出现 ValueError
【发布时间】:2018-07-10 21:06:20
【问题描述】:

我正在尝试使用 Keras 中的自动编码器,使用 Indian Pines 数据集对高光谱图像进行无监督分类。我在这里开始了一个项目https://github.com/KonstantinosF/Classification-of-Hyperspectral-Image 并修改了TrainTheModel.ipynb 中的部分,将 "Train the model" 部分(以及以下两个代码块)替换为以下代码,这只是自编码器的编码端:

input_img = Input(shape=input_shape)  # input_shape is (30, 5, 5)
x = Conv2D(16, (3, 3), activation='relu', padding='same')(input_img) # x1
x = MaxPooling2D((2, 2), padding='same')(x) # x2
x = Conv2D(8, (3, 3), activation='relu', padding='same')(x) # x3
x = MaxPooling2D((2, 2), padding='same')(x) # x4
x = Conv2D(8, (3, 3), activation='relu', padding='same')(x) #x5
encoded = MaxPooling2D((2, 2), padding='same')(x)

model = Model(input_img, encoded)
model.compile(optimizer='adadelta', loss='binary_crossentropy')

model.fit(X_train, y_train, batch_size=32, epochs=15)

我改编自 https://blog.keras.io/building-autoencoders-in-keras.html“卷积自动编码器” 部分。当我尝试运行命令时:

model.fit(X_train, y_train, batch_size=32, epochs=15)

命令,我收到错误消息:

ValueError: Error when checking target: expected max_pooling2d_25 to have 4 dimensions, but got array with shape (29685, 16)

由于我是 CNN 的新手,我不确定为什么我的尺寸不正确。如果我在这里打印出每个步骤的形状,我会得到:

(?, 16, 5, 5) # x1
(?, 16, 3, 3) # x2
(?, 8, 3, 3) # x3
(?, 8, 2, 2) # x4
(?, 8, 2, 2) # x5
(?, 8, 1, 1) # encoded

有什么想法吗?

【问题讨论】:

    标签: keras conv-neural-network autoencoder unsupervised-learning


    【解决方案1】:

    根据您的错误,我假设您的网络需要形状为 input_shape=(num,x,z,z) 的数据,而是输入形状为 (29685, 16) 的数据。

    检查您的 X_train、y_train 形状

    (还要小心你的image_data_format,无论是channels_last还是channels_first

    【讨论】:

      猜你喜欢
      • 2017-09-03
      • 1970-01-01
      • 1970-01-01
      • 2011-11-09
      • 1970-01-01
      • 2014-04-20
      • 1970-01-01
      • 2013-03-24
      • 2016-02-06
      相关资源
      最近更新 更多