贝叶斯优化用于LSTM

我试图使用贝叶斯优化来优化LSTM的超参数。但是当我运行代码时,收到了错误消息 TypeError: only integer scalar arrays can be converted to a scalar index。我找到的一个解决方案是将训练数据和验证数据转换为数组,但在我的代码中它们已经是数组而不是列表。或者将它们转换为元组,但我不知道如何操作

X_train 形状: (946, 60, 1)

y_train 形状: (946,)

X_val 形状: (192, 60, 1)

y_val 形状: (192,)

def build(hp):    activation = hp.Choice('activation',                         [                          'relu',                          'tanh',                          'linear',                          'selu',                          'elu'                        ])    num_rnn_layers = hp.Int(                        'num_rnn_layers',                         min_value=0,                        max_value=12,                        default=3)    recurrent_dropout = hp.Float(                        'recurrent_dropout',                         min_value=0.0,                        max_value=0.99,                        default=0.2)    num_units = hp.Int(                        'num_units',                         min_value=0,                        max_value=64,                        default=32)        model = Sequential()    model.add(LSTM(units=num_units, activation=activation, recurrent_dropout = recurrent_dropout,input_shape=(X_train[1], 1)))    model.add(Dense(1))    model.compile(loss='mse', metrics=['mse'], optimizer=keras.optimizers.Adam(      hp.Choice('learning_rate', values=[1e-2, 1e-3, 1e-4])))    model.compile(      optimizer=keras.optimizers.Adam(      hp.Float(        'learning_rate',        min_value=1e-10,        max_value=1e-2,        sampling='LOG',        default=1e-6            ),        ),        loss=tf.losses.MeanSquaredError(),        metrics=[tf.metrics.MeanAbsoluteError()]    )    return modelbayesian_opt_tuner = BayesianOptimization(    build,    objective='mse',    max_trials=3,    executions_per_trial=1,    directory=os.path.normpath('C:/keras_tuning'),    project_name='kerastuner_bayesian_poc',    overwrite=True)n_epochs=100bayesian_opt_tuner.search(X_train, y_train,epochs=n_epochs,     validation_data=(X_val, y_val),     validation_split=0.2,verbose=1)bayes_opt_model_best_model = bayesian_opt_tuner.get_best_models(num_models=1)model = bayes_opt_model_best_model[0]

错误日志:

