http://blog.csdn.net/u010167269/article/details/51966427
Introduction
CrossentropyCriterion in torch
CrossEntropyCriterion and ClassNLLCriterion only support with hard targets.
For examples, in multi-class classification task,
suppose we have 4 classes: dog, cat, carrot, book.
For one image whose annotation is “cat”, the label will be P=(0,1,0,0) P = ( 0 , 1 , 0 , 0 ) .
And the output of the model is Q=(0.1,0.7,0.01,0.19) Q = ( 0.1 , 0.7 , 0.01 , 0.19 )
CrossEntropyCriterion(torch)
loss=−log(Q2) l o s s = − l o g ( Q 2 )
Even if the label is soft P=(0.3,0.5,0.2,0.1) P = ( 0.3 , 0.5 , 0.2 , 0.1 ) ,
CrossEntropyCriterion(torch) won’t use other classes’ label when calculating the loss.
MyCrossEntropyCriterion
In fact, the crossentropy should be loss=−∑ipilog(qi)=−p1log(q1)−p2log(q2)−... l o s s = − ∑ i p i l o g ( q i ) = − p 1 l o g ( q 1 ) − p 2 l o g ( q 2 ) − . . .
One more thing:
“DistKLDivCriterion” is the KL divergence(relative entropy),
−∑(pi)log(pi/qi)=∑[(pi)log(p