Python 3.x 调整超参数Keras用于图像分类
我是否可以使用GridSearch或RandomizedSearch来调整keras模型中的超参数以进行图像分类?我想根据猫和狗的数据集对猫和狗进行分类Python 3.x 调整超参数Keras用于图像分类,python-3.x,keras,keras-layer,Python 3.x,Keras,Keras Layer,我是否可以使用GridSearch或RandomizedSearch来调整keras模型中的超参数以进行图像分类?我想根据猫和狗的数据集对猫和狗进行分类 class smallervggnet: @staticmethod def build(width, height, depth, classes, finalAct="softmax"): # initialize the model along with the input shape to be
class smallervggnet:
@staticmethod
def build(width, height, depth, classes, finalAct="softmax"):
# initialize the model along with the input shape to be
# "channels last" and the channels dimension itself
model = Sequential()
inputShape = (height, width, depth)
chanDim = -1
# if we are using "channels first", update the input shape
# and channels dimension
if K.image_data_format() == "channels_first":
inputShape = (depth, height, width)
chanDim = 1
# CONV => RELU => POOL
model.add(Conv2D(32, (3, 3), padding="same",
input_shape=inputShape))
model.add(Activation("relu"))
model.add(BatchNormalization(axis=chanDim))
model.add(MaxPooling2D(pool_size=(3, 3)))
model.add(Dropout(0.25))
# (CONV => RELU) * 2 => POOL
model.add(Conv2D(64, (3, 3), padding="same"))
model.add(Activation("relu"))
model.add(BatchNormalization(axis=chanDim))
model.add(Conv2D(64, (3, 3), padding="same"))
model.add(Activation("relu"))
model.add(BatchNormalization(axis=chanDim))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
# (CONV => RELU) * 2 => POOL
model.add(Conv2D(128, (3, 3), padding="same"))
model.add(Activation("relu"))
model.add(BatchNormalization(axis=chanDim))
model.add(Conv2D(128, (3, 3), padding="same"))
model.add(Activation("relu"))
model.add(BatchNormalization(axis=chanDim))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
# first (and only) set of FC => RELU layers
model.add(Flatten())
model.add(Dense(1024))
model.add(Activation("relu"))
model.add(BatchNormalization())
model.add(Dropout(0.5))
# softmax classifier
model.add(Dense(classes))
model.add(Activation(finalAct))
# return the constructed network architecture
return model
我想要调整的是卷积层或调整池层中的神经元
param_grid = {'neurons':[4, 8, 16, 32, 64],
'pooling': ['MaxPooling2D', 'AveragePooling2D', 'GlobalMaxPooling2D', 'GlobalAveragePooling2D']
}
其中参数网格应放入网格搜索或随机搜索中。我该怎么办?是的,这是可能的。 方法是使用sci工具包学习中的KerasClassifier
clf = KerasBatchClassifier(build_fn=create_model, epochs=epochs, shuffle=True)
grid = GridSearchCV(estimator=clf, param_grid=parameters,return_train_score=False)
grid_result = grid.fit(X,y)
print(grid_result.best_score_)
print(grid_result.best_params_)
create_model是构建函数,它接受参数
你应该考虑一些事情:
- 您没有在每个历元之后进行验证,也没有提前停止
- 使用10倍交叉验证(cv参数),非常耗时
- 不能很好地与发电机配合使用
为了解决这些问题,您可以尝试定制KerasClassifier的实现,我也这样做了。在我的方法中,我只做了3次交叉验证。我使用了20%的训练数据来利用早期停止,并将其用于处理发电机 我已经尝试过使用与您提到的相同的查询,但它不起作用。因为我的X.shape是(80,96,96,3),Y.shape是(80,2),这是我使用MultiLabelBinarizer执行的标签数据集。