gpt4 book ai didi

python - 如何在 Keras 中使用 TensorFlow 指标

转载 作者:太空狗 更新时间:2023-10-29 18:28:34 25 4
gpt4 key购买 nike

似乎已经有几个线程/问题,但在我看来这并没有得到解决:

How can I use tensorflow metric function within keras models?

https://github.com/fchollet/keras/issues/6050

https://github.com/fchollet/keras/issues/3230

人们似乎要么遇到变量初始化问题,要么遇到指标为 0 的问题。

我需要计算不同的分割指标并希望包括 tf.metric.mean_iou在我的 Keras 模型中。这是迄今为止我能想到的最好的:

def mean_iou(y_true, y_pred):
score, up_opt = tf.metrics.mean_iou(y_true, y_pred, NUM_CLASSES)
K.get_session().run(tf.local_variables_initializer())
return score

model.compile(optimizer=adam, loss='categorical_crossentropy', metrics=[mean_iou])

此代码不会抛出任何错误,但 mean_iou 始终返回 0。我相信这是因为 up_opt 未被评估。我在 TF 1.3 之前看到过 people have suggested使用类似control_flow_ops.with_dependencies([up_opt], score) 的东西来实现这一点。这在 TF 1.3 中似乎不再可能了。

总而言之,我如何评估 Keras 2.0.6 中的 TF 1.3 指标?这似乎是一个非常重要的功能。

最佳答案

您仍然可以使用control_dependencies

def mean_iou(y_true, y_pred):
score, up_opt = tf.metrics.mean_iou(y_true, y_pred, NUM_CLASSES)
K.get_session().run(tf.local_variables_initializer())
with tf.control_dependencies([up_opt]):
score = tf.identity(score)
return score

关于python - 如何在 Keras 中使用 TensorFlow 指标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45947351/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com