2017-03-05 18 views
6

मैं Keras (Tensorflow बैकएंड के साथ) का उपयोग करके बाइनरी वर्गीकरण कर रहा हूं और मुझे लगभग 76% सटीकता और 70% याद आया है। अब मैं निर्णय सीमा के साथ खेलने की कोशिश करना चाहता हूं। जहां तक ​​मुझे पता है कि Keras निर्णय दहलीज 0.5 का उपयोग करता है। निर्णय परिशुद्धता और याद करने के लिए कस्टम थ्रेसहोल्ड का उपयोग करने के लिए Keras में कोई तरीका है?परिशुद्धता के लिए केरा कस्टम निर्णय सीमा और याद रखें

आपके समय के लिए धन्यवाद!

उत्तर

8

इस तरह कस्टम मीट्रिक बनाने के लिए: @Marcin को

संपादित धन्यवाद: कि तर्क

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 
अब

आप उन्हें

model.compile(..., metrics = [precision_threshold(0.1), precision_threshold(0.2),precision_threshold(0.8), recall_threshold(0.2,...)]) 
में उपयोग कर सकते हैं के रूप में threshold_value साथ वांछित मैट्रिक्स रिटर्न कार्यों बनाएं

मुझे उम्मीद है कि यह मदद करता है :)

+0

@NassimBen अच्छा समाधान। मैं कुछ समान करना चाहता हूं लेकिन 'y_pred' में' kth' सबसे बड़े मान के आधार पर 'threshold_value' को द्विपक्षीय रूप से caclulate: मैंने यहां सवाल पूछा है: https://stackoverflow.com/questions/45720458/keras- कस्टम-रिकॉल-मेट्रिक-आधारित-ऑन-अनुमानित-मान – notconfusing

+0

यदि मैं इसे अलग थ्रेसहोल्ड मान देता हूं और मॉडल को सहेजता हूं तो मॉडल को कौन सा सटीक या याद करने वाला मॉडल मॉडल सहेजा जाएगा? – Mohsin

संबंधित मुद्दे