Search code examples
customizationtorchloss

Modifying ClassNLLCriterion in Torch


I am new to Torch and I want to create a custom loss function in Torch which is a modification of ClassNLLCriterion. Concretely, ClassNLLCriterion loss is:

loss(x, class) = -x[class]

I want to modify this to be:

loss(x, class) = -x[class] + F(x)

where F(x) is a function that looks x up in a table (as a key) and outputs its value.

My question is, what's the correct way of implementing this custom criterion? The updateOutput() function seems straightforward, but how do I implement the updateGradInput() function?


Solution

  • If F(x) is not differentiable with respect to the network parameters, then you can't use it within the loss function. Differentiability is a necessary condition to perform gradient descent during backpropagation. See Non-smooth and non-differentiable customized loss function tensorflow.