我正在尝试使用以下代码使用超带优化来调整 LSTM 的批量大小,但它不起作用,因为训练项目的数量在不同的批量大小条件下没有变化。你知道如何改进代码吗?先感谢您。
import keras_tuner as kt
import tensorflow as tf
from tensorflow import keras
import numpy as np
x_train = np.random.rand(63, 92)
y_train = np.random.randint(0,6, (63))
x_val = np.random.rand(63, 92)
y_val = np.random.randint(0,7, (63))
train_set = tf.keras.preprocessing.timeseries_dataset_from_array(
x_train, y_train, sequence_length=10)
val_set = tf.keras.preprocessing.timeseries_dataset_from_array(
x_val, y_val, sequence_length=10)
train_set = tf.keras.preprocessing.timeseries_dataset_from_array(
x_train, y_train, sequence_length=10, batch_size =1)
val_set = tf.keras.preprocessing.timeseries_dataset_from_array(
x_val, y_val, sequence_length=10, batch_size =1)
def model_builder(hp):
lr = hp.Choice('learning_rate', values=[1e-2, 1e-3, 1e-4])
hp_units = hp.Int('units', min_value=32, max_value=256, step=32)
hp_units1 = hp.Int('units1', min_value=32, max_value=256, step=32)
lstm_model = tf.keras.models.Sequential([
tf.keras.layers.Bidirectional(tf.keras.layers.LSTM(hp_units, return_sequences=True)),
tf.keras.layers.Bidirectional(tf.keras.layers.LSTM(hp_units1)),
tf.keras.layers.Dense(units=1)
])
lstm_model.compile(loss='mse',
optimizer=tf.optimizers.Adam(learning_rate=lr),
metrics=['mse'])
return lstm_model
class MyTuner(kt.tuners.Hyperband):
def run_trial(self, trial, *args, **kwargs):
kwargs['batch_size'] = trial.hyperparameters.Int('batch_size', 2, 6, step=2)
return super(MyTuner, self).run_trial(trial, *args, **kwargs)
tuner = MyTuner(model_builder,
objective='val_loss',
max_epochs=4,
factor=3,
directory='KT',
project_name='intro_to_kt0207',
overwrite=True)
early_stop = tf.keras.callbacks.EarlyStopping(
monitor='val_loss', min_delta=0, patience=20, verbose=0,
mode='min', baseline=None, restore_best_weights=True
)
tuner.search(train_set, epochs=1000,
validation_data = val_set)
这是输出:
搜索:运行试验#2
超参数 |值 |迄今为止的最佳值 learning_rate |0.0001 |0.01
单位 |192 |224 单位
1 |192 |64 批次大小
|4 |2
调谐器/时期 |2 |2
调谐器/initial_e...|0 |0
调谐器/支架 |1 |1 个
调谐器/轮 |0 |0
纪元 1/2 54/54 [===============================] - 7s 37ms/step - loss: 4.4262 - mse : 4.4262 - val_loss: 3.4221 - val_mse: 3.4221 Epoch 2/2 54/54 [=============================] - 1s 12ms/step - loss: 3.1213 - mse: 3.1213 - val_loss: 3.4463 - val_mse: 3.4463 Trial 2 Complete [00h 00m 08s] val_loss: 3.42207670211792
迄今为止的最佳 val_loss:3.1889588832855225 总经过时间:00h 00m 18s
搜索:运行试验#3
超参数 |值 |迄今为止的最佳值 learning_rate |0.01 |0.01
单位 |192 |224 单位
1 |96 |64 批次大小 |
2 |2
调谐器/时期 |2 |2
调谐器/初始_e...|0 |0
调谐器/支架 |1 |1 个
调谐器/轮 |0 |0
纪元 1/2 54/54 [===============================] - 7s 34ms/step - loss: 3.6241 - mse : 3.6241 - val_loss: 3.1699 - val_mse: 3.1699 Epoch 2/2 54/54 [=============================] - 1s 12ms/step - loss: 3.1807 - mse: 3.1807 - val_loss: 3.2480 - val_mse: 3.2480 Trial 3 Complete [00h 00m 08s] val_loss: 3.1699421405792236
迄今为止的最佳 val_loss:3.1699421405792236 总经过时间:00h 00m 26s