神剑山庄资源网 Design By www.hcban.com
我就废话不多说了,大家还是直接看代码吧!
# 利用sklearn自建评价函数 from sklearn.model_selection import train_test_split from sklearn.metrics import roc_auc_score from keras.callbacks import Callback class RocAucEvaluation(Callback): def __init__(self, validation_data=(), interval=1): super(Callback, self).__init__() self.interval = interval self.x_val,self.y_val = validation_data def on_epoch_end(self, epoch, log={}): if epoch % self.interval == 0: y_pred = self.model.predict(self.x_val, verbose=0) score = roc_auc_score(self.y_val, y_pred) print('\n ROC_AUC - epoch:%d - score:%.6f \n' % (epoch+1, score)) x_train,y_train,x_label,y_label = train_test_split(train_feature, train_label, train_size=0.95, random_state=233) RocAuc = RocAucEvaluation(validation_data=(y_train,y_label), interval=1) hist = model.fit(x_train, x_label, batch_size=batch_size, epochs=epochs, validation_data=(y_train, y_label), callbacks=[RocAuc], verbose=2)
补充知识:keras用auc做metrics以及早停
我就废话不多说了,大家还是直接看代码吧!
import tensorflow as tf from sklearn.metrics import roc_auc_score def auroc(y_true, y_pred): return tf.py_func(roc_auc_score, (y_true, y_pred), tf.double) # Build Model... model.compile(loss='categorical_crossentropy', optimizer='adam',metrics=['accuracy', auroc])
完整例子:
def auc(y_true, y_pred): auc = tf.metrics.auc(y_true, y_pred)[1] K.get_session().run(tf.local_variables_initializer()) return auc def create_model_nn(in_dim,layer_size=200): model = Sequential() model.add(Dense(layer_size,input_dim=in_dim, kernel_initializer='normal')) model.add(BatchNormalization()) model.add(Activation('relu')) model.add(Dropout(0.3)) for i in range(2): model.add(Dense(layer_size)) model.add(BatchNormalization()) model.add(Activation('relu')) model.add(Dropout(0.3)) model.add(Dense(1, activation='sigmoid')) adam = optimizers.Adam(lr=0.01) model.compile(optimizer=adam,loss='binary_crossentropy',metrics = [auc]) return model ####cv train folds = StratifiedKFold(n_splits=5, shuffle=False, random_state=15) oof = np.zeros(len(df_train)) predictions = np.zeros(len(df_test)) for fold_, (trn_idx, val_idx) in enumerate(folds.split(df_train.values, target2.values)): print("fold n°{}".format(fold_)) X_train = df_train.iloc[trn_idx][features] y_train = target2.iloc[trn_idx] X_valid = df_train.iloc[val_idx][features] y_valid = target2.iloc[val_idx] model_nn = create_model_nn(X_train.shape[1]) callback = EarlyStopping(monitor="val_auc", patience=50, verbose=0, mode='max') history = model_nn.fit(X_train, y_train, validation_data = (X_valid ,y_valid),epochs=1000,batch_size=64,verbose=0,callbacks=[callback]) print('\n Validation Max score : {}'.format(np.max(history.history['val_auc']))) predictions += model_nn.predict(df_test[features]).ravel()/folds.n_splits
以上这篇Keras 利用sklearn的ROC-AUC建立评价函数详解就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。
神剑山庄资源网 Design By www.hcban.com
神剑山庄资源网
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件!
如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
神剑山庄资源网 Design By www.hcban.com
暂无Keras 利用sklearn的ROC-AUC建立评价函数详解的评论...
更新日志
2024年11月18日
2024年11月18日
- 周华健.2015-水浒三部曲原创音乐选辑【滚石】【FLAC分轨】
- 钟志刚《为爱而歌DSD》[WAV+CUE]
- 孙露《情人的眼泪》[低速原抓WAV+CUE]
- 【雨果唱片】刘明源《胡琴专辑》1993[WAV+CUE]
- 黄莺莺《25周年纪念金曲专辑》[WAV+CUE][1.1G]
- 刘德丽《刘德丽新曲+精选》2023[WAV+CUE][1G]
- 潘美辰《鹰与月》双语专辑[WAV+CUE][1G]
- 梁咏琪.2007-女色新曲+精选2CD【华纳】【WAV+CUE】
- 黎亚.2006-我不在巴黎【星外星】【FLAC分轨】
- 陈洁仪.1994-心痛【立得唱片】【WAV+CUE】
- 车载必备专用超级选曲《劲爆中文DJ》2CD[WAV+CUE]
- 群星《民歌流淌60年(黑胶CD)》2CD[WAV+分轨]
- 群星《美丽时光》紫银合金AQCD[WAV+CUE]
- 群星《12大巨星畅销精选集》[WAV分轨][1.1G]
- 华语排行冠军曲《百事音乐风云榜》[WAV+CUE][1G]