定义自定义指标来计算“tensorflow.keras”的“几何平均分数”的问题

2024-03-20

我正在研究tensorflow.keras 中的不平衡分类问题。我决定按照建议计算“几何平均分数”这个答案经过交叉验证 https://stats.stackexchange.com/a/289132/233268。我找到了一个其实施 https://imbalanced-learn.readthedocs.io/en/stable/generated/imblearn.metrics.geometric_mean_score.html在一个名为不平衡学习 https://imbalanced-learn.readthedocs.io/en/stable/index.html#并意识到它不能用作其中的指标之一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:\Users\neevaN_Reddy\AppData\Local\Programs\Python\Python38\python.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:\Users\neevaN_Reddy\AppData\Local\Programs\Python\Python38\lib\site-packages\tensorflow\python\keras\utils\metrics_utils.py", line 80, in decorated
    for weight in metric_obj.weights:
  File "C:\Users\neevaN_Reddy\AppData\Local\Programs\Python\Python38\lib\site-packages\tensorflow\python\keras\engine\base_layer.py", line 1114, in weights
    return self.trainable_weights + self.non_trainable_weights
  File "C:\Users\neevaN_Reddy\AppData\Local\Programs\Python\Python38\lib\site-packages\tensorflow\python\keras\engine\base_layer.py", line 1080, in trainable_weights
    if self.trainable:
  File "C:\Users\neevaN_Reddy\AppData\Local\Programs\Python\Python38\lib\site-packages\tensorflow\python\keras\engine\base_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将张量传递给您的自定义度量函数。

Here https://stackoverflow.com/a/37663327/11220884 and here https://datascience.stackexchange.com/q/13746/84576是相关帖子。

This https://lars76.github.io/neural-networks/object-detection/losses-for-segmentation/是一篇关于自定义分割损失函数及其在 TensorFlow 中的实现的博客文章。它可能会帮助并指导您如何编写自定义指标/损失函数。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

定义自定义指标来计算“tensorflow.keras”的“几何平均分数”的问题 的相关文章

随机推荐