优化超参数始终是确保模型性能最佳的关键任务。通常,网格搜索、随机搜索和贝叶斯优化等技术是主要使用的方法。
今天分享几个常用于模型超参数优化的 Python 工具包,如下所示:
- scikit-learn:使用在指定参数值上进行的网格搜索或随机搜索。
- HyperparameterHunter:构建在scikit-learn之上,以使其更易于使用。
- Optuna:使用随机搜索、Parzen估计器(TPE)和基于群体的训练。
- Hyperopt:使用随机搜索和TPE。
- Talos:构建在Keras之上,以使其更易于使用。
技术交流
技术要学会分享、交流,不建议闭门造车。一个人可以走的很快、一堆人可以走的更远。
本文由粉丝群小伙伴总结与分享,如果你也想学习交流,资料获取,均可加交流群获取,群友已超过2000人,添加时最好的备注方式为:来源+兴趣方向,方便找到志同道合的朋友。
方式①、添加微信号:dkl88194,备注:来自CSDN + 加群
方式②、微信搜索公众号:Python学习与数据挖掘,后台回复:加群
现在,让我们看一些使用这些库进行自动编码器模型超参数优化的Python代码示例:
python
from keras.layers import Input, Dense
from keras.models import Model
# define the Autoencoder
input_layer = Input(shape=(784,))
encoded = Dense(32, activation='relu')(input_layer)
decoded = Dense(784, activation='sigmoid')(encoded)
autoencoder = Model(input_layer, decoded)
autoencoder.compile(optimizer='adam', loss='binary_crossentropy')
autoencoder.fit(X_train, X_train, epochs=100, batch_size=256, validation_data=(X_test, X_test))
scikit-learn
python
from sklearn.model_selection import GridSearchCV
# define the parameter values that should be searched
param_grid = {'batch_size': [64, 128, 256], 'epochs': [50, 100, 150]}
# create a KFold cross-validator
kfold = KFold(n_splits=10, random_state=7)
# create the grid search object
grid = GridSearchCV(estimator=autoencoder, param_grid=param_grid, cv=kfold)
# fit the grid search object to the training data
grid_result = grid.fit(X_train, X_train)
# print the best parameters and the corresponding score
print(f'Best parameters: {grid_result.best_params_}')
print(f'Best score: {grid_result.best_score_}')
HyperparameterHunter
python
import HyperparameterHunter as hh
# create a HyperparameterHunter object
hunter = hh.HyperparameterHunter(input_data=X_train, output_data=X_train, model_wrapper=hh.ModelWrapper(autoencoder))
# define the hyperparameter search space
hunter.setup(objective='val_loss', metric='val_loss', optimization_mode='minimize', max_trials=100)
hunter.add_experiment(parameters=hh.Real(0.1, 1, name='learning_rate', digits=3, rounding=4))
hunter.add_experiment(parameters=hh.Real(0.1, 1, name='decay', digits=3, rounding=4))
# perform the hyperparameter search
hunter.hunt(n_jobs=1, gpu_id='0')
# print the best hyperparameters and the corresponding score
print(f'Best hyperparameters: {hunter.best_params}')
print(f'Best score: {hunter.best_score}')
Hyperopt
python
from hyperopt import fmin, tpe, hp
# define the parameter space
param_space = {'batch_size': hp.quniform('batch_size', 64, 256, 1), 'epochs': hp.quniform('epochs', 50, 150, 1)}
# define the objective function
def objective(params):
autoencoder.compile(optimizer='adam', loss='binary_crossentropy')
autoencoder.fit(X_train, X_train, batch_size=params['batch_size'], epochs=params['epochs'], verbose=0)
scores = autoencoder.evaluate(X_test, X_test, verbose=0)
return {'loss': scores, 'status': STATUS_OK}
# perform the optimization
best = fmin(objective, param_space, algo=tpe.suggest, max_evals=100)
# print the best parameters and the corresponding score
print(f'Best parameters: {best}')
print(f'Best score: {objective(best)}')
Optuna
python
import optuna
# define the objective function
def objective(trial):
batch_size = trial.suggest_int('batch_size', 64, 256)
epochs = trial.suggest_int('epochs', 50, 150)
autoencoder.compile(optimizer='adam', loss='binary_crossentropy')
autoencoder.fit(X_train, X_train, batch_size=batch_size, epochs=epochs, verbose=0)
score = autoencoder.evaluate(X_test, X_test, verbose=0)
return score
# create the Optuna study
study = optuna.create_study()
# optimize the hyperparameters
study.optimize(objective, n_trials=100)
# print the best parameters and the corresponding score
print(f'Best parameters: {study.best_params}')
print(f'Best score: {study.best_value}')
Talos
python
import talos
# define the parameter space
param_space = {'learning_rate': [0.1, 0.01, 0.001], 'decay': [0.1, 0.01, 0.001]}
# define the objective function
def objective(params):
autoencoder.compile(optimizer='adam', loss='binary_crossentropy', lr=params['learning_rate'], decay=params['decay'])
history = autoencoder.fit(X_train, X_train, epochs=100, batch_size=256, validation_data=(X_test, X_test), verbose=0)
score = history.history['val_loss'][-1]
return score
# perform the optimization
best = talos.Scan(X_train, X_train, params=param_space, model=autoencoder, experiment_name='autoencoder').best_params(objective, n_trials=100)
# print the best parameters and the corresponding score
print(f'Best parameters: {best}')
print(f'Best score: {objective(best)}')