首页 > 解决方案 > 如何从历史中访问指标

问题描述

对于回归问题,我想比较一些指标,但我只能accuracy从历史中得到,这对回归目的毫无意义。我怎样才能获得其他指标等mean_squared_error

create_model(...)
    input_layer = ...
    output_laye = ...
    model = Model(input_layer, output_layer)
    model.compile(loss='mean_squared_error', optimizer=optimizer, metrics=['accuracy'])
    return model

model = KerasRegressor(build_fn=create_model, verbose=0)

batch_size = [1, 2]
epochs = [1, 2]
optimizer = ['Adam', 'sgd']    
param_grid = dict(batch_size=batch_size
                     , optimizer = optimizer
                     )

grid_obj  = RandomizedSearchCV(estimator=model 
                    , param_grid=hypparas
                    , n_jobs=1
                    , cv = 3
                    , scoring = ['explained_variance', 'neg_mean_squared_error', 'r2']
                    , refit = 'neg_mean_squared_error'
                    , return_train_score=True
                    , verbose = 2
                    )

grid_result = grid_obj.fit(X_train1, y_train1)

X_train1, X_val1, y_train1, y_val1 = train_test_split(X_train1, y_train1, test_size=0.2, shuffle=False)

grid_best = grid_result.best_estimator_
history = grid_best.fit(X_train1, y_train1
                        , validation_data=(X_val1, y_val1)
                        )

print(history.history.keys())
> dict_keys(['val_loss', 'val_accuracy', 'loss', 'accuracy'])

我已经看到https://stackoverflow.com/a/50137577/6761328得到例如

history.history['accuracy']

哪个有效,但我无法访问mean_squared_error或其他:

history.history['neg_mean_squared_error']
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-473-eb96973bf014> in <module>
----> 1 history.history['neg_mean_squared_error']

KeyError: 'neg_mean_squared_error'

这个问题最后是关于如何比较不同指标的后续问题?因为我认为这个问题是另一个问题的答案。

标签: pythonkerasneural-network

解决方案


In stand-alone Keras (not sure for the scikit-learn wrapper), history.history['loss'] (or val_loss respectively for the validation set) would do the job.

Here, 'loss' and 'val_loss' are keys; give

print(history.history.keys())

to see what keys are available in your case, and you will find among them the required ones for the loss (might even be the same, i.e. 'loss' and 'val_loss').

As a side note, you should remove completely metrics=['accuracy'] from your model compilation - as you correctly point out, accuracy is meaningless in regression settings (you might want to check What function defines accuracy in Keras when the loss is mean squared error (MSE)?).


推荐阅读