在Keras中训练Autoencoder进行无监督学习时的ValueError

时间:2018-01-31 16:59:06

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

我正在尝试使用Keras中的自动编码器,使用Indian Pines数据集对高光谱图像进行无监督分类。我已经开始使用项目https://github.com/KonstantinosF/Classification-of-Hyperspectral-Image并修改了TrainTheModel.ipynb中的部分,使用以下代码替换“训练模型”部分(以及以下两个代码块) ,这只是自动编码器的编码方面:

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

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

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

检查您的X_train,y_train形状

(同样要小心Failed to start service jboss.undertow.deployment.default-server.default-host.<<war File Name>>: org.jboss.msc.service.StartException in service jboss.undertow.deployment.default-server.default-host.<<war File Name>>: java.lang.RuntimeException: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'camelContext' defined in class path resource [com/.../CamelConfiguration.class]: Invocation of init method failed; nested exception is org.apache.camel.FailedToCreateRouteException: Failed to create route XXXRequestRoute: Route(XXXRequestRoute)[[From[webSphereMQ:queue:<<queue name>> because of Failed to resolve endpoint: webSphereMQ://queue:<<queue name>>?concurrentConsumers=5 due to: No component found with scheme: webSphereMQ Caused by: java.lang.RuntimeException: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'camelContext' defined in class path resource [com/.../CamelConfiguration.class]: Invocation of init method failed; nested exception is org.apache.camel.FailedToCreateRouteException: Failed to create route XXXRequestRoute: Route(XXXRequestRoute)[[From[webSphereMQ:queue:<<queue name>>... because of Failed to resolve endpoint: webSphereMQ://queue:<<queue name>>?concurrentConsumers=5 due to: No component found with scheme: webSphereMQ image_data_formatchannels_last