torch7 nn 模块 ClassNLLCriterion

nn模块中的loss函数有一个选择就是:nn.ClassNLLCriterion。既The negative log likelihood criterion。

需要说明的是,其表达式criterion = nn.ClassNLLCriterion([weights]) 中,weights是用于类别加权的,为一个1维的Tensor。

============模块自带文档======================

ClassNLLCriterion

 criterion = nn.ClassNLLCriterion([weights])

The negative log likelihood criterion. It is useful to train a classication problem with n classes.
If provided, the optional argument weights should be a 1D Tensor assigning weight to each of the classes.
This is particularly useful when you have an unbalanced training set.

The input given through a forward() is expected to contain log-probabilities of each class: input has to be a 1D Tensor of size n .
Obtaining log-probabilities in a neural network is easily achieved by adding a  LogSoftMax  layer in the last layer of your neural network.
You may use  CrossEntropyCriterion  instead, if you prefer not to add an extra layer to your network.
This criterion expect a class index (1 to the number of class) as target when calling  forward(input, target ) and  backward(input, target) .

The loss can be described as:
 loss(x, class) = -x[class]

or in the case of the weights argument being specified:
 loss(x, class) = -weights[class] * x[class]

The following is a code fragment showing how to make a gradient step given an input x , a desired output y (an integer 1 to n , in this case n = 2 classes), a network mlp and a learning rate learningRate :
function gradUpdate(mlp, x, y, learningRate)
   local criterion = nn.ClassNLLCriterion()
   pred = mlp:forward(x)
   local err = criterion:forward(pred, y)
   mlp:zeroGradParameters()
   local t = criterion:backward(pred, y)
   mlp:backward(x, t)
   mlp:updateParameters(learningRate)
end     
==============================================

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值