Using categorical_crossentropy for only two classes

纵饮孤独 提交于 2020-01-25 07:03:22

问题


Computer vision and deep learning literature usually say one should use binary_crossentropy for a binary (two-class) problem and categorical_crossentropy for more than two classes. Now I am wondering: is there any reason to not use the latter for a two-class problem as well?


回答1:


  • categorical_crossentropy:
    • accepts only one correct class per sample
    • will take "only" the true neuron and make the crossentropy calculation with that neuron
  • binary_crossentropy:
    • accepts many correct classes per sample
    • will do the crossentropy calculation for "all neurons", considering that each neuron can be two classes, 0 and 1.

A 2-class problem can be modeled as:

  • 2-neuron output with only one correct class: softmax + categorical_crossentropy
  • 1-neuron output, one class is 0, the other is 1: sigmoid + binary_crossentropy

Explanation

Notice how in categorical crossentropy (the first equation), the term y_true is only 1 for the true neuron, making all other neurons equal to zero.

The equation can be reduced to simply: ln(y_pred[correct_label]).

Now notice how binary crossentropy (the second equation in the picture) has two terms, one for considering 1 as the correct class, another for considering 0 as the correct class.



来源:https://stackoverflow.com/questions/59216024/using-categorical-crossentropy-for-only-two-classes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!