gpt4 book ai didi

python - 平衡错误率作为度量函数

转载 作者:太空宇宙 更新时间:2023-11-03 16:28:29 26 4
gpt4 key购买 nike

我正在尝试使用 Keras 的顺序模型解决二元分类问题
并且必须满足给定的Balanced Error Rate (BER)

所以我认为使用 BER 而不是准确性作为指标是一个好主意。

我的 BER 自定义指标实现如下所示:

def balanced_error_rate(y_true, y_pred):
labels = theano.shared(np.asmatrix([[0, 1]], dtype='int8'))
label_matrix = K.repeat_elements(labels, K.shape(y_true)[0], axis=1)
true_matrix = K.repeat_elements(y_true, K.shape(labels)[0], axis=1)
pred_matrix = K.repeat_elements(K.round(y_pred), K.shape(labels)[0], axis=1)

class_lens = K.sum(K.equal(label_matrix, true_matrix), axis=1)
return K.sum(K.sum(class_lens - K.sum(K.equal(label_matrix, K.not_equal(true_matrix,pred_matrix)), axis=1), axis=0)/class_lens, axis=0)/2

这个想法是根据可用标签创建一个矩阵,并将其与输入数据进行比较(然后对这些数据求和)以获得该标签的元素数量......

我的问题是:

> K.shape(y_true) 
Shape.0


> Typeinfo:

> type(y_true)
<class 'theano.tensor.var.TensorVariable'>

> type(K.shape(y_true))
<class 'theano.tensor.var.TensorVariable'>

...我找不到原因。

<小时/>

我现在正在寻找:

获取数组维度的方法/解释为什么 shape 的行为如此/为什么 y_true 似乎具有 0 维度

通过重复给定的行/列向量来创建具有给定宽度/高度的张量矩阵的方法。

使用张量函数计算 BER 的更智能解决方案。

最佳答案

A way to get the array dimensions / an explanation why shape acts like it does / the reason why y_true seems to have 0 dimensions

处理 print 和 Theano 等抽象库的问题是,您通常不会获得值,而是获得值的表示。所以如果你这样做

print(foo.shape)

您不会获得实际的形状,而是获得运行时完成的操作的表示。由于这一切都是在外部设备上计算的,因此计算不会立即运行,而是仅在使用适当的输入创建函数(或调用 foo.shape.eval())之后运行。

打印该值的另一​​种方法是在使用该值时使用theano.printing.Print,例如:

shape = theano.printing.Print('shape of foo')(foo.shape)
# use shape (not foo.shape!)

A method to create a tensor matrix with a given with/height by repeating a given row/column vector.

参见theano.tensor.repeat为了那个原因。 numpy 中的示例(用法非常相似):

>>> x
array([[1, 2, 3]])
>>> x.repeat(3, axis=0)
array([[1, 2, 3],
[1, 2, 3],
[1, 2, 3]])

关于python - 平衡错误率作为度量函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37797621/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com