Does custom loss function need custom Criterion?


(Janne) #1

What is the meaning of “criterion” when training?

I made my own loss_function, but do I need to create own criterion from it also? Or can I just use it directly?

My concrete problem is that my loss function takes 2 parameters, while criterion is initialized without parameters, making them incompatible. Is the criterion an object and calling its creator, since it has no parameters? Does it perhaps store propagation information?

criterion = nn.BCELoss()
vs
own_loss_function(y_pred, y_true)

Can I skip the criterion totally and just use directly loss-function, and will its autograd/backpropagation work in that case?

train code Before:
predictions = model(torch.cat((batch.t, batch.h))).squeeze(1)
loss = criterion(predictions, batch.l.float())

What I would want:
predictions =… same
loss = own_loss_function(predictions, batch.l.float())


(Omar Essam) #2

You can create your own custom loss function, however you will need to follow certain guidelines in Pytorch. I’m not a Pytorch expert myself to help with that, so feel free to take a look at that: