gpt4 book ai didi

python - `.fit()` 的输入应具有等级 4

转载 作者:行者123 更新时间:2023-12-04 10:45:00 26 4
gpt4 key购买 nike

我正在使用 keras 学习数据增强。链接:https://keras.io/preprocessing/image/该链接使用 CNN,但是当我尝试使用下面给出的密集层时,出现错误。错误在 datagen.fit()。描述说输入应该是等级 4。如何解决?

#import dataset
(X_train, y_train), (X_test, y_test) = cifar10.load_data()

#change shape from image to vector
X_train = X_train.reshape(50000, 32 * 32 * 3)
X_test = X_test.reshape(10000, 32 * 32 * 3)

#preprocess
X_train = X_train.astype('float32')
X_test = X_test.astype('float32')
X_train /= 255.0
X_test /= 255.0

#change labels from numeric to one hot encoded
Y_train = to_categorical(y_train, 10)
Y_test = to_categorical(y_test, 10)

model = Sequential()
model.add(Dense(1024, input_shape=(3072, )))
model.add(Activation('relu'))
model.add(Dense(10))
model.add(Activation('softmax'))

model.compile(loss='categorical_crossentropy',
optimizer='adam',
metrics=['accuracy'])


datagen = ImageDataGenerator(
featurewise_center=True,
featurewise_std_normalization=True,
rotation_range=20,
width_shift_range=0.2,
height_shift_range=0.2,
horizontal_flip=True)


datagen.fit(X_train)

# fits the model on batches with real-time data augmentation:
model.fit_generator(datagen.flow(X_train, Y_train, batch_size=32),
steps_per_epoch=len(X_train) / 32, epochs=epochs,verbose=1,
validation_data=datagen.flow(X_test, Y_test, batch_size=32))

错误

---------------------------------------------------------------------------
ValueError
---> 57 datagen.fit(X_train)


ValueError: Input to `.fit()` should have rank 4. Got array with shape: (50000, 3072)

最佳答案

你 reshape 了你的数组。 ImageDataGenerator 需要 4 阶输入矩阵(图像索引、高度、宽度、深度)。您的 Reshaping 给出了 2 阶输入矩阵。因此错误。解决方法是删除 reshape ,然后在第一个密集层上方添加一个 CNN 层(只是建议)。

关于python - `.fit()` 的输入应具有等级 4,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59745740/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com