Tensorflow中的交叉熵(Cross Entropy)
Cross Entropy (Sigmoid)
适用于二分类,输入函数的logits
和labels
应当是一维的。如果输入One-Hot过的logits,会被当做多个一维分别计算。注意不要将已经通过sigmoid
计算得到的数值输入函数,那样会得到错误的结果。
sigmoid(x)=x^=1+e−x1
loss=−ylogx^−(1−y)log(1−x^)
x=[5.0],y=[1],loss=−log1+e−51=0.006715
x=[5.0],y=[0],loss=−log1+e−5e−5=5.006715
x=[5.0],y=[−1],loss=log1+e−51−2log1+e−5e−5=10.006715
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
接收的logits
和labels
至少是二维的,sparse_softmax_cross_entropy_with_logits
接收的logits
至少是二维的,但labels
不是One-Hot的,而是类别的下标,例如
[0,0,1,0]这样的label就是2(从0开始)。注意不要将已经通过softmax
计算得到的数值输入函数,那样会得到错误的结果。
softmax(x)=x^i=∑kexkexi
loss=−k∑yklogx^i
x=[[−1.0,1.0]],y=[[1,−1]],loss=−loge−1+e1e−1+loge−1+e1e1=2
x=[[−1.0,1.0]],y=[[1,0]],loss=−loge−1+e1e−1=2.137
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