Search code examples
c++functionneural-networkentropyloss

Cross-entropy error calculation


I do not fully understand how the cross entropy error is calculated. Since the log of some negative number is not defined (or imaginary) and the output of the neural network could be some negative number (weights and bias are initialized randomly) I often get NaN as result. How do I avoid that?

Example:

output[] = {-0.78, -0.98, 0.78, -0.68};
target[] = {-1.0,  -1.0,  1.0,   1.0};
error = ln(0.78) * 1.0 + ln(-0.68) * 1.0;

Solution

  • If your task is a binary classification I suggest you to change the target value "-1" to "0". In the last layer, the output layer, you have two neurones (one for each class). For the output of neural network you should use softmax function, it assigns probability to every class (meaning, you don't have negative values). In the end, the output for every instance is the label of the class which corresponds to bigger of these two probabilities.