Как использовать функции метрики оценки scikit-learn с Keras в Python? - PullRequest
0 голосов
/ 06 января 2019

Keras предлагает возможность определения пользовательских метрик оценки - меня интересуют варианты метрики F, например, F1, F2 и т. Д., Предоставляемые Scikit learn - но инструктируют нас делать это, вызывая бэкэнд-функции Keras, которые ограничены в этом отношении.

Моя цель - использовать эти метрики в сочетании с методом ранней остановки Кераса. Поэтому я должен найти метод для интеграции метрики с процессом обучения модели Кераса. (Конечно, вне процесса обучения / подгонки я могу просто вызвать Scikit-Learn с результатами).

Какие у меня есть варианты?

Обновление

Реализовав решение Аарона с набором данных titanic_all_numeric от Kaggle, я получаю следующее:

# Compile the model
model.compile(optimizer = 'adam', loss = 'categorical_crossentropy', metrics = ['accuracy', f1])

# Fit the model
hist = model.fit(predictors, target, validation_split = 0.3)

Train on 623 samples, validate on 268 samples
Epoch 1/1
623/623 [==============================] - 0s 642us/step - loss: 0.8037 - acc: 0.6132 - f1: 0.6132 - val_loss: 0.5815 - val_acc: 0.7537 - val_f1: 0.7537

# Compile the model
model.compile(optimizer = 'adam', loss = 'categorical_crossentropy', metrics = ['accuracy'])

# Fit the model
hist = model.fit(predictors, target, validation_split = 0.3)

Train on 623 samples, validate on 268 samples
Epoch 1/1
623/623 [==============================] - 0s 658us/step - loss: 0.8148 - acc: 0.6404 - val_loss: 0.7056 - val_acc: 0.7313

# Compile the model
model.compile(optimizer = 'adam', loss = 'categorical_crossentropy', metrics = [f1])

# Fit the model
hist = model.fit(predictors, target, validation_split = 0.3)

Train on 623 samples, validate on 268 samples
Epoch 1/1
623/623 [==============================] - 0s 690us/step - loss: 0.6554 - f1: 0.6709 - val_loss: 0.5107 - val_f1: 0.7612

Мне интересно, хороши ли эти результаты. На этот раз точность и оценка f1 совпадают.

1 Ответ

0 голосов
/ 07 января 2019

Вы можете просто передать свои прогнозы и метки из своей модели keras в любую функцию scikit-learn для оценки. Например, если вы решаете проблему классификации, вы можете использовать classification_report из scikit-learn, который предоставляет такие метрики, как точность, отзыв, f1-оценка, например. (пример кода взят из документов):

from sklearn.metrics import classification_report
y_true = [0, 1, 2, 2, 2]
y_pred = [0, 0, 2, 2, 1]
target_names = ['class 0', 'class 1', 'class 2']
print(classification_report(y_true, y_pred, target_names=target_names))

          precision    recall  f1-score   support

 class 0       0.50      1.00      0.67         1
 class 1       0.00      0.00      0.00         1
 class 2       1.00      0.67      0.80         3

 micro avg     0.60      0.60      0.60         5
 macro avg     0.50      0.56      0.49         5
 weighted avg  0.70      0.60      0.61         5

Обновление: Если вы хотите включить метрики в обучение керасу, используйте:

from keras import backend as K

def f1(y_true, y_pred):
    def recall(y_true, y_pred):
        """Recall metric.

        Only computes a batch-wise average of recall.

        Computes the recall, a metric for multi-label classification of
        how many relevant items are selected.
        """
        true_positives = K.sum(K.round(K.clip(y_true * y_pred, 0, 1)))
        possible_positives = K.sum(K.round(K.clip(y_true, 0, 1)))
        recall = true_positives / (possible_positives + K.epsilon())
        return recall

    def precision(y_true, y_pred):
        """Precision metric.

        Only computes a batch-wise average of precision.

        Computes the precision, a metric for multi-label classification of
        how many selected items are relevant.
        """
        true_positives = K.sum(K.round(K.clip(y_true * y_pred, 0, 1)))
        predicted_positives = K.sum(K.round(K.clip(y_pred, 0, 1)))
        precision = true_positives / (predicted_positives + K.epsilon())
        return precision
    precision = precision(y_true, y_pred)
    recall = recall(y_true, y_pred)
    return 2*((precision*recall)/(precision+recall+K.epsilon()))


model.compile(loss='binary_crossentropy',
          optimizer= "adam",
          metrics=[f1])
...