我想编写一个我正在关注的自定义指标评估器这个链接 https://www.tensorflow.org/beta/guide/keras/training_and_evaluation#specifying_a_loss_metrics_and_an_optimizer。我的虚拟代码是
import tensorflow as tf
from tensorflow import keras
class DummyMetric(keras.metrics.Metric):
def __init__(self, name='categorical_true_positives', **kwargs):
super(DummyMetric, 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):
print("Evaluating tensor of shape {} against gt of shape {}".format(y_pred.shape, y_true.shape))
self.true_positives.assign_add(1.0)
def result(self):
return self.true_positives
def reset_states(self):
# The state of the metric will be reset at the start of each epoch.
self.true_positives.assign(0.)
我的tensorflow版本是从源安装的1.13.1.
keras.metrics.Metric
throws
AttributeError:模块“tensorflow._api.v1.keras.metrics”没有属性“Metric”。
当我做pip install tensorflow-gpu==1.14
然后这个错误就消失了。
如果可能的话,请建议任何解决方案/黑客,这将使它在不升级到 1.14 的情况下工作