我使用hyperopt优化了我的keras模型.现在我们如何将最佳优化的keras模型及其权重保存到磁盘.
我的代码:
from hyperopt import fmin,tpe,hp,STATUS_OK,Trials
from sklearn.metrics import roc_auc_score
import sys
X = []
y = []
X_val = []
y_val = []
space = {'choice': hp.choice('num_layers',[ {'layers':'two',},{'layers':'three','units3': hp.uniform('units3',64,1024),'dropout3': hp.uniform('dropout3',.25,.75)}
]),'units1': hp.choice('units1',[64,1024]),'units2': hp.choice('units2','dropout1': hp.uniform('dropout1',.75),'dropout2': hp.uniform('dropout2','batch_size' : hp.uniform('batch_size',20,100),'nb_epochs' : 100,'optimizer': hp.choice('optimizer',['adadelta','adam','rmsprop']),'activation': 'relu'
}
def f_nn(params):
from keras.models import Sequential
from keras.layers.core import Dense,Dropout,Activation
from keras.optimizers import Adadelta,Adam,rmsprop
print ('Params testing: ',params)
model = Sequential()
model.add(Dense(output_dim=params['units1'],input_dim = X.shape[1]))
model.add(Activation(params['activation']))
model.add(Dropout(params['dropout1']))
model.add(Dense(output_dim=params['units2'],init = "glorot_uniform"))
model.add(Activation(params['activation']))
model.add(Dropout(params['dropout2']))
if params['choice']['layers']== 'three':
model.add(Dense(output_dim=params['choice']['units3'],init = "glorot_uniform"))
model.add(Activation(params['activation']))
model.add(Dropout(params['choice']['dropout3']))
model.add(Dense(1))
model.add(Activation('sigmoid'))
model.compile(loss='binary_crossentropy',optimizer=params['optimizer'])
model.fit(X,y,nb_epoch=params['nb_epochs'],batch_size=params['batch_size'],verbose = 0)
pred_auc =model.predict_proba(X_val,batch_size = 128,verbose = 0)
acc = roc_auc_score(y_val,pred_auc)
print('AUC:',acc)
sys.stdout.flush()
return {'loss': -acc,'status': STATUS_OK}
trials = Trials()
best = fmin(f_nn,space,algo=tpe.suggest,max_evals=100,trials=trials)
print 'best: '
print best
最佳答案
我不知道如何将一些变量发送到f_nn或另一个hyperopt目标explicilty.但我使用两种方法来完成同样的任务.
第一种方法是一些全局变量(不喜欢它,因为它不清楚),第二种方法是将度量值保存到文件中,然后读取并与当前度量进行比较.最后一种方法在我看来更好.
第一种方法是一些全局变量(不喜欢它,因为它不清楚),第二种方法是将度量值保存到文件中,然后读取并与当前度量进行比较.最后一种方法在我看来更好.
def f_nn(params):
...
# I omit a part of the code
pred_auc =model.predict_proba(X_val,pred_auc)
try:
with open("metric.txt") as f:
min_acc = float(f.read().strip()) # read best metric,except FileNotFoundError:
min_acc = acc # else just use current value as the best
if acc < min_acc:
model.save("model.hd5") # save best to disc and overwrite metric
with open("metric.txt","w") as f:
f.write(str(acc))
print('AUC:','status': STATUS_OK}
trials = Trials()
best = fmin(f_nn,trials=trials)
print 'best: '
print best
from keras.models import load_model
best_model = load_model("model.hd5")
这种方法有几个优点:您可以将度量和模型保持在一起,甚至可以应用于某些版本或数据版本控制系统 – 这样您就可以在将来恢复实验结果.
编辑
如果存在来自先前运行的某个度量标准,则可能会导致意外行为,但您不会将其删除.因此,您可以采用代码 – 在优化后删除指标或使用时间戳等来区分您的实验数据.