2017-03-06 41 views
0

我目前使用的是Torch 7,我需要自定义丢失函数,特别是交叉熵错误函数。我如何定制火炬中的损失功能?

我想添加一些交叉熵错误函数的参数,我找不到应修改的部分。

我看了CrossEntropyCriterion.lua,但仍然不知道方式,因为我没有看到这个文件中的任何方程式。

谁能告诉我方程在哪里?或者我应该修改哪个文件?

+0

的[添加我的自定义损失函数火炬]可能的复制(http://stackoverflow.com/questions/33648796/add -my-custom-loss-function-to-torch) –

回答

0

为了定制损失函数,您必须更改方法__init,updateOutputupdateGradInput

  • __init是类的初始化功能,当您使用:forward()方法在你的标准当您使用:backward()
  • updateGradInput会叫
  • updateOutput将被调用,它是你的标准的梯度

定制标准的结构如下所示:

local yourCriterion, parent = torch.class('nn.yourCriterion', 'nn.Criterion') 

function yourCriterion:__init(your_parameters): 
    parent.__init(self) 
    ... (you can add as many parameters as you want to your criterion 
     and give them the name your prefer) 
    self.parameters = your_parameters 


function yourCriterion:updateOutput(input) 
    ... (your criterion code here) 
    return value_of_the_criterion 

function yourCriterion:updateGradInput(input): 
    ... (your criterion gradient code here) 
    return gradient 

[编辑]:你可以在这里找到交叉熵准则的代码https://github.com/torch/nn/blob/master/CrossEntropyCriterion.lua

+0

对不起,我忘了把链接放到交叉熵标准中。我编辑了我的答案来添加它。 –

+0

感谢您的回答:)我会尽力的 –