Keras-hyperopt (kopt);使用hyperopt对Keras进行超参数调优。
项目描述
# kopt - Keras的超参数优化
[](https://travis-ci.org/avsecz/keras-hyperopt)
[](https://github.com/avsecz/keras-hyperopt/blob/master/LICENSE)
kopt是Keras的超参数优化库。它基于[hyperopt](https://github.com/hyperopt/hyperopt)。
## 入门
以下是一个对Keras IMDB示例模型的超参数优化示例。
example model.
```python
from keras.datasets import imdb
from keras.preprocessing import sequence
from keras.models import Sequential
import keras.layers as kl
from keras.optimizers import Adam
# kopt和hyoperot导入
from kopt import CompileFN, KMongoTrials, test_fn
from hyperopt import fmin, tpe, hp, STATUS_OK, Trials
# 1. 定义数据函数,返回训练数据,(验证数据,测试数据)
def data(max_features=5000, maxlen=80)
(x_train, y_train), (x_test, y_test) = imdb.load_data(num_words=max_features)
x_train = sequence.pad_sequences(x_train, maxlen=maxlen)
x_test = sequence.pad_sequences(x_test, maxlen=maxlen)
return (x_train[:100], y_train[:100], max_features), (x_test, y_test)
# 2. 定义模型函数,返回一个编译好的Keras模型
def model(train_data, lr=0.001,
embedding_dims=128, rnn_units=64,
dropout=0.2)
# 提取数据维度
max_features = train_data[2]
model = Sequential()
model.add(kl.Embedding(max_features, embedding_dims))
model.add(kl.LSTM(rnn_units, dropout=dropout, recurrent_dropout=dropout))
model.add(kl.Dense(1, activation='sigmoid'))
model.compile(loss='binary_crossentropy',
optimizer=Adam(lr=lr),
metrics=['accuracy'])
return model
# 指定优化指标
db_name="imdb"
实验名称="myexp1"
目标 = 编译函数(db_name, exp_name,
数据函数=data,
模型函数=model,
损失度量="acc",# 要优化的指标
损失度量模式="max",# 尝试最大化指标
有效分割=.2,# 使用20%的训练数据作为验证集
保存模型='best',# 保存最佳模型
保存结果=True,# 保存结果为.json文件(除MongoDB外)
保存目录="./saved_models/",# 存储模型的位置
## 定义超参数范围
## 更多信息请参阅 https://github.com/hyperopt/hyperopt/wiki/FMin
超参数 = {
"数据": {
"max_features": 100,
"maxlen": 80,
},
"模型": {
"lr": hp.loguniform("m_lr", np.log(1e-4), np.log(1e-2)),# 0.0001 - 0.01
"embedding_dims": hp.choice("m_emb", (64, 128)),
"rnn_units": 64,
"dropout": hp.uniform("m_do", 0, 0.5),
},
"fit": {
"epochs": 20
}
}
## 在一个小子集上测试模型训练,一个epoch
测试函数(objective, 超参数)
## 顺序运行超参数优化(无需数据库)
试验 = Trials()
最佳 = fmin(objective, 超参数, trials=试验, algo=tpe.suggest, max_evals=2)
## 并行运行超参数优化(将结果保存到MonogoDB)
## 遵循hyperopt指南
## https://github.com/hyperopt/hyperopt/wiki/Parallelizing-Evaluations-During-Search-via-MongoDB
## KMongoTrials扩展了hyperopt.MongoTrials,具有便利的方法
试验 = KMongoTrials(db_name, exp_name,
ip="localhost",
port=22334)
最佳 = fmin(objective, 超参数, trials=试验, algo=tpe.suggest, max_evals=2)
```
## 另请参阅
- [nbs/imdb_example.ipynb](nbs/imdb_example.ipynb)
`concise.hyopt`的文档(`kopt`是从`concise.hyopt`移植过来的)
- [教程](https://i12g-gagneurweb.in.tum.de/public/docs/concise/tutorials/hyper-parameter_optimization/)
- [API文档](https://i12g-gagneurweb.in.tum.de/public/docs/concise/hyopt/)
- [Jupyter笔记本](https://github.com/gagneurlab/concise/blob/master/nbs/hyper-parameter_optimization.ipynb)
[](https://travis-ci.org/avsecz/keras-hyperopt)
[](https://github.com/avsecz/keras-hyperopt/blob/master/LICENSE)
kopt是Keras的超参数优化库。它基于[hyperopt](https://github.com/hyperopt/hyperopt)。
## 入门
以下是一个对Keras IMDB示例模型的超参数优化示例。
example model.
```python
from keras.datasets import imdb
from keras.preprocessing import sequence
from keras.models import Sequential
import keras.layers as kl
from keras.optimizers import Adam
# kopt和hyoperot导入
from kopt import CompileFN, KMongoTrials, test_fn
from hyperopt import fmin, tpe, hp, STATUS_OK, Trials
# 1. 定义数据函数,返回训练数据,(验证数据,测试数据)
def data(max_features=5000, maxlen=80)
(x_train, y_train), (x_test, y_test) = imdb.load_data(num_words=max_features)
x_train = sequence.pad_sequences(x_train, maxlen=maxlen)
x_test = sequence.pad_sequences(x_test, maxlen=maxlen)
return (x_train[:100], y_train[:100], max_features), (x_test, y_test)
# 2. 定义模型函数,返回一个编译好的Keras模型
def model(train_data, lr=0.001,
embedding_dims=128, rnn_units=64,
dropout=0.2)
# 提取数据维度
max_features = train_data[2]
model = Sequential()
model.add(kl.Embedding(max_features, embedding_dims))
model.add(kl.LSTM(rnn_units, dropout=dropout, recurrent_dropout=dropout))
model.add(kl.Dense(1, activation='sigmoid'))
model.compile(loss='binary_crossentropy',
optimizer=Adam(lr=lr),
metrics=['accuracy'])
return model
# 指定优化指标
db_name="imdb"
实验名称="myexp1"
目标 = 编译函数(db_name, exp_name,
数据函数=data,
模型函数=model,
损失度量="acc",# 要优化的指标
损失度量模式="max",# 尝试最大化指标
有效分割=.2,# 使用20%的训练数据作为验证集
保存模型='best',# 保存最佳模型
保存结果=True,# 保存结果为.json文件(除MongoDB外)
保存目录="./saved_models/",# 存储模型的位置
## 定义超参数范围
## 更多信息请参阅 https://github.com/hyperopt/hyperopt/wiki/FMin
超参数 = {
"数据": {
"max_features": 100,
"maxlen": 80,
},
"模型": {
"lr": hp.loguniform("m_lr", np.log(1e-4), np.log(1e-2)),# 0.0001 - 0.01
"embedding_dims": hp.choice("m_emb", (64, 128)),
"rnn_units": 64,
"dropout": hp.uniform("m_do", 0, 0.5),
},
"fit": {
"epochs": 20
}
}
## 在一个小子集上测试模型训练,一个epoch
测试函数(objective, 超参数)
## 顺序运行超参数优化(无需数据库)
试验 = Trials()
最佳 = fmin(objective, 超参数, trials=试验, algo=tpe.suggest, max_evals=2)
## 并行运行超参数优化(将结果保存到MonogoDB)
## 遵循hyperopt指南
## https://github.com/hyperopt/hyperopt/wiki/Parallelizing-Evaluations-During-Search-via-MongoDB
## KMongoTrials扩展了hyperopt.MongoTrials,具有便利的方法
试验 = KMongoTrials(db_name, exp_name,
ip="localhost",
port=22334)
最佳 = fmin(objective, 超参数, trials=试验, algo=tpe.suggest, max_evals=2)
```
## 另请参阅
- [nbs/imdb_example.ipynb](nbs/imdb_example.ipynb)
`concise.hyopt`的文档(`kopt`是从`concise.hyopt`移植过来的)
- [教程](https://i12g-gagneurweb.in.tum.de/public/docs/concise/tutorials/hyper-parameter_optimization/)
- [API文档](https://i12g-gagneurweb.in.tum.de/public/docs/concise/hyopt/)
- [Jupyter笔记本](https://github.com/gagneurlab/concise/blob/master/nbs/hyper-parameter_optimization.ipynb)
项目详情
关闭
kopt-0.1.0.tar.gz的散列
算法 | 散列摘要 | |
---|---|---|
SHA256 | d90fe5f63dbf89dff514d0903c5cc933c5147e7bb7b109c2ad1d57b6505c7e85 |
|
MD5 | 18b48e691e7fe5d40fde2210a427539f |
|
BLAKE2b-256 | a7e2650da402870396bcf6660e98e55499f6ec7d05c8443f3bfb2c0e64dda172 |