在我们搭建完模型之后,一般需要对模型内预设定的参数进行搜索,来确定最佳的参数组合
常见的搜索方法是网格搜索(GridSearch)、随机搜索(RandomSearch)、贝叶斯搜索等…
可以使用的API一般是使用Sklearn中的GridSearchCV模块,但是该模块存在一个问题就是只能对单输入模型进行超参数搜索,而我们的模型可能不止有一个输入。
使用GridSearch可以实现单输入模型的网格搜索
from sklearn.model_selection import GridSearchCV
def getModel():
input = Input((100,)) #(None,100,1)
ouput = Dense(10,activation='softmax')(input)
model = Model(inputs=inputs, outputs=output)
return model
grid_model = KerasClassifier(build_fn=getModel, verbose=1)
batch_size = [10, 20, 40, 60, 80, 100]
epochs = [10, 50, 100]
param_grid = dict(batch_size=batch_size, epochs=epochs)
grid = GridSearchCV(estimator=grid_model,
param_grid=param_grid,
n_jobs=1,
cv=3)#3折交叉验证
x_train = np.arrange(10000).reshape(100,100,1)
y_train = np.arrange(100)
grid_result = grid.fit(x_train, y_train)
display_cv_results(grid_result)
但是当我们模型有双输入的需求时这个API就会出现问题,例如下文这个模型存在两个输入
def getModel():
input1 = Input((100,1)) #(None,100,1)
input2 = Input((100,3)) #(None,100,3)
input1 = Flatten()(input1)
input2 = Flatten()(input2)
ouput1 = Dense(10,activation='softmax')(input1)
ouput2 = Dense(10,activation='softmax')(input2)
output = tf.append(output1,output2)
model = Model(inputs=[intput1,input2], outputs=output)
return model
因为GridSearch源代码写死了,只能有一个输入,所以不能直接使用GridSearch
我们可以把模型的结构稍微调整一下,在模型表面上是接受一个输入,然后在模型内部将它拆分为两个输入.然后模型对外是一个输入,其实在内部是两个输入。
def getModel():
input = Input((100,4))
input1 = input[:,:,0] #(None,100,1)
input2 = input[:,:,1:4] #(None,100,3)
input1 = Flatten()(input1)
input2 = Flatten()(input2)
ouput1 = Dense(10,activation='softmax')(input1)
ouput2 = Dense(10,activation='softmax')(input2)
output = tf.append(output1,output2)
model = Model(inputs=intput, outputs=output)
return model
grid_model = KerasClassifier(build_fn=getModel, verbose=1)
batch_size = [10, 20, 40, 60, 80, 100]
epochs = [10, 50, 100]
param_grid = dict(batch_size=batch_size, epochs=epochs)
grid = GridSearchCV(estimator=grid_model,
param_grid=param_grid,
n_jobs=1,
cv=3)#3折交叉验证
x_train_1 = np.arrange(10000).reshape(100,100,1)
x_train_2 = np.arrange(30000).reshape(100,100,3)
_train = np.arrange(100)
x_train = tf.concat([x_train_1,x_train_2],-1)
grid_result = grid.fit(x_train, y_train)
display_cv_results(grid_result)
专门针对Keras的超参数搜索库,可以完美支持多输入的模型的超参数搜索,非常好用,推荐!!!
https://github.com/cerlymarco/keras-hypetune
pip install keras-hypetune
def getModel(param):
input1 = Input((100,1)) #(None,100,1)
input2 = Input((100,3)) #(None,100,3)
input1 = Flatten()(input1)
input2 = Flatten()(input2)
ouput1 = Dense(param['units'],activation='softmax')(input1)
ouput2 = Dense(param['units'],activation='softmax')(input2)
output = tf.append(output1,output2)
model = Model(inputs=[intput1,input2], outputs=output)
return model
param_grid = {
'units': [10,20,30]
'epochs': 100,
'batch_size': 512
}
x_train_1 = np.arrange(10000).reshape(100,100,1)
x_train_2 = np.arrange(30000).reshape(100,100,3)
kgs = KerasGridSearch(get_model, param_grid, monitor='val_loss', greater_is_better=False) #这个的monitor支持keras中log的任意指标,可以是val_accurate
kgs.search([x_train_1,x_train_2], y_train, validation_data=(x_valid, y_valid))