在Keras Tuner搜索中使用ImageDataGenerator时出错 [英] Error using ImageDataGenerator with Keras Tuner search
本文介绍了在Keras Tuner搜索中使用ImageDataGenerator时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用tensorflow.keras.preprocessing.image import ImageDataGenerator 中的这是代码:
I am using from tensorflow.keras.preprocessing.image import ImageDataGenerator
Here is the code :
(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调谐器HyperModel构建模型:
then I am building a model using 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
此后,为了最好地搜索,出现了一个错误:
After that, in an attempt to search best got an error :
如何处理?
推荐答案
看起来像 Sparse_categorical_crossentropy
并不是生成器的最佳选择,这就是整个问题.
Looks like Sparse_categorical_crossentropy
wasn't the best choice for the generator and that was the whole problem.
参考: https://github.com/keras-team/keras-tuner/issues/223
这篇关于在Keras Tuner搜索中使用ImageDataGenerator时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文