2

我正在使用from 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

之后,在尝试搜索最佳时出现错误: 在此处输入图像描述

如何处理?

4

1 回答 1

0

看起来Sparse_categorical_crossentropy不是发电机的最佳选择,这就是整个问题。

参考:https ://github.com/keras-team/keras-tuner/issues/223

于 2020-02-02T08:31:20.983 回答