![](/img/trans.png)
[英]design a custom loss function in Keras (on the element index in tensors in Keras)
[英]Writing a custom loss function element by element for Keras
我是机器学习,python和tensorflow的新手。 我习惯用C ++或C#编写代码,我很难使用tf.backend。 我正在尝试为LSTM网络编写自定义丢失函数,该网络试图预测时间序列的下一个元素是正还是负。 我的代码与binary_crossentropy损失函数运行良好。 我现在想改进我的网络有一个损失函数,如果预测概率大于0.5,则增加下一个时间序列元素的值,如果概率小于或等于0.5,则减去它。 我试过这样的事情:
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帮助我吗? 非常感谢
从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=?)
应根据要求的总和使用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
现在,如果您只有2D目标:
axis=0 -> batch or sample dimension (number of sequences)
axis=1 -> predictions for each sequence
如果您只想对每个序列求和,那么就不要放轴参数。
由于它仅包含来自yTrue
值,因此无法反向传播以更改权重。 这将导致“无值不支持”错误或类似的东西。
虽然yPred
(连接到模型权重的那个)在函数中使用,但它仅用于获得真正的x false条件,这是不可微分的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.