Traceback (most recent call last):  File "/usr/local/lib/python3.7/dist-packages/kerastuner/engine/hypermodel.py", line 105, in build    model = self.hypermodel.build(hp)  File "<ipython-input-80-00452994e0d6>", line 33, in build    model.add(LSTM(units=num_units, activation=activation, recurrent_dropout = recurrent_dropout,input_shape=(X_train[1], 1)))  File "/usr/local/lib/python3.7/dist-packages/tensorflow/python/training/tracking/base.py", line 517, in _method_wrapper    result = method(self, *args, **kwargs)  File "/usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/engine/sequential.py", line 204, in add    batch_shape=batch_shape, dtype=dtype, name=layer.name + '_input')  File "/usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/engine/input_layer.py", line 309, in Input    input_layer = InputLayer(**input_layer_config)  File "/usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/engine/input_layer.py", line 160, in __init__    ragged=ragged)  File "/usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/backend.py", line 1247, in placeholder    shape=shape, dtype=dtype, name=name)  File "/usr/local/lib/python3.7/dist-packages/tensorflow/python/framework/tensor_spec.py", line 51, in __init__    self._shape = tensor_shape.TensorShape(shape)  File "/usr/local/lib/python3.7/dist-packages/tensorflow/python/framework/tensor_shape.py", line 758, in __init__    self._dims = [Dimension(d) for d in dims]  File "/usr/local/lib/python3.7/dist-packages/tensorflow/python/framework/tensor_shape.py", line 758, in <listcomp>    self._dims = [Dimension(d) for d in dims]  File "/usr/local/lib/python3.7/dist-packages/tensorflow/python/framework/tensor_shape.py", line 201, in __init__    self._value = int(value.__index__())TypeError: only integer scalar arrays can be converted to a scalar index[Warning] Invalid model 0/5Traceback (most recent call last):  File "/usr/local/lib/python3.7/dist-packages/kerastuner/engine/hypermodel.py", line 105, in build    model = self.hypermodel.build(hp)  File "<ipython-input-80-00452994e0d6>", line 33, in build    model.add(LSTM(units=num_units, activation=activation, recurrent_dropout = recurrent_dropout,input_shape=(X_train[1], 1)))  File "/usr/local/lib/python3.7/dist-packages/tensorflow/python/training/tracking/base.py", line 517, in _method_wrapper    result = method(self, *args, **kwargs)  File "/usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/engine/sequential.py", line 204, in add    batch_shape=batch_shape, dtype=dtype, name=layer.name + '_input')  File "/usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/engine/input_layer.py", line 309, in Input    input_layer = InputLayer(**input_layer_config)  File "/usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/engine/input_layer.py", line 160, in __init__    ragged=ragged)  File "/usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/backend.py", line 1247, in placeholder    shape=shape, dtype=dtype, name=name)  File "/usr/local/lib/python3.7/dist-packages/tensorflow/python/framework/tensor_spec.py", line 51, in __init__    self._shape = tensor_shape.TensorShape(shape)  File "/usr/local/lib/python3.7/dist-packages/tensorflow/python/framework/tensor_shape.py", line 758, in __init__    self._dims = [Dimension(d) for d in dims]  File "/usr/local/lib/python3.7/dist-packages/tensorflow/python/framework/tensor_shape.py", line 758, in <listcomp>    self._dims = [Dimension(d) for d in dims]  File "/usr/local/lib/python3.7/dist-packages/tensorflow/python/framework/tensor_shape.py", line 201, in __init__    self._value = int(value.__index__())TypeError: only integer scalar arrays can be converted to a scalar index[Warning] Invalid model 1/5Traceback (most recent call last):  File "/usr/local/lib/python3.7/dist-packages/kerastuner/engine/hypermodel.py", line 105, in build    model = self.hypermodel.build(hp)  File "<ipython-input-80-00452994e0d6>", line 33, in build    model.add(LSTM(units=num_units, activation=activation, recurrent_dropout = recurrent_dropout,input_shape=(X_train[1], 1)))  File "/usr/local/lib/python3.7/dist-packages/tensorflow/python/training/tracking/base.py", line 517, in _method_wrapper    result = method(self, *args, **kwargs)  File "/usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/engine/sequential.py", line 204, in add    batch_shape=batch_shape, dtype=dtype, name=layer.name + '_input')  File "/usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/engine/input_layer.py", line 309, in Input    input_layer = InputLayer(**input_layer_config)  File "/usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/engine/input_layer.py", line 160, in __init__    ragged=ragged)  File "/usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/backend.py", line 1247, in placeholder    shape=shape, dtype=dtype, name=name)  File "/usr/local/lib/python3.7/dist-packages/tensorflow/python/framework/tensor_spec.py", line 51, in __init__    self._shape = tensor_shape.TensorShape(shape)  File "/usr/local/lib/python3.7/dist-packages/tensorflow/python/framework/tensor_shape.py", line 758, in __init__    self._dims = [Dimension(d) for d in dims]  File "/usr/local/lib/python3.7/dist-packages/tensorflow/python/framework/tensor_shape.py", line 758, in <listcomp>    self._dims = [Dimension(d) for d in dims]  File "/usr/local/lib/python3.7/dist-packages/tensorflow/python/framework/tensor_shape.py", line 201, in __init__    self._value = int(value.__index__())TypeError: only integer scalar arrays can be converted to a scalar index[Warning] Invalid model 2/5Traceback (most recent call last):  File "/usr/local/lib/python3.7/dist-packages/kerastuner/engine/hypermodel.py", line 105, in build    model = self.hypermodel.build(hp)  File "<ipython-input-80-00452994e0d6>", line 33, in build    model.add(LSTM(units=num_units, activation=activation, recurrent_dropout = recurrent_dropout,input_shape=(X_train[1], 1)))  File "/usr/local/lib/python3.7/dist-packages/tensorflow/python/training/tracking/base.py", line 517, in _method_wrapper    result = method(self, *args, **kwargs)  File "/usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/engine/sequential.py", line 204, in add    batch_shape=batch_shape, dtype=dtype, name=layer.name + '_input')  File "/usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/engine/input_layer.py", line 309, in Input    input_layer = InputLayer(**input_layer_config)  File "/usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/engine/input_layer.py", line 160, in __init__    ragged=ragged)  File "/usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/backend.py", line 1247, in placeholder    shape=shape, dtype=dtype, name=name)  File "/usr/local/lib/python3.7/dist-packages/tensorflow/python/framework/tensor_spec.py", line 51, in __init__    self._shape = tensor_shape.TensorShape(shape)  File "/usr/local/lib/python3.7/dist-packages/tensorflow/python/framework/tensor_shape.py", line 758, in __init__    self._dims = [Dimension(d) for d in dims]  File "/usr/local/lib/python3.7/dist-packages/tensorflow/python/framework/tensor_shape.py", line 758, in <listcomp>    self._dims = [Dimension(d) for d in dims]  File "/usr/local/lib/python3.7/dist-packages/tensorflow/python/framework/tensor_shape.py", line 201, in __init__    self._value = int(value.__index__())TypeError: only integer scalar arrays can be converted to a scalar index[Warning] Invalid model 3/5Traceback (most recent call last):  File "/usr/local/lib/python3.7/dist-packages/kerastuner/engine/hypermodel.py", line 105, in build    model = self.hypermodel.build(hp)  File "<ipython-input-80-00452994e0d6>", line 33, in build    model.add(LSTM(units=num_units, activation=activation, recurrent_dropout = recurrent_dropout,input_shape=(X_train[1], 1)))  File "/usr/local/lib/python3.7/dist-packages/tensorflow/python/training/tracking/base.py", line 517, in _method_wrapper    result = method(self, *args, **kwargs)  File "/usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/engine/sequential.py", line 204, in add    batch_shape=batch_shape, dtype=dtype, name=layer.name + '_input')  File "/usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/engine/input_layer.py", line 309, in Input    input_layer = InputLayer(**input_layer_config)  File "/usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/engine/input_layer.py", line 160, in __init__    ragged=ragged)  File "/usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/backend.py", line 1247, in placeholder    shape=shape, dtype=dtype, name=name)  File "/usr/local/lib/python3.7/dist-packages/tensorflow/python/framework/tensor_spec.py", line 51, in __init__    self._shape = tensor_shape.TensorShape(shape)  File "/usr/local/lib/python3.7/dist-packages/tensorflow/python/framework/tensor_shape.py", line 758, in __init__    self._dims = [Dimension(d) for d in dims]  File "/usr/local/lib/python3.7/dist-packages/tensorflow/python/framework/tensor_shape.py", line 758, in <listcomp>    self._dims = [Dimension(d) for d in dims]  File "/usr/local/lib/python3.7/dist-packages/tensorflow/python/framework/tensor_shape.py", line 201, in __init__    self._value = int(value.__index__())TypeError: only integer scalar arrays can be converted to a scalar index[Warning] Invalid model 4/5Traceback (most recent call last):  File "/usr/local/lib/python3.7/dist-packages/kerastuner/engine/hypermodel.py", line 105, in build    model = self.hypermodel.build(hp)  File "<ipython-input-80-00452994e0d6>", line 33, in build    model.add(LSTM(units=num_units, activation=activation, recurrent_dropout = recurrent_dropout,input_shape=(X_train[1], 1)))  File "/usr/local/lib/python3.7/dist-packages/tensorflow/python/training/tracking/base.py", line 517, in _method_wrapper    result = method(self, *args, **kwargs)  File "/usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/engine/sequential.py", line 204, in add    batch_shape=batch_shape, dtype=dtype, name=layer.name + '_input')  File "/usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/engine/input_layer.py", line 309, in Input    input_layer = InputLayer(**input_layer_config)  File "/usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/engine/input_layer.py", line 160, in __init__    ragged=ragged)  File "/usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/backend.py", line 1247, in placeholder    shape=shape, dtype=dtype, name=name)  File "/usr/local/lib/python3.7/dist-packages/tensorflow/python/framework/tensor_spec.py", line 51, in __init__    self._shape = tensor_shape.TensorShape(shape)  File "/usr/local/lib/python3.7/dist-packages/tensorflow/python/framework/tensor_shape.py", line 758, in __init__    self._dims = [Dimension(d) for d in dims]  File "/usr/local/lib/python3.7/dist-packages/tensorflow/python/framework/tensor_shape.py", line 758, in <listcomp>    self._dims = [Dimension(d) for d in dims]  File "/usr/local/lib/python3.7/dist-packages/tensorflow/python/framework/tensor_shape.py", line 201, in __init__    self._value = int(value.__index__())TypeError: only integer scalar arrays can be converted to a scalar index[Warning] Invalid model 5/5---------------------------------------------------------------------------TypeError                                 Traceback (most recent call last)/usr/local/lib/python3.7/dist-packages/kerastuner/engine/hypermodel.py in build(self, hp)    104                 with maybe_distribute(self.distribution_strategy):--> 105                     model = self.hypermodel.build(hp)    106             except:17 framesTypeError: only integer scalar arrays can be converted to a scalar indexDuring handling of the above exception, another exception occurred:RuntimeError                              Traceback (most recent call last)/usr/local/lib/python3.7/dist-packages/kerastuner/engine/hypermodel.py in build(self, hp)    113                 if i == self._max_fail_streak:    114                     raise RuntimeError(--> 115                         'Too many failed attempts to build model.')    116                 continue    117 RuntimeError: Too many failed attempts to build model.

