Tensorflow 使用Keras调谐器进行时间序列分割
是否可以使用Keras调谐器使用时间序列分割来调整NN,类似于sklearn中的sklearn.model_selection.TimeSeriesSplitTensorflow 使用Keras调谐器进行时间序列分割,tensorflow,keras,deep-learning,keras-tuner,Tensorflow,Keras,Deep Learning,Keras Tuner,是否可以使用Keras调谐器使用时间序列分割来调整NN,类似于sklearn中的sklearn.model_selection.TimeSeriesSplit 例如从中考虑一个采样调谐器类 调谐器: tuner_rs = RandomSearch( hypermodel, objective='mse', seed=42, max_trials=10, executions_p
例如从
中考虑一个采样调谐器类 调谐器:tuner_rs = RandomSearch(
hypermodel,
objective='mse',
seed=42,
max_trials=10,
executions_per_trial=2)
tuner_rs.search(x_train_scaled, y_train, epochs=10, validation_split=0.2, verbose=0)
因此,在上述行中,是否可以执行以下操作,而不是验证\u split=0.2
from sklearn.model_selection import TimeSeriesSplit
#defining a time series split object
tscv = TimeSeriesSplit(n_splits = 5)
#using that in Keras Tuner
tuner_rs.search(x_train, y_train, epochs=10, validation_split=tscv, verbose=0)
我这样解决:
首先,我声明了一个类,它允许执行阻塞时间序列拆分。我发现使用这个时间序列分割可能比使用Sklearn TimeSeriesSplit更好,因为我们不会让我们的模型在已经看到数据的实例上训练。从图中可以看出,如果拆分的数量为5,BTS将把您的培训数据分成5个部分,只有验证数据在拆分中是相同的。(由于StackOverflow不允许我上传图像,我将发布一个参考链接:)
然后,您将继续创建自己的模型:
def build_model(hp):
pass
最后,您可以将CVtuner创建为一个类,该类将回调BlockingTimeSeriesSplit
class CVTuner(kt.engine.tuner.Tuner):
def run_trial(self, trial, x, y, *args, **kwargs):
cv = BlockingTimeSeriesSplit(n_splits=5)
val_accuracy_list = []
batch_size = trial.hyperparameters.Int('batch_size', 0, 64, step=8)
epochs = trial.hyperparameters.Int('epochs', 10, 100, step=10)
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_loss, val_accuracy, val_auc = model.evaluate(x_test, y_test)
val_accuracy_list.append(val_accuracy)
self.oracle.update_trial(trial.trial_id, {'val_accuracy': np.mean(val_accuracy_list)})
self.save_model(trial.trial_id, model)
tuner = CVTuner(oracle=kt.oracles.BayesianOptimization(objective='val_accuracy',max_trials=1), hypermodel=create_model)
stop_early = tf.keras.callbacks.EarlyStopping(monitor='accuracy', patience=10)
tuner.search(X, Y, callbacks=[stop_early])
best_model = tuner.get_best_models()[0]
best_model.summary()
best_model.evaluate(x_out_of_sample, y_out_of_sample)
def build_model(hp):
pass
class CVTuner(kt.engine.tuner.Tuner):
def run_trial(self, trial, x, y, *args, **kwargs):
cv = BlockingTimeSeriesSplit(n_splits=5)
val_accuracy_list = []
batch_size = trial.hyperparameters.Int('batch_size', 0, 64, step=8)
epochs = trial.hyperparameters.Int('epochs', 10, 100, step=10)
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_loss, val_accuracy, val_auc = model.evaluate(x_test, y_test)
val_accuracy_list.append(val_accuracy)
self.oracle.update_trial(trial.trial_id, {'val_accuracy': np.mean(val_accuracy_list)})
self.save_model(trial.trial_id, model)
tuner = CVTuner(oracle=kt.oracles.BayesianOptimization(objective='val_accuracy',max_trials=1), hypermodel=create_model)
stop_early = tf.keras.callbacks.EarlyStopping(monitor='accuracy', patience=10)
tuner.search(X, Y, callbacks=[stop_early])
best_model = tuner.get_best_models()[0]
best_model.summary()
best_model.evaluate(x_out_of_sample, y_out_of_sample)