如何使用Keras构建自定义损失函数 [英] how to build custom loss function with Keras
问题描述
我有为暹罗网络实现的丢失功能.在Keras中,如果必须构建自己的损失函数,则应仅将输入参数作为(y_true,y_pred). 但就我而言,我有y_pred1,y_pred1,y_true1(class_label),y_true2(class_label),y_true3(相似性标签)
I have loss function which implemented for siamese network. In Keras if you have to build your own loss function, it should only take input arguments as (y_true, y_pred). But in my case I have y_pred1, y_pred1, y_true1(class_label), y_true2(class_label), y_true3(similarity label)
所以我的解决方案是将我喜欢的东西串联起来:
So my solution is to concatenate what I have like:
def my loss ( y_true, y_pred):
y_true1 = y_true[:, 0]
y_true2 = y_true[:, 1]
label = y_true[:, 2]
y_pred1 = y_pred[:, 0]
y_pred2 = y_pred[:, 1]
第二个问题是,我有一个参数(alpha),它是当前时期数的函数,我也应该将其传递给损失函数.
The second problem is, I have one parameter (alpha) which is a function of current epoch number that I should pass it to the loss function also.
通常,如果必须传递其他参数,则可以使用包装函数作为建议的解决方案
In general , if you have to pass some another argument you can use the wrapper function as the solution suggested here.
但是对于我来说,这无济于事,因为我的alpha值应根据当前纪元数进行更改.它基本上是当前纪元的Sigmoied函数.
But it will not help me in my case , because my alpha should be change depending on the current number of epoch. It is basically the Sigmoied function of the current epoch.
我可以跟踪纪元号的唯一方法是在自己的生成器中,因为我有内置在tfrecords中的数据集.因此,我正在使用自己的生成器将数据提供给模型.
The only way that I can track the epoch number is inside my own generator, because I have dataset built in tfrecords. So I am using my own generator to feed the data to model.
那么任何人都知道我该怎么办?如何跟踪当前纪元编号并使用它.
So any one have any idea what should I do? How I can track the current epoch number and use it.
推荐答案
重要!您是哪种情况?
- 案例1:具有3个输出的模型
- 案例2:一个具有一个输出的模型是三个输出的串联?
您需要三个独立的损失函数,每个函数将仅看到其自己的y_true
和y_pred
.
You need three independent loss functions, each function will see only its own y_true
and y_pred
.
def loss1(yTrue,yPred):
...
def loss2(yTrue,yPred):
...
def loss3(yTrue,yPred):
...
model.compile(loss=[loss1,loss2,loss3],...)
案例2
在这种情况下,您将能够按照建议的方式进行操作.
Case 2
In this case, you will be able to do it the way you proposed.
def my loss ( y_true, y_pred):
y_true1 = y_true[:, 0]
y_true2 = y_true[:, 1]
y_pred1 = y_pred[:, 0]
y_pred2 = y_pred[:, 1]
使用alpha
Alpha必须是张量",而不是普通的var:
Using alpha
Alpha must be a "tensor", not an ordinary var:
alpha = K.variable(someInitialNpArray, dtype=...)
alpha值必须更改",而不是重新分配:
The value of alpha must be "changed", not reassigned:
K.set_value(alpha, newValues)
现在,为on_epoch_end
创建一个LambdaCallback
,以更改alpha的值:
Now, create a LambdaCallback
for on_epoch_end
in order to change the value of alpha:
def changeAlpha(epoch,logs):
#maybe use epoch+1, because it starts with 0
K.set_value(alpha, valuesBasedOn(epoch))
alphaChanger = LambdaCallback(on_epoch_end=changeAlpha) #or on_epoch_begin (or start?)
损失:
def loss(true,pred):
#blablabla
#you can use alpha here
培训:
model.fit(..... callbacks = [alphaChanger])
model.fit_generator(......, callbacks = [alphaChanger])
这篇关于如何使用Keras构建自定义损失函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!