[英]Weird behaviour in tensorflow metric
我创建了一个 tensorflow 指标,如下所示:
def AttackAcc(y_true, y_pred):
r = tf.random.uniform(shape=(), minval=0, maxval=11, dtype=tf.int32)
if tf.math.greater(r,tf.constant(5) ):
return tf.math.equal( tf.constant(0.6) , tf.constant(0.2) )
else:
return tf.math.equal( tf.constant(0.6) , tf.constant(0.6) )
该指标添加到model.compile
中,如下所示:
metrics=[AttackAcc]
这应该在一半时间返回 0,在另一半时间返回 1。 因此,在训练我的 model 时,我应该看到该指标的值约为 0.5。 然而它始终为 0。
关于为什么的任何想法?
看起来您正在比较两个常量,它们将永远不相等。 尝试BinaryAccuracy并使用您的输入变量来更新 state。
def AttackAcc(y_true, y_pred):
r = tf.random.uniform(shape=(), minval=0, maxval=11, dtype=tf.int32)
acc_metric = tf.keras.metrics.BinaryAccuracy()
acc_metric.update_state(y_true, y_pred)
if tf.math.greater(r, tf.constant(5)):
return acc_metric.result()
else:
return 1 - acc_metric.result()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.