定义自定义指标以计算"tensorflow.keras" "geometric mean score"时出现问题



我正在tensorflow.keras中处理一个imabalanced分类问题;几何平均分数";正如交叉验证中的这个答案所建议的那样。我在一个名为不平衡学习的包中找到了它的实现,并意识到它不能用作tensorflow.keras.Model.compile(metrics=[])中的度量之一;由于我也想在每次调用中都向它传递一个参数,我决定自己实现一个自定义度量并使用它。但我在测试过程中遇到了一个错误,上面写着:

AttributeError: 'GeometricMeanScore' object has no attribute '_trainable'

这是我的自定义度量代码:

from imblearn.metrics import geometric_mean_score
from tensorflow.keras import metrics
import numpy as np
class GeometricMeanScore(metrics.Metric):
    def __init__(self, average):
        # this 'average' is an argument "geometric_mean_score" takes for calculation.
        self.average = average
        # to store result
        self.geometric_score = 0
    # from looking at source code on github, I could learn that function that will be called for calculation is named 'update_state' and this function is what that accepts 'y_true' and 'y_pred'
    def update_state(self, y_true, y_pred):
        # store the result
        self.geometric_score = geometric_mean_score(y_pred=y_pred, y_true=y_true, average=self.average)
    def result(self):
        # access/print the result during every batch of every epoch.
        return self.geometric_score

测试:

# creating an instance
abc = GeometricMeanScore(average='weighted')
abc.update_state(y_true=np.array([0, 1, 2, 0, 1, 2]), y_pred=np.array([0, 2, 1, 0, 0, 1]))
print(abc.result())

完全错误:

C:UsersneevaN_ReddyAppDataLocalProgramsPythonPython38python.exe "C:/Users/neevaN_Reddy/Documents/custom_metric/defining custom meric.py"
2020-07-22 12:09:24.916554: W tensorflow/stream_executor/platform/default/dso_loader.cc:55] Could not load dynamic library 'cudart64_101.dll'; dlerror: cudart64_101.dll not found
2020-07-22 12:09:24.916874: I tensorflow/stream_executor/cuda/cudart_stub.cc:29] Ignore above cudart dlerror if you do not have a GPU set up on your machine.
Traceback (most recent call last):
  File "C:/Users/neevaN_Reddy/Documents/custom_metric/defining custom meric.py", line 19, in <module>
    abc.update_state(y_true=[0, 1, 2, 0, 1, 2], y_pred=[0, 2, 1, 0, 0, 1])
  File "C:UsersneevaN_ReddyAppDataLocalProgramsPythonPython38libsite-packagestensorflowpythonkerasutilsmetrics_utils.py", line 80, in decorated
    for weight in metric_obj.weights:
  File "C:UsersneevaN_ReddyAppDataLocalProgramsPythonPython38libsite-packagestensorflowpythonkerasenginebase_layer.py", line 1114, in weights
    return self.trainable_weights + self.non_trainable_weights
  File "C:UsersneevaN_ReddyAppDataLocalProgramsPythonPython38libsite-packagestensorflowpythonkerasenginebase_layer.py", line 1080, in trainable_weights
    if self.trainable:
  File "C:UsersneevaN_ReddyAppDataLocalProgramsPythonPython38libsite-packagestensorflowpythonkerasenginebase_layer.py", line 1007, in trainable
    return self._trainable
AttributeError: 'GeometricMeanScore' object has no attribute '_trainable'
Process finished with exit code 1

我缺少什么?我如何修复这个错误并在tf.keras中使用它,如下所示:

tensorflow.keras.Model.compile(metrics=[GeometricMeanScore(average='weighted')])
函数geometric_mean_score()以NumPy数组作为输入。但是keras会将张量传递给您的自定义度量函数。

这里和这里是相关的帖子。

这是一篇关于自定义分段丢失函数及其在TensorFlow中的实现的博客文章。它可能会帮助和指导您如何编写自定义度量/损失函数。

相关内容

  • 没有找到相关文章

最新更新