首页 > 解决方案 > ValueError:检查目标时出错:预期的预测有 4 个维度,但得到了形状为 (256, 1, 1) 的数组

问题描述

我正在尝试训练模型 Keras,但我遇到了问题:

g = ImageDataGenerator(featurewise_center=True,
                    featurewise_std_normalization=True,
                    rotation_range=45,
                    width_shift_range=0.2,
                    height_shift_range=0.2,
                    horizontal_flip=True,
                    validation_split=validation_split,
                    preprocessing_function=lambda x: x / 127 - 1)

g_train = g.flow(x_train, y_train,
             batch_size=batch_size,
             subset='training')

g_valid = g.flow(x_train, y_train,
             batch_size=batch_size,
             shuffle=False,
             subset='validation')


history = network.fit_generator(g_train,
                          steps_per_epoch=len(x_train) / 32, 
                          epochs=epochs)

ValueError: Error when checking target: expected predictions to have 4 dimensions, but got array with shape (256, 1)

有人知道为什么吗?在我看来,这很像文档中的示例。

x_train.shape
(50000, 32, 32, 1)
y_train.shape
(50000, 1, 1)

标签: python-3.xkeras

解决方案


推荐阅读