码迷,mamicode.com
首页 > 其他好文 > 详细

keras 上添加 roc auc指标

时间:2018-03-04 17:10:36      阅读:1623      评论:0      收藏:0      [点我收藏+]

标签:append   ack   结束   number   loss   tps   []   detail   post   

https://stackoverflow.com/questions/41032551/how-to-compute-receiving-operating-characteristic-roc-and-auc-in-keras

https://github.com/keras-team/keras/issues/3230#issuecomment-319208366

http://www.luozhipeng.com/?p=1225

http://scikit-learn.org/stable/modules/generated/sklearn.metrics.roc_auc_score.html

http://blog.csdn.net/zdy0_2004/article/details/44948511

 

keras 输出roc指标,不能每个batch输出一次,需要全部计算完再一次计算一次。使用sklearn中的metrics roc来计算。几个帖子类似

class roc_callback(keras.callbacks.Callback):
    def __init__(self,training_data,validation_data):
        
        self.x = training_data[0]
        self.y = training_data[1]
        self.x_val = validation_data[0]
        self.y_val = validation_data[1]
        
    
    def on_train_begin(self, logs={}):
        return
 
    def on_train_end(self, logs={}):
        return
 
    def on_epoch_begin(self, epoch, logs={}):
        return
 
    def on_epoch_end(self, epoch, logs={}):        
        y_pred = self.model.predict(self.x)
        roc = roc_auc_score(self.y, y_pred)      
        
        y_pred_val = self.model.predict(self.x_val)
        roc_val = roc_auc_score(self.y_val, y_pred_val)      
        
        print(\rroc-auc: %s - roc-auc_val: %s % (str(round(roc,4)),str(round(roc_val,4))),end=100* +\n)
        return
 
    def on_batch_begin(self, batch, logs={}):
        return
 
    def on_batch_end(self, batch, logs={}):
        return   


callbacks=[roc_callback(training_data=training_data,validation_data=validation_data)] 

 

 

首先建立callbacks脚本,

my_callbacks.py如下:

import keras
from sklearn.metrics import roc_auc_score
import numpy as np

 

class Histories(keras.callbacks.Callback):
6
    def on_train_begin(self, logs={}):
7
        self.aucs = []
8
        self.losses = []
9
 
10
    def on_train_end(self, logs={}):
11
        return
12
 
13
    def on_epoch_begin(self, epoch, logs={}):
14
        return
15
 
16
    def on_epoch_end(self, epoch, logs={}):
17
        self.losses.append(logs.get(loss))
18
        y_pred = self.model.predict(self.validation_data[0:2])
19
 
20
        yp = []
21
        for i in xrange(0, len(y_pred)):
22
            yp.append(y_pred[i][0])
23
        yt = []
24
        for x in self.validation_data[2]:
25
            yt.append(x[0])
26
         
27
        auc = roc_auc_score(yt, yp)
28
        self.aucs.append(auc)
29
        print val-loss,logs.get(loss),  val-auc: ,auc,
30
        print \n
31
         
32
        return
33
 
34
    def on_batch_begin(self, batch, logs={}):
35
        return
36
 
37
    def on_batch_end(self, batch, logs={}):
38
        return

 

 

模型的输入为:

1 model = Model(inputs=[keyword1, keyword2], outputs=y)

 

在每个epoch结束时计算auc并输出:

1 histories = my_callbacks.Histories()
2  
3 model.fit(train_x, train_y, batch_size=1024, epochs=20,shuffle=True, class_weight={1:1.0, 0:0.25}, validation_split=0.2, callbacks=[histories, model_check, lr])

keras 上添加 roc auc指标

标签:append   ack   结束   number   loss   tps   []   detail   post   

原文地址:https://www.cnblogs.com/bnuvincent/p/8505101.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!