我正在研究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')])