机器学习模型超参数优化最常用的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)}')
相关推荐
点云SLAM2 分钟前
Eigen 中矩阵的拼接(Concatenation)与 分块(Block Access)操作使用详解和示例演示
人工智能·线性代数·算法·矩阵·eigen数学工具库·矩阵分块操作·矩阵拼接操作
木枷1 小时前
NAS-Bench-101: Towards Reproducible Neural Architecture Search
人工智能·物联网
算法_小学生1 小时前
支持向量机(SVM)完整解析:原理 + 推导 + 核方法 + 实战
算法·机器学习·支持向量机
BAOYUCompany2 小时前
暴雨服务器更懂人工智能+
运维·服务器·人工智能
飞哥数智坊2 小时前
Coze实战第17讲:工资条自动拆分+一对一邮件发送
人工智能·coze
cwn_2 小时前
自然语言处理NLP (1)
人工智能·深度学习·机器学习·自然语言处理
点云SLAM2 小时前
PyTorch中flatten()函数详解以及与view()和 reshape()的对比和实战代码示例
人工智能·pytorch·python·计算机视觉·3d深度学习·张量flatten操作·张量数据结构
智海观潮2 小时前
Unity Catalog与Apache Iceberg如何重塑Data+AI时代的企业数据架构
大数据·人工智能·ai·iceberg·catalog
爱分享的飘哥2 小时前
第三篇:VAE架构详解与PyTorch实现:从零构建AI的“视觉压缩引擎”
人工智能·pytorch·python·aigc·教程·生成模型·代码实战
算法_小学生2 小时前
逻辑回归(Logistic Regression)详解:从原理到实战一站式掌握
算法·机器学习·逻辑回归