简体   繁体   English

为Keras编写元素的自定义损失函数元素

[英]Writing a custom loss function element by element for Keras

I am new to machine learning, python and tensorflow. 我是机器学习,python和tensorflow的新手。 I am used to code in C++ or C# and it is difficult for me to use tf.backend. 我习惯用C ++或C#编写代码,我很难使用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. 我正在尝试为LSTM网络编写自定义丢失函数,该网络试图预测时间序列的下一个元素是正还是负。 My code runs nicely with the binary_crossentropy loss function. 我的代码与binary_crossentropy损失函数运行良好。 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. 我现在想改进我的网络有一个损失函数,如果预测概率大于0.5,则增加下一个时间序列元素的值,如果概率小于或等于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

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? 如果没有,你能用tf.backend帮助我吗? Thanks a lot 非常感谢

From keras backend functions, you have the function greater that you can use: 从keras后端函数,您可以使用greater的函数:

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=?)

The axis parameter should be used according to what you want to sum. 应根据要求的总和使用axis参数。

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 

Now, if you have only a 2D target: 现在,如果您只有2D目标:

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

If you simply want to sum everything for every sequence, then just don't put the axis parameter. 如果您只想对每个序列求和,那么就不要放轴参数。

Important note about this function: 关于此功能的重要说明:

Since it contains only values from yTrue , it cannot backpropagate to change the weights. 由于它仅包含来自yTrue值,因此无法反向传播以更改权重。 This will lead to a "none values not supported" error or something very similar. 这将导致“无值不支持”错误或类似的东西。

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. 虽然yPred (连接到模型权重的那个)在函数中使用,但它仅用于获得真正的x false条件,这是不可微分的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM