如何在theano上实现加权二元交叉熵?

时间:2016-09-09 12:51:51

标签: python theano keras lasagne cross-entropy

如何在theano上实现加权二进制交叉熵?

我的卷积神经网络仅预测0~1(sigmoid)。

我想以这种方式惩罚我的预测:

Cost-Table

基本上,我希望在模型预测为0时惩罚更多,但事实是1。

问题:如何使用theano和lasagne创建此加权二进制CrossEntropy 函数?

我在下面尝试了这个

prediction = lasagne.layers.get_output(model)


import theano.tensor as T
def weighted_crossentropy(predictions, targets):

    # Copy the tensor
    tgt = targets.copy("tgt")

    # Make it a vector
    # tgt = tgt.flatten()
    # tgt = tgt.reshape(3000)
    # tgt = tgt.dimshuffle(1,0)

    newshape = (T.shape(tgt)[0])
    tgt = T.reshape(tgt, newshape)

   #Process it so [index] < 0.5 = 0 , and [index] >= 0.5 = 1


    # Make it an integer.
    tgt = T.cast(tgt, 'int32')


    weights_per_label = theano.shared(lasagne.utils.floatX([0.2, 0.4]))

    weights = weights_per_label[tgt]  # returns a targets-shaped weight matrix
    loss = lasagne.objectives.aggregate(T.nnet.binary_crossentropy(predictions, tgt), weights=weights)

    return loss

loss_or_grads = weighted_crossentropy(prediction, self.target_var)

但我收到以下错误:

TypeError:重塑形状中的新形状必须是向量或标量的列表/元组。转换为矢量后得到Subtensor {int64} .0。

参考:https://github.com/fchollet/keras/issues/2115

参考:https://groups.google.com/forum/#!topic/theano-users/R_Q4uG9BXp8

2 个答案:

答案 0 :(得分:2)

感谢lasagne group的开发人员,我通过构建自己的损失函数来解决这个问题。

loss_or_grads = -(customized_rate * target_var * tensor.log(prediction) + (1.0 - target_var) * tensor.log(1.0 - prediction))

loss_or_grads = loss_or_grads.mean()

答案 1 :(得分:0)

解决语法错误:

更改

newshape = (T.shape(tgt)[0])
tgt = T.reshape(tgt, newshape)

newshape = (T.shape(tgt)[0],)
tgt = T.reshape(tgt, newshape)

T.reshape期待一个轴元组,你没有提供这个,因此错误。

在惩罚假阴性(预测0,真相1)之前,请确保此预测误差不是基于训练数据的统计数据,如@uyaseen suggested