python - 如何在 Kerastuner 中通过交叉验证调整模型中的时期和批量大小?
问题描述
我想使用 Kerastuner 调整我的 Keras 模型。我遇到了一些调整批量大小和时期的代码片段,以及单独的 Kfold 交叉验证。我想同时做这些。
Batch 大小和 Epoch 的代码
class MyTuner(kerastuner.tuners.BayesianOptimization):
def run_trial(self, trial, *args, **kwargs):
# You can add additional HyperParameters for preprocessing and custom training loops
# via overriding `run_trial`
kwargs['batch_size'] = trial.hyperparameters.Int('batch_size', 32, 256, step=32)
kwargs['epochs'] = trial.hyperparameters.Int('epochs', 10, 30)
super(MyTuner, self).run_trial(trial, *args, **kwargs)
# Uses same arguments as the BayesianOptimization Tuner.
tuner = MyTuner(...)
# Don't pass epochs or batch_size here, let the Tuner tune them.
tuner.search(...)
交叉验证代码
import kerastuner
import numpy as np
from sklearn import model_selection
class CVTuner(kerastuner.engine.tuner.Tuner):
def run_trial(self, trial, x, y, batch_size=32, epochs=1):
cv = model_selection.KFold(5)
val_losses = []
for train_indices, test_indices in cv.split(x):
x_train, x_test = x[train_indices], x[test_indices]
y_train, y_test = y[train_indices], y[test_indices]
model = self.hypermodel.build(trial.hyperparameters)
model.fit(x_train, y_train, batch_size=batch_size, epochs=epochs)
val_losses.append(model.evaluate(x_test, y_test))
self.oracle.update_trial(trial.trial_id, {'val_loss': np.mean(val_losses)})
self.save_model(trial.trial_id, model)
tuner = CVTuner(
hypermodel=my_build_model,
oracle=kerastuner.oracles.BayesianOptimization(
objective='val_loss',
max_trials=40))
x, y = ... # NumPy data
tuner.search(x, y, batch_size=64, epochs=30)
如何更改以run_trial
使这两种方法可以一起执行?
解决方案
推荐阅读
- r - R:在循环/w中操作输入数据帧。purrr-functions 无需创建新对象
- image - 为什么建议将图像存储在远程服务器上?
- r - 计算自变量在解释线性回归中因变量方差中的重要性
- html - 如何使用 Ionic 3 将文本“ON”和“OFF”添加到切换按钮
- python - PyQt5 QtreeWidget:如何在 QtreeWidgetItem 中访问自定义小部件的方法?
- python - 使用 Python mysql 连接器获取 csv
- android - 当我在云存储中有超过 1 个文档时,geopoint 上的 Android getLatitude() 返回 null
- javascript - FileReader - 如何将字符编码从 UTF-8 更改为 ANSI
- sql-server-2012 - 如何在T-Sql中以逗号分隔的另一列的值中查找一列的数值?
- php - 第二层exec不返回线程