回答:

您的代码应如下所示:

def build(hp):    activation = hp.Choice('activation',                         [                          'relu',                          'tanh',                          'linear',                          'selu',                          'elu'                        ])    num_rnn_layers = hp.Int(                        'num_rnn_layers',                         min_value=0,                        max_value=12,                        default=3)    recurrent_dropout = hp.Float(                        'recurrent_dropout',                         min_value=0.0,                        max_value=0.99,                        default=0.2)    num_units = hp.Int(                        'num_units',                         min_value=0,                        max_value=64,                        default=32)        model = Sequential()    model.add(LSTM(units=num_units, activation=activation, recurrent_dropout = recurrent_dropout,input_shape=(X_train.shape[1], 1)))    model.add(Dense(1))    model.compile(loss='mse', metrics=['mse'], optimizer=keras.optimizers.Adam(      hp.Choice('learning_rate', values=[1e-2, 1e-3, 1e-4])))    model.compile(      optimizer=keras.optimizers.Adam(      hp.Float(        'learning_rate',        min_value=1e-10,        max_value=1e-2,        sampling='LOG',        default=1e-6            ),        ),        loss=tf.losses.MeanSquaredError(),        metrics=[tf.metrics.MeanAbsoluteError()]    )    return modelbayesian_opt_tuner = BayesianOptimization(    build,    objective='mse',    max_trials=3,    executions_per_trial=1,    directory=os.path.normpath('C:/keras_tuning'),    project_name='kerastuner_bayesian_poc',    overwrite=True)n_epochs=100bayesian_opt_tuner.search(X_train, y_train,epochs=n_epochs,     validation_data=(X_val, y_val),     validation_split=0.2,verbose=1)bayes_opt_model_best_model = bayesian_opt_tuner.get_best_models(num_models=1)model = bayes_opt_model_best_model[0]

我认为导致问题的这一行:

model.add(LSTM(units=num_units, activation=activation, recurrent_dropout = recurrent_dropout,input_shape=(X_train[1], 1)))

将其更改为:

model.add(LSTM(units=num_units, activation=activation, recurrent_dropout = recurrent_dropout,input_shape=(X_train.shape[1], 1)))

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注