栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

Keras自定义决策阈值以实现精确度和召回率

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Keras自定义决策阈值以实现精确度和召回率

创建这样的自定义指标:

感谢@Marcin进行编辑 :创建返回带有

threshold_value
as参数的所需度量的函数

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 precisiondef 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,...)])

我希望这有帮助 :)



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/441290.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号