gpt4 book ai didi

python - 图像分类模型的网格搜索超参数

转载 作者:行者123 更新时间:2023-11-30 09:40:27 25 4
gpt4 key购买 nike

概述

好的,所以我尝试使用 scikit-learn 为 Keras 中的图像分类模型网格搜索超参数。有人告诉我这是不可能的;然而,当我运行代码时,我将向您展示它产生了一些看起来像我所期望的东西。

那么,如果不可能使用 scikit-learn 来网格搜索图像分类模型的超参数,那么为什么我会得到相应的输出以及如何在 Keras 中网格搜索图像分类模型的超参数?

首先,我要感谢您阅读本文并提供帮助。

代码

import sys
from matplotlib import pyplot
from keras.models import Sequential
from keras.layers import Conv2D
from keras.layers import MaxPooling2D
from keras.layers import Dense
from keras.layers import Flatten
from keras.optimizers import SGD
from keras.preprocessing.image import ImageDataGenerator
import numpy
from sklearn.model_selection import GridSearchCV
from keras.wrappers.scikit_learn import KerasClassifier

def create_model():
model = Sequential()
model.add(Conv2D(32, (3, 3), activation='relu', kernel_initializer='he_uniform', padding='same', input_shape=(256, 256, 3)))
model.add(MaxPooling2D((2, 2)))
model.add(Conv2D(64, (3, 3), activation='relu', kernel_initializer='he_uniform', padding='same'))
model.add(MaxPooling2D((2, 2)))
model.add(Flatten())
model.add(Dense(128, activation='relu', kernel_initializer='he_uniform'))
model.add(Dense(44, activation='softmax'))
# compile model
opt = SGD(lr=0.001, momentum=0.9)
model.compile(optimizer=opt, loss='categorical_crossentropy', metrics=['accuracy'])
return model
seed = 7
numpy.random.seed(seed)

datagen = ImageDataGenerator(rescale=1.0/255.0)
dataset = datagen.flow_from_directory('dataset_dog_breeds/train/', class_mode='categorical')

X, Y = dataset.next()

model = KerasClassifier(build_fn=create_model, verbose=0)

batch_size = [10, 20, 40, 60, 80, 100]
epochs = [10, 50, 100]
param_grid = dict(batch_size=batch_size, epochs=epochs)
grid = GridSearchCV(estimator=model, param_grid=param_grid, n_jobs=1, cv=3)
grid_result = grid.fit(X, Y)
print("Best: %f using %s" % (grid_result.best_score_, grid_result.best_params_))
means = grid_result.cv_results_['mean_test_score']
stds = grid_result.cv_results_['std_test_score']
params = grid_result.cv_results_['params']
for mean, stdev, param in zip(means, stds, params):
print("%f (%f) with: %r" % (mean, stdev, param))

输出

Best: 0.093750 using {'batch_size': 40, 'epochs': 100}
0.031250 (0.043178) with: {'batch_size': 10, 'epochs': 10}
0.000000 (0.000000) with: {'batch_size': 10, 'epochs': 50}
0.062500 (0.045383) with: {'batch_size': 10, 'epochs': 100}
0.062500 (0.045383) with: {'batch_size': 20, 'epochs': 10}
0.031250 (0.043178) with: {'batch_size': 20, 'epochs': 50}
0.062500 (0.045383) with: {'batch_size': 20, 'epochs': 100}
0.000000 (0.000000) with: {'batch_size': 40, 'epochs': 10}
0.062500 (0.042137) with: {'batch_size': 40, 'epochs': 50}
0.093750 (0.004214) with: {'batch_size': 40, 'epochs': 100}
0.062500 (0.086356) with: {'batch_size': 60, 'epochs': 10}
0.031250 (0.043178) with: {'batch_size': 60, 'epochs': 50}
0.062500 (0.092702) with: {'batch_size': 60, 'epochs': 100}
0.000000 (0.000000) with: {'batch_size': 80, 'epochs': 10}
0.000000 (0.000000) with: {'batch_size': 80, 'epochs': 50}
0.000000 (0.000000) with: {'batch_size': 80, 'epochs': 100}
0.000000 (0.000000) with: {'batch_size': 100, 'epochs': 10}
0.031250 (0.043178) with: {'batch_size': 100, 'epochs': 50}
0.000000 (0.000000) with: {'batch_size': 100, 'epochs': 100}

最佳答案

当然,即使使用神经网络,也可以使用网格搜索进行超参数优化。然而,对于复杂的问题(涉及数十万个参数和庞大的数据集),这是根本不可行的。

当您遇到训练时间从几小时到几天的问题时,详尽的网格搜索效率极低,您可能会更好地自行调整超参数。

总之,您的结果可能完全有效 - 只是网格搜索在与神经网络一起使用时不能很好地扩展。

关于python - 图像分类模型的网格搜索超参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59023969/

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