我正在使用hyperopt软件包对我的深度神经网络进行超参数搜索。下面是我的代码。我的数据集太大了。我正在使用虹膜数据集来显示问题。
from sklearn import datasets
from sklearn.preprocessing import OneHotEncoder
iris = datasets.load_iris()
X = iris.data
y = iris.target.reshape(-1,1)
enc = OneHotEncoder(sparse=False)
y = enc.fit_transform(y)
X_train, X_val, y_train, y_val = train_test_split(X, y, test_size=0.33, random_state=1)
# search space
space = {'choice': hp.choice('num_layers',
[
{'layers':'two',},
{'layers':'three',
'units3': hp.choice('units3', [64, 128, 256, 512]),
'dropout3': hp.choice('dropout3', [0.25,0.5,0.75]) }
]),
'units1': hp.choice('units1', [64, 128, 256, 512]),
'units2': hp.choice('units2', [64, 128, 256, 512]),
'dropout1': hp.choice('dropout1', [0.25,0.5,0.75]),
'dropout2': hp.choice('dropout2', [0.25,0.5,0.75]),
'batch_size' : hp.choice('batch_size', [28,64,128]),
'epochs' : 50,
'optimizer': 'adadelta',
'activation': 'relu' }
#Objective function that hyperopt will minimize
def objective(params):
from keras.models import Sequential
from keras.layers.core import Dense, Dropout, Activation
from keras.optimizers import Adadelta
from keras.layers.normalization import BatchNormalization
from keras.callbacks import Callback,EarlyStopping, ModelCheckpoint
print ('Params testing: ', params)
print ('\n ')
model = Sequential()
model.add(Dense(units=params['units1'], input_dim = X_train.shape[1], kernel_initializer = "glorot_uniform"))
model.add(Activation(params['activation']))
model.add(Dropout(params['dropout1']))
model.add(BatchNormalization())
model.add(Dense(units=params['units2'], kernel_initializer = "glorot_uniform"))
model.add(Activation(params['activation']))
model.add(Dropout(params['dropout2']))
model.add(BatchNormalization())
if params['choice']['layers']== 'three':
model.add(Dense(units=params['choice']['units3'], kernel_initializer = "glorot_uniform"))
model.add(Activation(params['activation']))
model.add(Dropout(params['choice']['dropout3']))
model.add(BatchNormalization())
patience=8
else:
patience=4
model.add(Dense(3, kernel_initializer= "glorot_uniform", activation='sigmoid'))
model.compile(loss='binary_crossentropy', optimizer=params['optimizer'], metrics=['accuracy'])
# To use val_loss as early stopping
call_back = [EarlyStopping(monitor='val_loss', patience=patience ),
ModelCheckpoint(filepath='best_model.h5', monitor='val_loss', save_best_only=True)]
#includes the call back object
model.fit(X_train, y_train, validation_data=(X_val, y_val),
nb_epoch=params['epochs'], batch_size=params['batch_size'], verbose = 2,
callbacks=call_back) #******
#predict the validation set
pred_val = model.predict_classes(X_val)
y_val_original = one_hot_to_indices(y_val)
score = f1_score(y_val_original, pred_val, average='weighted')
eval_score = 1-score
print("f1 score {:.3f} params {}".format(score, params))
return (eval_score)
trials = Trials()
best_hParams = fmin(fn=objective, space=space, algo=tpe.suggest, trials=trials, max_evals=2)
print (best_hParams)
来自fmin
结果的最佳超参数如下所示。
{'units2': 1, 'dropout1': 1, 'batch_size': 2, 'num_layers': 0, 'units1': 2, 'dropout2': 1}
值似乎是我搜索空间的索引。
有人知道我如何将那些索引转换为真实参数吗?我觉得我在这个包裹里缺少什么。应该有一些简单的方法可以做到这一点。 提前非常感谢!