为Keras逐个元素编写自定义损失函数 [英] Writing a custom loss function element by element for Keras

查看:243
本文介绍了为Keras逐个元素编写自定义损失函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是机器学习,python和tensorflow的新手.我习惯于用C ++或C#编写代码,而我很难使用tf.backend. 我正在尝试为LSTM网络编写自定义损失函数,以尝试预测时间序列的下一个元素是正数还是负数.我的代码可以使用binary_crossentropy损失函数很好地运行.我现在要改善网络,使其具有损失函数,如果预测概率大于0.5,则将下一个时间序列元素的值相加,如果概率小于或等于0.5,则将其减去. 我尝试过这样的事情:

I am new to machine learning, python and tensorflow. I am used to code in C++ or C# and it is difficult for me to use tf.backend. I am trying to write a custom loss function for an LSTM network that tries to predict if the next element of a time series will be positive or negative. My code runs nicely with the binary_crossentropy loss function. I want now to improve my network having a loss function that adds the value of the next time series element if the predicted probability is greater than 0.5 and substracts it if the prob is less or equal to 0.5. I tried something like this:

def customLossFunction(y_true, y_pred):
    temp = 0.0
    for i in range(0, len(y_true)):
        if(y_pred[i] > 0):
            temp += y_true[i]
        else:
            temp -= y_true[i]
    return temp

显然,尺寸是错误的,但是由于我在调试时无法进入函数,因此很难在这里掌握尺寸. 您能否告诉我是否可以使用逐个元素的功能?如果是,怎么办?如果没有,您可以在tf.backend上帮我吗? 非常感谢

Obviously, dimensions are wrong but since I cannot step into my function while debugging, it is very hard to get a grasp of dimensions here. Can you please tell me if I can use an element-by-element function? If yes, how? And if not, could you help me with tf.backend? Thanks a lot

推荐答案

从keras后端函数中,您可以使用函数greater:

From keras backend functions, you have the function greater that you can use:

import keras.backend as K

def customLossFunction(yTrue,yPred)

    greater = K.greater(yPred,0.5)
    greater = K.cast(greater,K.floatx()) #has zeros and ones
    multiply = (2*greater) - 1 #has -1 and 1

    modifiedTrue = multiply * yTrue

    #here, it's important to know which dimension you want to sum
    return K.sum(modifiedTrue, axis=?)

应根据要求和的值使用axis参数.

The axis parameter should be used according to what you want to sum.

axis=0 -> batch or sample dimension (number of sequences)     
axis=1 -> time steps dimension (if you're using return_sequences = True until the end)     
axis=2 -> predictions for each step 

现在,如果您只有2D目标:

Now, if you have only a 2D target:

axis=0 -> batch or sample dimension (number of sequences)
axis=1 -> predictions for each sequence

如果您只是想对每个序列的所有内容求和,则不要放置axis参数.

If you simply want to sum everything for every sequence, then just don't put the axis parameter.

由于仅包含yTrue中的值,因此无法反向传播以更改权重.这将导致不支持任何值"错误或非常类似的错误.

Since it contains only values from yTrue, it cannot backpropagate to change the weights. This will lead to a "none values not supported" error or something very similar.

尽管在函数中使用了yPred(与模型的权重相连的那个),但它仅用于获取真x假条件,这是不可区分的.

Although yPred (the one that is connected to the model's weights) is used in the function, it's used only for getting a true x false condition, which is not differentiable.

这篇关于为Keras逐个元素编写自定义损失函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