gpt4 book ai didi

python - y_true 和 y_pred 具有不同数量的输出 (10!=1)

转载 作者:行者123 更新时间:2023-12-01 07:20:09 26 4
gpt4 key购买 nike

我正在使用 Scikit 学习包装器 KerasClassifier 通过 RandomizedSearchCV 对我的 LSTM 模型进行超参数调整。以下是我正在做的事情的总结:1. xtrain的形状为[355,5,10],ytrain的形状为[355,10],有355个训练样本和10个特征和标签。2.首先,我使用 build_lstm_model 函数创建模型3.定义KerasClassifier4.指定用于拟合以确定评分的参数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中的指标?

最佳答案

我正在使用 KerasClassifier。我不知道 SKlearn 中还有另一个包装器 KerasRegressor。当我使用 KerasRegressor 时,我可以使用回归相关的指标来找到一个好的模型。谢谢。

关于python - y_true 和 y_pred 具有不同数量的输出 (10!=1),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57739947/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com