the inplace operation error comes from building encoded_target.
encoded_target is not differentiable anyways, so first build it and then wrap it in a Variable, like this:
 encoded_target = output.data.clone()
encoded_target[...] = 0
encoded_target.scatter_(1, target.unsqueeze(1), 1)
encoded_target = Variable(encoded_target)