gpt4 book ai didi

python - 使用 keras 中的 grid_search 和 flow_from_directory 进行 super 参数优化

转载 作者:行者123 更新时间:2023-12-02 06:37:01 25 4
gpt4 key购买 nike

我尝试优化用于图像分类的 keras CNN 中的超参数。我决定使用 sklearn 的网格搜索。我克服了从 keras flow_from_directory 中制作 x 和 y 的基本困难,但它仍然不起作用。最后一行错误ValueError:dropout不是合法参数

def grid_model(optimizer='adam',
kernel_initializer='random_uniform',
dropout=0.2,
loss='categorical_crossentropy'):

model = Sequential()

model.add(Conv2D(6,(5,5),activation="relu",padding="same",
input_shape=(img_width, img_height, 3)))
model.add(MaxPooling2D((2,2)))
model.add(Dropout(dropout))

model.add(Conv2D(16,(5,5),activation="relu"))
model.add(MaxPooling2D((2,2)))
model.add(Dropout(dropout))

model.add(Flatten())

model.add(Dense(120, activation='relu', kernel_initializer=kernel_initializer))
model.add(Dropout(dropout))
model.add(Dense(84, activation='relu', kernel_initializer=kernel_initializer))
model.add(Dropout(dropout))
model.add(Dense(10, activation='softmax'))

model.compile(loss=loss,
optimizer=optimizer,
metrics=['accuracy'])
return model

train_generator = ImageDataGenerator(rescale=1/255)

validation_generator = ImageDataGenerator(rescale=1/255)

# Retrieve images and their classes for train and validation sets
train_flow = train_generator.flow_from_directory(directory=train_data_dir,
batch_size=batch_size,
target_size=(img_height,img_width))

validation_flow = validation_generator.flow_from_directory(directory=validation_data_dir,
batch_size=batch_size,
target_size=(img_height,img_width),
shuffle = False)

clf = KerasClassifier(build_fn=grid_model(), epochs=epochs, verbose=0)

param_grid = {
'clf__optimizer':['adam', 'Nadam'],
'clf__epochs':[100, 200],
'clf__dropout':[0.1, 0.2, 0.5],
'clf__kernel_initializer':['normal','uniform'],
'clf__loss':['categorical_crossentropy',
'sparse_categorical_crossentropy',
'kullback_leibler_divergence']
}

pipeline = Pipeline([('clf',clf)])

(X_train, Y_train) = train_flow.next()

grid = GridSearchCV(pipeline, cv=2, param_grid=param_grid)
grid.fit(X_train, Y_train)

最佳答案

问题出在这一行:

clf = KerasClassifier(build_fn=grid_model(), epochs=epochs, verbose=0)

修改为

clf = KerasClassifier(build_fn=grid_model, epochs=epochs, verbose=0)

不应调用 grid_model 方法,但应传递对其的引用。

此外,在损失列表中,不能使用“sparse_categorical_crossentropy”(整数),因为模型所需的输出形状与“categorical_crossentropy”(one-hot)的输出形状不兼容。

关于python - 使用 keras 中的 grid_search 和 flow_from_directory 进行 super 参数优化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55552892/

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