深度学习模型超参数搜索(单输入与多输入)

问题描述

在我们搭建完模型之后,一般需要对模型内预设定的参数进行搜索,来确定最佳的参数组合

常见的搜索方法是网格搜索(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

二、多输入的网格搜索

1.仍然使用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)

2.使用其他的库

专门针对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))

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值