keras中没有对AUC的定义,需要我们自己定义:
x
= np
.linspace
(-6, 6, 200)
y
= np
.array
([0.0]*100 + [1.0]*100)
state
= np
.random
.get_state
()
np
.random
.shuffle
(x
)
np
.random
.set_state
(state
)
np
.random
.shuffle
(y
)
x_train
, y_train
= x
[0:160], y
[0:160]
plt
.scatter
(x_train
, y_train
)
plt
.show
()
from keras
.models
import Sequential
from keras
.layers
import Dense
import tensorflow
as tf
from sklearn
.metrics
import roc_auc_score
def auroc(y_true
, y_pred
):
return tf
.compat
.v1
.py_func
(roc_auc_score
, (y_true
, y_pred
), tf
.double
)
model
= Sequential
()
model
.add
(Dense
(units
=1, input_dim
=1))
from keras
.optimizers
import SGD
model
.compile(loss
=my_loss
, optimizer
=SGD
(lr
=0.01, momentum
=0.9, nesterov
=True),metrics
=['accuracy', auroc
])
model
.fit
(x_train
, y_train
, epochs
=100, batch_size
=64)
‘py_func’的问题,因为tf1升到tf2,需要把tf.py_func改成tf.compat.v1.pyfunc即可。
参考文档: https://cloud.tencent.com/developer/ask/154717 https://blog.csdn.net/huazaikai/article/details/106162826