平衡错误率作为度量函数

Balanced Error Rate as metric function

我正在尝试使用 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 个维度

一种通过重复给定的 row/column 向量来创建具有给定 with/height 的张量矩阵的方法。

使用张量函数计算 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]])