机器学习模型超参数优化最常用的5个工具包!

优化超参数始终是确保模型性能最佳的关键任务。通常,网格搜索、随机搜索和贝叶斯优化等技术是主要使用的方法。

今天分享几个常用于模型超参数优化的 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)}')
相关推荐
学好statistics和DS1 天前
【CV】神经网络中哪些参数需要被学习?
人工智能·神经网络·学习
大千AI助手1 天前
机器学习特征筛选中的IV值详解:原理、应用与实现
人工智能·机器学习·kl散度·roc·iv·信息值·woe
姜—姜1 天前
通过构建神经网络实现项目预测
人工智能·pytorch·深度学习·神经网络
Ro Jace1 天前
模式识别与机器学习课程笔记(4):线性判决函数
人工智能·笔记·机器学习
科研小白_1 天前
基于遗传算法优化BP神经网络(GA-BP)的数据时序预测
人工智能·算法·回归
互联网江湖1 天前
蓝桥杯出局,少儿编程的价值祛魅时刻?
人工智能·生活
Elastic 中国社区官方博客1 天前
根据用户行为数据中的判断列表在 Elasticsearch 中训练 LTR 模型
大数据·数据库·人工智能·elasticsearch·搜索引擎·ai·全文检索
paid槮1 天前
OpenCV图像形态学详解
人工智能·opencv·计算机视觉
点控云1 天前
点控云智能短信:重构企业与用户的连接,让品牌沟通更高效
大数据·人工智能·科技·重构·外呼系统·呼叫中心
救救孩子把1 天前
14-机器学习与大模型开发数学教程-第1章 1-6 费马定理与极值判定
人工智能·数学·机器学习