KeyError:Keras Tuner回归搜索中的{objective}

vcirk6k6  于 2023-04-21  发布在  其他
关注(0)|答案(1)|浏览(118)

我尝试使用Keras Tuner对回归ANN进行超参数调优。当我进行超参数搜索时,我收到KeyError: 'val_mean_absolute_error
下面是我的代码:

def build_model(hp):
    model = keras.Sequential()
    for i in range(hp.Int('num_layers', 2, 20)):
        model.add(layers.Dense(
            units=hp.Int('units_' + str(i), min_value=32, max_value=512, step=32),
            activation='relu'
        ))
        if hp.Boolean("dropout"):
            model.add(layers.Dropout(rate=0.5))
    model.add(layers.Dense(1, activation='linear'))
    model.compile(
        optimizer=keras.optimizers.Adam(hp.Choice('learning_rate', [1e-4, 1e-3, 1e-5])),
        loss='mean_absolute_error',
        metrics=['mean_absolute_error']
    )
    return model

tuner = kt.RandomSearch(
    build_model,
    objective='val_mean_absolute_error',
    max_trials=5,
    executions_per_trial=2,
    overwrite=True,
    directory="Local Files",
    project_name="Keras")

tuner.search(x_train, y_train, epochs=1)

在一个模型被训练之后,我得到了这个错误:

Traceback (most recent call last):
  File "c:\Users\{user}\.venv\lib\site-packages\keras_tuner\engine\base_tuner.py", line 266, in _try_run_and_update_trial
    self._run_and_update_trial(trial, *fit_args, **fit_kwargs)
  File "c:\Users\{user}\.venv\lib\site-packages\keras_tuner\engine\base_tuner.py", line 257, in _run_and_update_trial
    tuner_utils.convert_to_metrics_dict(
  File "c:\Users\{user}\.venv\lib\site-packages\keras_tuner\engine\tuner_utils.py", line 270, in convert_to_metrics_dict
    [convert_to_metrics_dict(elem, objective) for elem in results]
  File "c:\Users\{user}\.venv\lib\site-packages\keras_tuner\engine\tuner_utils.py", line 270, in <listcomp>
    [convert_to_metrics_dict(elem, objective) for elem in results]
  File "c:\Users\{user}\.venv\lib\site-packages\keras_tuner\engine\tuner_utils.py", line 283, in convert_to_metrics_dict
    best_value, _ = _get_best_value_and_best_epoch_from_history(
  File "c:\Users\{user}\.venv\lib\site-packages\keras_tuner\engine\tuner_utils.py", line 254, in _get_best_value_and_best_epoch_from_history
    objective_value = objective.get_value(metrics)
  File "c:\Users\{user}\.venv\lib\site-packages\keras_tuner\engine\objective.py", line 57, in get_value
    return logs[self.name]
KeyError: 'val_mean_absolute_error'

我在任何地方都找不到这个错误,我从多个示例中复制了代码,所有这些代码都抛出了相同的错误。这里发生了什么?

dgsult0t

dgsult0t1#

我有类似的问题。帮助:metrics=['accuracy'](而不是metrics=['binary_accuracy'])

相关问题