本文介绍了CNTK:定义自定义损失函数(Sørensen-Dice系数)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用作为CNTK / Python中的损失函数。如何定义自定义损失函数。

I'd like to use Wiki: Sørensen–Dice coefficient as a loss function in CNTK/Python. How can I define a custom loss function.

推荐答案

要回答更普遍的问题我如何定义自定义损失函数:

To answer your more general question "How can I define a custom loss function:"

在CNTK中,损失函数并不特殊。任何导致标量的表达式都可以用作损失函数。学习者将通过对小批量中所有样本的标量损耗值求和来计算小批量水平损耗,然后像通过任何CNTK表达式一样通过它进行反向传播。

In CNTK, loss functions are not special. Any expression that results in a scalar can be used as a loss function. The learner will compute the minibatch-level loss by summing up the scalar loss values of all samples in the minibatch, and backpropagate through it like through any CNTK expression.

,以下是定义平方误差损失的一种方法:

For example, the following is a way of defining a square-error loss:

def my_square_error(x,y):
    diff = x-y
    return times_transpose(diff, diff)

cross_entropy_with_softmax()损失可以用Python编写,如下所示:

and the cross_entropy_with_softmax() loss can be written in Python like this:

def my_cross_entropy_with_softmax(output, labels):
    logZ = reduce_log_sum(output)  # log of softmax denominator
    return times_transpose(labels, output) - logZ

最后,通过使用损失函数对多个损失进行加权求和,可以轻松实现多任务学习。

Lastly, multi-task learning can be trivially realized by using a loss function that is a weighted sum over multiple losses.

这篇关于CNTK:定义自定义损失函数(Sørensen-Dice系数)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-19 17:15