我就废话不多说了,大家还是直接看代码吧~
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可使用的评价函数
1:binary_accuracy(对二分类问题,计算在所有预测值上的平均正确率)
binary_accuracy(y_true, y_pred)
2:categorical_accuracy(对多分类问题,计算在所有预测值上的平均正确率)
categorical_accuracy(y_true, y_pred)
3:sparse_categorical_accuracy(与categorical_accuracy相同,在对稀疏的目标值预测时有用 )
sparse_categorical_accuracy(y_true, y_pred)
4:top_k_categorical_accuracy(计算top-k正确率,当预测值的前k个值中存在目标类别即认为预测正确 )
top_k_categorical_accuracy(y_true, y_pred, k=5)
5:sparse_top_k_categorical_accuracy(与top_k_categorical_accracy作用相同,但适用于稀疏情况)
sparse_top_k_categorical_accuracy(y_true, y_pred, k=5)
以上这篇keras用auc做metrics以及早停实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。