Python 使用不匹配形状(Keras)的拟合生成器时出错

Python 使用不匹配形状(Keras)的拟合生成器时出错,python,machine-learning,keras,neural-network,Python,Machine Learning,Keras,Neural Network,我正在尝试建立一个简单的分类CNN,使用以下代码将一组1233张图像分为4类: unclassified_datagen = keras.preprocessing.image.ImageDataGenerator( rescale=1. / 255, horizontal_flip=True ) unclassified_generator = train_datagen.flow_from_directory( 'data/unclassified', tar

我正在尝试建立一个简单的分类CNN,使用以下代码将一组1233张图像分为4类:

unclassified_datagen = keras.preprocessing.image.ImageDataGenerator(
    rescale=1. / 255,
    horizontal_flip=True
)
unclassified_generator = train_datagen.flow_from_directory(
    'data/unclassified',
    target_size=(120, 120),
    batch_size=1233,
    class_mode='input',
    shuffle=False,
)

model_unclassified = keras.Sequential()
model_unclassified.add(layers.Conv2D(1233, (3, 3), input_shape=(120, 120, 3), padding="SAME"))
model_unclassified.add(layers.Dense(64, activation='relu'))
model_unclassified.add(layers.Dense(4, activation='sigmoid'))

model_unclassified.compile(loss='sparse_categorical_crossentropy',
                           optimizer='rmsprop',
                           metrics=['accuracy'])
model_unclassified.fit_generator(unclassified_generator, epochs=1)

但我得到了以下错误:
ValueError:checking target时出错:预期密集型_2具有形状(120,120,1),但得到了具有形状(120,120,3)的数组。


我做错了什么?

您应该添加
Flatten
层,因为
Conv2D
为每个样本返回3D数组:

model_unclassified=keras.Sequential()
model_unclassified.add(layers.Conv2D(1233,(3,3),input_shape=(120120,3),padding=“SAME”))
模型_未分类。添加(layers.flatte())
model_unclassified.add(layers.Dense(64,activation='relu'))
模型_未分类。添加(层。密集(4,激活='sigmoid'))