python - 如何将 Scikit-Learn 的 RandomizedSearchCV 与 Tensorflow 的 ImageDataGenerator 结合使用

标签 python tensorflow keras scikit-learn hyperparameters

我正在尝试使用 ImageDataGenerators 实现 RandomizedSearchCV。

这里是用于训练集的生成器的示例:

train_datagen = ImageDataGenerator(rotation_range=20,
                                        rescale=1./255,
                                        shear_range=0.2,
                                        zoom_range=0.25,
                                        horizontal_flip=True,
                                        width_shift_range=0.2,
                                        height_shift_range=0.2)

train_generator = train_datagen.flow_from_directory(paths["train_path"],
                                                batch_size=train_batch_size,
                                                class_mode='binary',
                                                target_size=(image_shape[0], image_shape[1]))

我对 validation_generator 做了同样的事情,最后模型被拟合为:

model_history = model.fit(train_generator, 
                        epochs=200, 
                        steps_per_epoch=train_steps_per_epoch,
                        validation_data=validation_generator, 
                        validation_steps=valdation_steps_per_epoch,
                        callbacks=[callbacks.EarlyStopping(patience=20)])

我想应用网格搜索(使用 sklearn RandomizedSearchCV)来优化我的模型,因此我使用了 SciKeras:

rnd_search_cv = RandomizedSearchCV(keras_reg, param_distribs, n_iter=10, cv=3)

其中 keras_reg 是封装 sklearn 模型的 KerasClassifierparam_distribs 是包含超参数值的字典。

最后我按如下方式安装了 RandomizedSearchCV 对象:

rnd_search_cv.fit(train_generator, 
                epochs=200, 
                steps_per_epoch=train_steps_per_epoch,
                validation_data=validation_generator, 
                validation_steps=valdation_steps_per_epoch,
                callbacks=[callbacks.EarlyStopping(patience=20)])

我有以下错误:

Traceback (most recent call last):
File "/home/docker_user/.local/lib/python3.8/site-packages/sklearn/model_selection/_validation.py", line 684, in _fit_and_score
    estimator.fit(X_train, **fit_params)
TypeError: fit() missing 1 required positional argument: 'y'

您知道如何集成 RandomizedSearchCV 和 ImageDataGenerator 对象吗?

最佳答案

我不推荐以下解决方案和方法。我强烈建议使用 KerasTuner但如果你想输入keras.preprocessing.image.DirectoryIterator就像你的 train_generatorRandomizedSearchCV您需要从 DirectoryIterator 中提取 XY 然后将它们输入到 RandomizedSearchCV 中,如下所示:

import numpy  as np

len_dataset = 100
batch_size = 32
image_size = (150,150)
channel_images = 3
num_clases = 5
x_train = np.empty((len_dataset*batch_size, 
              image_size[0], 
              image_size[1], 
              channel_images))
y_train = np.empty((len_dataset*batch_size, num_clases))

for idx in range(len_dataset):
    x_batch, y_batch = train_generator.__getitem__(idx)
    for i in range(x_batch.shape[0]):
        x_train[idx*batch_size+i] = x_batch[i]
        y_train[idx*batch_size+i] = y_batch[i]

rnd_search_cv.fit(x_train, y_train)

关于python - 如何将 Scikit-Learn 的 RandomizedSearchCV 与 Tensorflow 的 ImageDataGenerator 结合使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72647610/

相关文章:

Python 模拟 : How to test the number of calls on a recursive function?

使用 locals() 的 Python 字典理解给出 KeyError

python - 作为Docker镜像运行后,聊天机器人对话的CLI不再出现

python - Tensorflow 对象检测 API - 'ValueError: anchor_strides must be a list with the same length as self._box_specs'

python-3.x - 使用 LSTM 训练的模型仅预测所有值相同的值

python-3.x - 减少深度学习模型的输入维度

python - 这个语法在 Python 中是什么意思?

python - 属性错误 : 'Settings' object has no attribute 'MERCHANT_SETTINGS'

python - tensorflow.keras.preprocessing.text.Tokenizer.texts_to_sequences 的 Numpy 数组给出了奇怪的输出,list([2]) 而不是 [[2]]

keras - 如何为波斯语实现词嵌入