使用带有Keras调谐器搜索的ImageDataGenerator时出错

使用带有Keras调谐器搜索的ImageDataGenerator时出错,keras,tensorflow2.0,tf.keras,tuner,Keras,Tensorflow2.0,Tf.keras,Tuner,我正在使用tensorflow.keras.preprocessing.image import ImageDataGenerator中的 代码如下: (x_train, y_train), (x_test, y_test) = cifar10.load_data() x_train, x_test = x_train.astype('float32') / 255, x_test.astype('float32') / 255 y_train, y_test = to_categorical(

我正在使用tensorflow.keras.preprocessing.image import ImageDataGenerator中的
代码如下:

(x_train, y_train), (x_test, y_test) = cifar10.load_data()
x_train, x_test = x_train.astype('float32') / 255, x_test.astype('float32') / 255
y_train, y_test = to_categorical(y_train), to_categorical(y_test)

img_gen = ImageDataGenerator(
    rotation_range=10,
    width_shift_range=.1,
    height_shift_range=.1,
    horizontal_flip=True,
    vertical_flip=True,
    fill_mode="nearest"
)

img_gen_train = img_gen.flow(x_train, y_train, batch_size=128, shuffle=True)

rand_tuner = RandomSearch(hypermodel=hypermodel,
                          objective='val_acc',
                          max_trials=max_trials,
                          project_name='cifar10')
然后,我使用keras tuner HyperModel构建模型:

def hypermodel(hp):
    units_choice = hp.Int('units', min_value=32, max_value=512, step=32, default=128)
    lr_choice = hp.Float('learning_rate', 1e-5, 1e-2, sampling='LOG', default=1e-3)
    dropout_rate_choice = hp.Float('rate', 0, .5, step=.1, default=.2)
    filters_choice = hp.Choice('num_filters', values=[32, 64], default=64)

    model = Sequential()
    model.add(Conv2D(filters=16, kernel_size=3, 
                            activation='relu', input_shape=input_shape))
    model.add(Dropout(rate=dropout_rate_choice))
    model.add(Flatten())
    model.add(Dense(units=units_choice, activation='relu'))
    model.add(Dense(num_labels, activation='softmax'))
    model.compile(loss='sparse_categorical_crossentropy',
                optimizer=Adam(lr_choice),
                metrics=['accuracy'])

    return model
之后,在尝试搜索best时,出现了一个错误:


如何处理它?

看起来
稀疏\u分类\u交叉熵
不是生成器的最佳选择,这就是整个问题所在

参考: