2017-03-05 14 views
6

Robię binarną klasyfikację przy użyciu Keras (z Tensorflow backend) i mam około 76% precyzji i 70% odwołania. Teraz chcę spróbować grać z progiem decyzyjnym. O ile mi wiadomo, Keras wykorzystuje próg decyzyjny 0,5. Czy jest jakiś sposób, aby w Keras użyć niestandardowego progu dla precyzji decyzji i odwołania?Keras niestandardowy próg decyzyjny dla precyzji i wycofania

Dziękuję za poświęcony czas!

Odpowiedz

8

tworzyć niestandardowe dane tak:

redakcją dzięki @Marcin: Tworzenie funkcji, która zwraca żądane dane z threshold_value jako argument

def precision_threshold(threshold=0.5): 
    def precision(y_true, y_pred): 
     """Precision metric. 
     Computes the precision over the whole batch using threshold_value. 
     """ 
     threshold_value = threshold 
     # Adaptation of the "round()" used before to get the predictions. Clipping to make sure that the predicted raw values are between 0 and 1. 
     y_pred = K.cast(K.greater(K.clip(y_pred, 0, 1), threshold_value), K.floatx()) 
     # Compute the number of true positives. Rounding in prevention to make sure we have an integer. 
     true_positives = K.round(K.sum(K.clip(y_true * y_pred, 0, 1))) 
     # count the predicted positives 
     predicted_positives = K.sum(y_pred) 
     # Get the precision ratio 
     precision_ratio = true_positives/(predicted_positives + K.epsilon()) 
     return precision_ratio 
    return precision 

def recall_threshold(threshold = 0.5): 
    def recall(y_true, y_pred): 
     """Recall metric. 
     Computes the recall over the whole batch using threshold_value. 
     """ 
     threshold_value = threshold 
     # Adaptation of the "round()" used before to get the predictions. Clipping to make sure that the predicted raw values are between 0 and 1. 
     y_pred = K.cast(K.greater(K.clip(y_pred, 0, 1), threshold_value), K.floatx()) 
     # Compute the number of true positives. Rounding in prevention to make sure we have an integer. 
     true_positives = K.round(K.sum(K.clip(y_true * y_pred, 0, 1))) 
     # Compute the number of positive targets. 
     possible_positives = K.sum(K.clip(y_true, 0, 1)) 
     recall_ratio = true_positives/(possible_positives + K.epsilon()) 
     return recall_ratio 
    return recall 

teraz można z nich korzystać w

model.compile(..., metrics = [precision_threshold(0.1), precision_threshold(0.2),precision_threshold(0.8), recall_threshold(0.2,...)]) 

Mam nadzieję, że to pomoże :)

+0

@NassimBen dobre rozwiązanie. Chciałbym zrobić coś bardzo podobnego, ale dynamaically kaclulate 'próg_wartości' oparty na' 'kth' największą wartość w' y_pred': Zadałem pytanie tutaj: https://stackoverflow.com/questions/45720458/keras- niestandardowe wywoływanie-oparte na metodzie wartości przewidywanych – notconfusing

+0

jeśli podaję inne wartości progowe i zapiszę model z jaką dokładnością lub wartością przywołania model będzie zapisany w modelu? – Mohsin