在Keras/Tensorflow中,如何制作truepostitives的自定义指标减去假阳性(TP-FP)?
我知道有tf.keras.metrics.precision()
,tf.keras.metrics.truepositives()
, tf.keras.metrics.metrics.falsepositives() 。但是如何在自定义度量功能中实现这些内置指标的输出? 这是我的工作代码:
import tensorflow_addons as tfa
import tensorflow as tf
import autokeras as ak
def f1_loss(y_true, y_pred): # not coded by me
tp = K.sum(K.cast(y_true*y_pred, 'float'), axis=0)
tn = K.sum(K.cast((1-y_true)*(1-y_pred), 'float'), axis=0)
fp = K.sum(K.cast((1-y_true)*y_pred, 'float'), axis=0)
fn = K.sum(K.cast(y_true*(1-y_pred), 'float'), axis=0)
p = tp / (tp + fp + K.epsilon())
r = tp / (tp + fn + K.epsilon())
f1 = 2*p*r / (p+r+K.epsilon())
f1 = tf.where(tf.math.is_nan(f1), tf.zeros_like(f1), f1)
return 1- K.mean(f1)
length=100000;WIDTH = 3; HEIGHT=3;CLASSES=2
X=np.random.random((length,HEIGHT,WIDTH)).astype(np.float32)
Y_float=np.ones((length,CLASSES)).astype(np.float32)
for i in range (length):
Y_float[i]=np.array([np.mean( X[i]),np.mean( X[i])/2])
Y_binary= (Y_float>=0.5).astype(np.int32)
input_node = ak.Input()
output_node=ak.DenseBlock()(input_node)
Classification_output = ak.ClassificationHead(loss=f1_loss,metrics=[tfa.metrics.F1Score(num_classes=2),
tf.keras.metrics.TruePositives(), tf.keras.metrics.FalsePositives()],multi_label=True)(output_node)
auto_model= ak.AutoModel( inputs=[input_node], outputs=[Classification_output], max_trials=1,overwrite=True)
ak_history=auto_model.fit(x=[X],y=Y_binary,validation_split=0.2 )
尽管f1_loss
从不等于tfa.metrics.f1score.f1score
或1-tfa.metrics .f1score
。真正的问题是,我需要添加一个度量标准,该指标以后可以用于搜索最佳模型。
def diff(y_true, y_pred): # the new custom metric I would like to add
d=tf.keras.metrics.TruePositives()- tf.keras.metrics.FalsePositives()
return d
现在,如果更新指标为
metrics=[diff,tfa.metrics.F1Score(num_classes=2),tf.keras.metrics.TruePositives(), tf.keras.metrics.FalsePositives()]
我有错误:
TypeError: in user code:
/opt/conda/lib/python3.7/site-packages/keras/engine/training.py:853 train_function *
return step_function(self, iterator)
/tmp/ipykernel_33/1113116857.py:16 diff *
d=tf.keras.metrics.TruePositives()- tf.keras.metrics.FalsePositives()
TypeError: unsupported operand type(s) for -: 'TruePositives' and 'FalsePositives'
I know there is tf.keras.metrics.Precision()
,tf.keras.metrics.TruePositives()
, tf.keras.metrics.FalsePositives()
.But how to implement the output of these built in metrics in a custom metric function?
here is my working code:
import tensorflow_addons as tfa
import tensorflow as tf
import autokeras as ak
def f1_loss(y_true, y_pred): # not coded by me
tp = K.sum(K.cast(y_true*y_pred, 'float'), axis=0)
tn = K.sum(K.cast((1-y_true)*(1-y_pred), 'float'), axis=0)
fp = K.sum(K.cast((1-y_true)*y_pred, 'float'), axis=0)
fn = K.sum(K.cast(y_true*(1-y_pred), 'float'), axis=0)
p = tp / (tp + fp + K.epsilon())
r = tp / (tp + fn + K.epsilon())
f1 = 2*p*r / (p+r+K.epsilon())
f1 = tf.where(tf.math.is_nan(f1), tf.zeros_like(f1), f1)
return 1- K.mean(f1)
length=100000;WIDTH = 3; HEIGHT=3;CLASSES=2
X=np.random.random((length,HEIGHT,WIDTH)).astype(np.float32)
Y_float=np.ones((length,CLASSES)).astype(np.float32)
for i in range (length):
Y_float[i]=np.array([np.mean( X[i]),np.mean( X[i])/2])
Y_binary= (Y_float>=0.5).astype(np.int32)
input_node = ak.Input()
output_node=ak.DenseBlock()(input_node)
Classification_output = ak.ClassificationHead(loss=f1_loss,metrics=[tfa.metrics.F1Score(num_classes=2),
tf.keras.metrics.TruePositives(), tf.keras.metrics.FalsePositives()],multi_label=True)(output_node)
auto_model= ak.AutoModel( inputs=[input_node], outputs=[Classification_output], max_trials=1,overwrite=True)
ak_history=auto_model.fit(x=[X],y=Y_binary,validation_split=0.2 )
Searching for the best model and training is realy good, despite thatf1_loss
never equals tfa.metrics.F1Score
or 1-tfa.metrics.F1Score
. The real problem is that I need to add a metric that could be used later in searching for the best model.
def diff(y_true, y_pred): # the new custom metric I would like to add
d=tf.keras.metrics.TruePositives()- tf.keras.metrics.FalsePositives()
return d
Now if updating metrics to be
metrics=[diff,tfa.metrics.F1Score(num_classes=2),tf.keras.metrics.TruePositives(), tf.keras.metrics.FalsePositives()]
I got the error:
TypeError: in user code:
/opt/conda/lib/python3.7/site-packages/keras/engine/training.py:853 train_function *
return step_function(self, iterator)
/tmp/ipykernel_33/1113116857.py:16 diff *
d=tf.keras.metrics.TruePositives()- tf.keras.metrics.FalsePositives()
TypeError: unsupported operand type(s) for -: 'TruePositives' and 'FalsePositives'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
自定义度量在这里适应我的要求。
a custom metric here was adapted to my requirement.
您可以编写
函数
用于计算自定义损失tp -fp -fp
如下:输出:输出:
You can write a
function
for computing custom loss ofTP - FP
like below:Output: