【发布时间】:2020-01-04 11:42:18
【问题描述】:
我正在使用 Scikit 学习包装器 KerasClassifier 为我的 LSTM 模型使用 RandomizedSearchCV 调整超参数。以下是我正在做的事情的摘要: 1. xtrain的形状为[355,5,10],ytrain的形状为[355,10],有355个训练样本和10个特征和标签。 2.首先我使用build_lstm_model函数创建模型 3.定义KerasClassifier 4. 指定要用于拟合以确定评分的参数 5. 使用 RandomizedSearchCV 指定要搜索的参数 5. 拟合模型
我使用“neg_mean_squared_error”作为评分指标。运行代码时出现错误“y_true 和 y_pred 的输出数量不同 (10!=1)”
我发现,如果我不指定任何评分指标,那么它可以正常工作。但是,我想使用 neg_mean_squared_error,因为它是一个回归问题。
# keras model
def build_lstm_model(n_blocks=6, n_cells=40, lr=0.001, lookback=lookback, n=n):
model = Sequential()
for i in range(n_blocks-1):
model.add(LSTM(n_cells, input_shape=(lookback, n), return_sequences=True, activation='tanh', kernel_initializer='uniform'))
model.add(LSTM(n_cells, input_shape=(lookback, n), activation='tanh', kernel_initializer='uniform'))
model.add(Dense(n))
adam = optimizers.Adam(lr=lr, beta_1=0.9, beta_2=0.999, epsilon=None, decay=0.0, amsgrad=False)
model.compile(loss='mean_squared_error', optimizer=adam, metrics=['accuracy'])
return model
# pass in fixed parameters n_input and n_class
model_lstm = KerasClassifier(
build_fn = build_lstm_model,
lookback = lookback, n = n)
# specify other extra parameters pass to the .fit
# number of epochs is set to a large number
keras_fit_params = {
'epochs': 10,
'batch_size': 16,
'validation_data': (xvalid, yvalid),
'verbose': 0
}
# random search parameters
# specify the options and store them inside the dictionary
# batch size and training method can also be hyperparameters, but it is fixed
n_blocks_params = [3, 4, 5, 6, 7, 8]
n_cells_params = [20, 30, 40, 50, 60]
lr_params = [0.001, 0.0001]
keras_param_options = {
'n_blocks': n_blocks_params,
'n_cells': n_cells_params,
'lr': lr_params
}
# `verbose` 2 will print the class info for every cross-validation, kind of too much
rs_lstm = GridSearchCV(
model_lstm,
param_distributions = keras_param_options,
#fit_params = keras_fit_params,
scoring = 'neg_mean_squared_error',
n_iter = 3,
cv = 5,
n_jobs = -1
#verbose = 0
)
rs_lstm.fit(xtrain, ytrain)
有没有办法可以使用 mean_squared_error 作为 RandomizedSearchCV 中的指标?
【问题讨论】:
-
你的代码中“n”的值是多少?
-
n = 10 在我的代码中。
-
我认为错误是由于使用 mean_squired_error 指标时 y_true 和 y_pred 的形状不同。我创建了一个自定义指标函数,如下所示。我发现 y_true 具有 (71, ) 并且 y_pred 具有 (71,10) 形状。 71 对应于折叠 (355/5 = 71)。我无法弄清楚为什么 y_true 具有 (71,) 形状。因为 y_train 的形状为 [355,10]。任何帮助表示赞赏。谢谢。
标签: python tensorflow keras scikit-learn