我实现了此页中显示的自定义指标(CatgoricalTruePositives) https://www.tensorflow.org/alpha/guide/keras/training_and_evaluation
我认为它充满了错误,并有一些评论说(#fix this)。 无论如何,这有什么特别的错误。 NN的准确性接近99%,但是,该指标表示:
binary_true_positives: 8459.0000
(也显示在网站上)。 如果MNIST中的样本数量为50,000,则其中至少45k应该是真实阳性。 他们的简单自定义指标示例出了什么问题?
为完整起见,这是他们的示例:
class CatgoricalTruePositives(keras.metrics.Metric):
def __init__(self, name='binary_true_positives', **kwargs):
super(CatgoricalTruePositives, self).__init__(name=name, **kwargs)
self.true_positives = self.add_weight(name='tp', initializer='zeros')
def update_state(self, y_true, y_pred, sample_weight=None):
y_pred = tf.argmax(y_pred)
values = tf.equal(tf.cast(y_true, 'int32'), tf.cast(y_pred, 'int32'))
values = tf.cast(values, 'float32')
if sample_weight is not None:
sample_weight = tf.cast(sample_weight, 'float32')
values = tf.multiply(values, sample_weight)
return self.true_positives.assign_add(tf.reduce_sum(values)) # TODO: fix
def result(self):
return tf.identity(self.true_positives) # TODO: fix
def reset_states(self):
# The state of the metric will be reset at the start of each epoch.
self.true_positives.assign(0.)