Tensorflow中的交叉熵(Cross Entropy)
Cross Entropy (Sigmoid)
适用于二分类,输入函数的logits
和labels
应当是一维的。如果输入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}}
sigmoid(x)=x^=1+e−x1
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)
loss=−ylogx^−(1−y)log(1−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=[1],loss=−log1+e−51=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=[0],loss=−log1+e−5e−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
x=[5.0],y=[−1],loss=log1+e−51−2log1+e−5e−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
接收的logits
和labels
至少是二维的,sparse_softmax_cross_entropy_with_logits
接收的logits
至少是二维的,但labels
不是One-Hot的,而是类别的下标,例如
[
0
,
0
,
1
,
0
]
[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}}
softmax(x)=x^i=∑kexkexi
l
o
s
s
=
−
∑
k
y
k
l
o
g
x
^
i
loss=-\sum_k y_k log\hat x_i
loss=−k∑yklogx^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,−1]],loss=−loge−1+e1e−1+loge−1+e1e1=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
x=[[−1.0,1.0]],y=[[1,0]],loss=−loge−1+e1e−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