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

时间:2019-08-31 16:49:48

标签: python tensorflow keras scikit-learn

我正在使用Scikit学习包装器KerasClassifier来使用RandomizedSearchCV调整我的LSTM模型的超参数。以下是我在做什么的摘要: 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中的指标?

1 个答案:

答案 0 :(得分:0)

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