Tensorflow中的交叉熵(Cross Entropy)

Tensorflow中的交叉熵(Cross Entropy)

Cross Entropy (Sigmoid)

适用于二分类,输入函数的logitslabels应当是一维的。如果输入One-Hot过的logits,会被当做多个一维分别计算。注意不要将已经通过sigmoid计算得到的数值输入函数,那样会得到错误的结果。

s i g m o i d ( x ) = x ^ = 1 1 + e x sigmoid(x)=\hat x=\frac{1}{1+e^{-x}}
l o s s = y l o g x ^ ( 1 y ) l o g ( 1 x ^ ) loss=-ylog\hat x - (1-y)log(1-\hat x)
x = [ 5.0 ] , y = [ 1 ] , l o s s = l o g 1 1 + e 5 = 0.006715 x=[5.0],y=[1],loss=-log\frac{1}{1+e^{-5}}=0.006715
x = [ 5.0 ] , y = [ 0 ] , l o s s = l o g e 5 1 + e 5 = 5.006715 x=[5.0],y=[0],loss=-log\frac{e^{-5}}{1+e^{-5}}=5.006715
x = [ 5.0 ] , y = [ 1 ] , l o s s = l o g 1 1 + e 5 2 l o g e 5 1 + e 5 = 10.006715 x=[5.0],y=[-1],loss=log\frac{1}{1+e^{-5}}-2log\frac{e^{-5}}{1+e^{-5}}=10.006715

# 3 samples
preds = [5., 5., 5.]
labels = [1., 0., -1.]
loss = tf.nn.sigmoid_cross_entropy_with_logits(logits=preds, labels=labels)

Cross Entropy (Softmax)

适用于多分类,softmax_cross_entropy_with_logits_v2接收的logitslabels至少是二维的,sparse_softmax_cross_entropy_with_logits接收的logits至少是二维的,但labels不是One-Hot的,而是类别的下标,例如 [ 0 , 0 , 1 , 0 ] [0,0,1,0] 这样的label就是2(从0开始)。注意不要将已经通过softmax计算得到的数值输入函数,那样会得到错误的结果。

s o f t m a x ( x ) = x ^ i = e x i k e x k softmax(x)=\hat x_i=\frac{e^{x_i}}{\sum_k e^{x_k}}
l o s s = k y k l o g x ^ i loss=-\sum_k y_k log\hat x_i
x = [ [ 1.0 , 1.0 ] ] , y = [ [ 1 , 1 ] ] , l o s s = l o g e 1 e 1 + e 1 + l o g e 1 e 1 + e 1 = 2 x=[[-1.0,1.0]], y=[[1,-1]],loss=-log\frac{e^{-1}}{e^{-1}+e^{1}}+log\frac{e^{1}}{e^{-1}+e^{1}}=2
x = [ [ 1.0 , 1.0 ] ] , y = [ [ 1 , 0 ] ] , l o s s = l o g e 1 e 1 + e 1 = 2.137 x=[[-1.0,1.0]], y=[[1,0]],loss=-log\frac{e^{-1}}{e^{-1}+e^{1}}=2.137

# 4 samples
preds = [[10., -10.], [10., -10.], [10., -10.], [10.,-10.]]
labels = [[1., 0.], [1., -1.], [0., 1.], [-1., 1.]]
loss1 = tf.nn.softmax_cross_entropy_with_logits_v2(logits=preds, labels=labels)

labels = np.argmax(labels)
loss1 = tf.nn.sparse_softmax_cross_entropy_with_logits(logits=preds, labels=labels)

参考

Tensorflow sparse_softmax_cross_entropy_with_logits
Tensorflow sigmoid_cross_entropy_with_logits
Tensorflow softmax_cross_entropy_with_logits_v2

猜你喜欢

转载自blog.csdn.net/songbinxu/article/details/84791040