简体   繁体   English

如何在Keras中创建一个返回多维值的指标?

[英]How to create a metric in Keras that return a multi-dimensional value?

I'm using keras to solve a multi-class problem. 我正在使用keras来解决多类问题。 My data is very unbalanced, so I'm trying to create something similar to a confusion matrix. 我的数据非常不平衡,所以我试图创建类似于混淆矩阵的东西。 My dataset is very large, and saved as HDF5, so I use HDF5Matrix to fetch the X and Y, making scikit-learn confusion matrix irrelevant (as far as I know). 我的数据集非常大,并保存为HDF5,因此我使用HDF5Matrix来获取X和Y,使scikit-learn混淆矩阵无关紧要(据我所知)。 I've seen it is possible to save the predictions and true labels , or output the error per label , however a more elegant solution would be to create a multi-dimensional metric that accumulates the (predicted,true) label pairs (sort of like a confusion matrix). 我已经看到可以保存预测和真实标签 ,或输出每个标签的错误 ,但更优雅的解决方案是创建一个累积(预测的,真实的)标签对的多维度量(有点像混乱矩阵)。 I have used the following callback to try and peek into what's going on per batch / epoch: 我使用了以下回调来试图查看每批/历元发生的事情:

from keras.callbacks import LambdaCallback
batch_print_callback = LambdaCallback(on_batch_end=lambda batch, logs: 
print(logs),on_epoch_end=lambda epoch, logs: print(logs))

but it only accumulates a single value (usually the average of sorts). 但它只累积一个值(通常是排序的平均值)。

I've also tried to see if it's possible to return the y_pred / y_true as following (to try and see if I can print a multi-dimensional value in the logs): 我还试着看看是否有可能返回y_pred / y_true如下(试着看看我是否可以在日志中打印多维值):

def pred(y_true, y_pred):
     return y_pred

def true(y_true, y_pred):
    return y_true

However, it doesn't return a multi-dimensional value as I expected So basically, my question is, can I use keras to accumulate multi-dimensional metric? 但是,它没有像我预期的那样返回多维值所以基本上,我的问题是,我可以使用keras来累积多维度量吗?

Well, to my best knowledge, it is not possible, since before returning the value of a tensor, K.mean is applied. 嗯,据我所知,这是不可能的,因为在返回张量值之前,应用了K.mean。 I posted an issue about this on keras github. 我在keras github上发布了一个关于此的问题 The best design I came up with is a metric for each cell in the confusion matrix, and a callback that collects them, inpired by the thread mentioned in the question. 我想出的最好的设计是混淆矩阵中每个单元格的度量标准,以及收集它们的回调,由问题中提到的线程提供。 A sort-of working solution can be found here 可以在这里找到一种工作解决方案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM