作为度量函数的平衡错误率 [英] Balanced Error Rate as metric function
问题描述
我正在尝试使用 Keras 的序列模型解决二元分类问题
并且必须满足给定的平衡错误率 (BER)
所以我认为使用 BER 而不是准确性作为衡量标准是个好主意.
我的 BER 自定义指标实现如下所示:
defbalanced_error_rate(y_true, y_pred):标签 = 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)形状.0>类型信息:>类型(y_true)<class 'theano.tensor.var.TensorVariable'>>类型(K.shape(y_true))<class 'theano.tensor.var.TensorVariable'>
...我不知道为什么.
<小时>我正在寻找:
获取数组维度的方法/解释为什么 shape
表现得像它/为什么 y_true
似乎具有 0
维度
或
一种通过重复给定的行/列向量来创建具有给定宽度/高度的张量矩阵的方法.
或
使用张量函数计算 BER 的更智能解决方案.
获取数组维度的方法/解释为什么形状像它一样/为什么
y_true
似乎有 0 个维度
print
和像 Theano 这样的抽象库的问题是,你通常不会得到值,而是值的表示.所以如果你这样做
print(foo.shape)
您不会得到实际的形状,而是在运行时完成的操作的表示.由于这都是在外部设备上计算的,因此计算不会立即运行,而是在创建具有适当输入的函数(或调用 foo.shape.eval()
)后才运行.
打印值的另一种方法是在使用值时使用theano.printing.Print
,例如:
shape = theano.printing.Print('foo的形状')(foo.shape)# 使用形状(不是 foo.shape!)
<块引用>
一种通过重复给定的行/列向量来创建具有给定宽度/高度的张量矩阵的方法.
参见theano.tensor.重复
.numpy 中的示例(用法非常相似):
I am trying to solve a binary classification problem with the sequential model from Keras
and have to meet a given Balanced Error Rate (BER)
So I thought it would be a good idea to use the BER instead of accuracy as a metric.
My custom metric implementation for BER looks like this:
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
The idea is to create a matrix from the available labels and compare it to the input data (then sum the ones) to get the number of elements of this label....
My problem is that:
> 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'>
...and I can't find out why.
I am now looking for:
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
or
A method to create a tensor matrix with a given with/height by repeating a given row/column vector.
or
A smarter solution to calculate the BER using tensor functions.
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
The deal with print
and abstraction libraries like Theano is that you usually do not get the values but a represenation of the value. So if you do
print(foo.shape)
You won't get the actual shape but a representation of the operation that is done at runtime. Since this is all computed on an external device the computation is not run immediately but only after creating a function with appropriate inputs (or calling foo.shape.eval()
).
Another way to print the value is to use theano.printing.Print
when using the value, e.g.:
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.
See theano.tensor.repeat
for that. Example in numpy (usage is quite similar):
>>> x
array([[1, 2, 3]])
>>> x.repeat(3, axis=0)
array([[1, 2, 3],
[1, 2, 3],
[1, 2, 3]])
这篇关于作为度量函数的平衡错误率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!