criterion = MarginCriterion()
Creates a criterion that optimizes a two-class classification hinge loss (margin-based loss) between input x (a Tensor of dimension 1) and output y (which is a scalar, either 1 or -1) :
loss(x,y) = forward(x,y) = max(0,m- y x).
m is the margin, which is by default 1.
criterion = MarginCriterion(marginValue)
sets a different value of m.
Example:
require "nn"
require "lab"
function gradUpdate(mlp, x, y, criterion, learningRate)
local pred = mlp:forward(x)
local err = criterion:forward(pred, y)
local gradCriterion = criterion:backward(pred, y)
mlp:zeroGradParameters()
mlp:backward(x, gradCriterion)
mlp:updateParameters(learningRate)
end
mlp=nn.Sequential()
mlp:add(nn.Linear(5,1))
x1=lab.rand(5)
x2=lab.rand(5)
criterion=nn.MarginCriterion(1)
for i=1,1000 do
gradUpdate(mlp,x1,1,criterion,0.01)
gradUpdate(mlp,x2,-1,criterion,0.01)
end
print(mlp:forward(x1))
print(mlp:forward(x2))
print(criterion:forward(mlp:forward(x1),1))
print(criterion:forward(mlp:forward(x2),-1))
gives the output:
1.0043 [torch.Tensor of dimension 1] -1.0061 [torch.Tensor of dimension 1] 0 0i.e. the mlp successfully separates the two data points such that they both have a margin of 1, and hence a loss of 0.