本文介绍了以y_true取决于y_pred的方式自定义Keras的损失函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究多标签分类器.我有很多输出标签[1、0、0、1 ...],其中1表示输入属于该标签,而0表示否则.

I'm working on a multi-label classifier. I have many output labels [1, 0, 0, 1...] where 1 indicates that the input belongs to that label and 0 means otherwise.

在我的情况下,我使用的损失函数基于MSE.我想以某种方式更改损失函数,即当输出标签为-1时,它将更改为该标签的预测概率.

In my case the loss function that I use is based on MSE. I want to change the loss function in a way that when the output label is -1 than it will change to the predicted probability of this label.

检查所附图像以最好地理解我的意思:情况是-当输出标签为-1时,我希望MSE等于零:

Check the attached images to best understand what I mean:The scenario is - when the output label is -1 I want the MSE to be equal to zero:

这是方案:

在这种情况下,我希望它更改为:

And in such case I want it to change to:

在这种情况下,第二个标签(中间输出)的MSE为零(这是特殊情况,我不希望分类器了解此标签).

In such case the MSE of the second label (the middle output) will be zero (this is a special case where I don't want the classifier to learn about this label).

感觉这是一种必需的方法,我真的不相信我是第一个考虑它的人,所以首先我想知道是否有这种训练Neural Net的方法的名称,其次我想知道我该怎么做.

It feels like this is a needed method and I don't really believe that I'm the first to think about it so firstly I wanted to know if there's a name for such way of training Neural Net and second I would like to know how can I do it.

我了解到我需要更改损失函数中的某些内容,但是我真的是Theano的新手,并且不确定如何在此处查找特定值以及如何更改张量的内容.

I understand that I need to change some stuff in the loss function but I'm really newbie to Theano and not sure about how to look there for a specific value and how to change the content of the tensor.

推荐答案

我相信这就是您想要的.

I believe this is what you looking for.

import theano
from keras import backend as K
from keras.layers import Dense
from keras.models import Sequential

def customized_loss(y_true, y_pred):
    loss = K.switch(K.equal(y_true, -1), 0, K.square(y_true-y_pred))
    return K.sum(loss)

if __name__ == '__main__':
    model = Sequential([ Dense(3, input_shape=(4,)) ])
    model.compile(loss=customized_loss, optimizer='sgd')

    import numpy as np
    x = np.random.random((1, 4))
    y = np.array([[1,-1,0]])

    output = model.predict(x)
    print output
    # [[ 0.47242549 -0.45106074  0.13912249]]
    print model.evaluate(x, y)  # keras's loss
    # 0.297689884901
    print (output[0, 0]-1)**2 + 0 +(output[0, 2]-0)**2 # double-check
    # 0.297689929093

这篇关于以y_true取决于y_pred的方式自定义Keras的损失函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-12 16:13