Keras中的自定义损失函数 [英] Custom loss function in Keras

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

问题描述

我正在研究使用CNN作为特征提取器和完全连接的块进行分类的图像类增量分类器方法.

I'm working on a image class-incremental classifier approach using a CNN as a feature extractor and a fully-connected block for classifying.

首先,我对VGG训练有素的网络进行了微调,以完成一项新任务.在为新任务训练了网络之后,我将为每个班级存储一些示例,以避免忘记何时有新班级出现.

First, I did a fine-tuning of a VGG per-trained network to do a new task. Once the net is trained for the new task, i store some examples for every class in order to avoid forgetting when new classes are available.

当某些类可用时,我必须计算示例的每个输出,包括新类的示例.现在为旧类的输出添加零并在新类输出上添加与每个新类相对应的标签,我有我的新标签,即: 如果有3个新班级进入....

When some classes are available, i have to compute every output of the exemplars included the exemplars for the new classes. Now adding zeros to the outputs for old classes and adding the label corresponding to each new class on the new classes outputs i have my new labels, i.e: if 3 new classes enter....

旧类类型输出:[0.1, 0.05, 0.79, ..., 0 0 0]

新的类类型输出:[0.1, 0.09, 0.3, 0.4, ..., 1 0 0] **最后的输出对应于该类.

New class type output: [0.1, 0.09, 0.3, 0.4, ..., 1 0 0] **the last outputs correspond to the class.

我的问题是,如何更改自定义函数的损失函数以训练新课程? 我要实现的损失函数定义为:

My question is, how i can change the loss function for a custom one to train for the new classes? The loss function that i want to implement is defined as:

其中蒸馏损失对应于旧类以避免遗忘的输出,分类损失对应于新类.

where distillation loss corresponds to the outputs for old classes to avoid forgetting, and classification loss corresponds to the new classes.

如果您可以提供示例代码来更改keras中的损失函数,那就很好了.

If you can provide me a sample of code to change the loss function in keras would be nice.

谢谢!!!!!

推荐答案

您所要做的就是为此定义一个函数,使用keras后端函数进行计算.该函数必须具有真实值和模型预测值.

All you have to do is define a function for that, using keras backend functions for calculations. The function must take the true values and the model predicted values.

现在,由于我不确定您的函数中的g,q,x和y是什么,因此我将在此处创建一个基本示例,而不关心它的含义或它是否是实际有用的函数:

Now, since I'm not sure about what are g, q, x an y in your function, I'll just create a basic example here without caring about what it means or whether it's an actual useful function:

import keras.backend as K

def customLoss(yTrue,yPred):
    return K.sum(K.log(yTrue) - K.log(yPred))

所有后端功能都可以在此处看到: https://keras.io/backend/#backend-功能

All backend functions can be seen here: https://keras.io/backend/#backend-functions

然后,使用该函数而不是常规函数来编译模型:

After that, compile your model using that function instead of a regular one:

model.compile(loss=customLoss, optimizer = .....)

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

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