我正在尝试使用 scikeras.wrappers.KerasRegressor 调整超参数,我遇到了如下所述的问题:
代码:
# define a func to create an instance of lstm_model
def create_lstm_model():
model = Sequential([
LSTM(5, input_shape = (Xtrain.shape[1], Xtrain.shape[2]), dropout = 0.1, activation = 'tanh', return_sequences = True),
LSTM(10, dropout = 0.05, activation = 'tanh'),
Dense(5, activation = 'relu'),
Dense(1)
])
model.compile(optimizer = tf.keras.optimizers.Adam(), loss = tf.keras.losses.MeanSquaredError(), metrics = [keras.metrics.MeanSquaredError()])
return model
#create the sklearn model for the network
model = KerasRegressor(build_fn = create_lstm_model, verbose = 1)
#param grid
batches = [16, 32]
epochs = [3, 4]
param_grid = dict(batch_size = batches, epochs = epochs)
grid = GridSearchCV(estimator = model,
param_grid = param_grid,
cv = 3)
grid.fit(Xtrain, ytrain, validation_data = (Xvalidation, yvalidation))
错误:
fn_or_cls = keras_metric_get(metric)
File "/home/aaa/Desktop/aaa/aaa/2024-gold-price-prediction-with-lstm-model/.venv/lib/python3.10/site-packages/keras/src/metrics/__init__.py", line 204, in get
raise ValueError(f"Could not interpret metric identifier: {identifier}")
ValueError: Could not interpret metric identifier: loss
我从更复杂的代码开始,并在故障排除期间将其简化到最低限度。我什至试图从模型函数中删除指标。
您对我的代码有什么问题有什么建议吗?
由于您使用不同的库来实现两个不同的任务,因此该错误可能是由于两个库中的 Keras 后端版本不匹配造成的;我建议使用损失的名称作为字符串。
model.compile(optimizer = tf.keras.optimizers.Adam(), loss = tf.keras.losses.MeanSquaredError(), metrics = ['mean_squared_error'])