下面几种交叉熵计算方法,得到的结果不尽相同,但是搭模型时用cross_entropy1的计算方式,得到的loss值会缓慢下降,其他几种loss值很大程度上会出现NAN,具体原因我也没搞明白~
注:cross_entropy1在计算时,内部会做softmax,所以最后输出全连接层时,不用做softmax
if __name__ == '__main__':
# 交叉熵损失函数test,四种交叉熵损失计算方式
# 第一种交叉熵计算方式不会出现nan
sess = tf.InteractiveSession()
input_label = np.array([[1.0,0.0],[1.0,0.0],[1.0,0.0],[0.0,1.0],[0.0,1.0]])
y_conv = np.array([[0.2,0.8],[0.9,0.1],[0.5,0.5],[0.3,0.7],[0.6,0.4]])
print(input_label)
print(y_conv)
input = tf.placeholder(tf.float32, [None, 2])
y1 = tf.placeholder(tf.float32, [None, 2])
# + (1-input)*tf.log(1-tf.nn.softmax(y1)
cross_entropy1 = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=y1, labels=input_label))
cross_entropy2 = tf.reduce_mean(-tf.reduce_sum(input*tf.log(tf.nn.softmax(y1)),reduction_indices = [1]) )
cross_entropy3 = -tf.reduce_mean(input * tf.log(tf.clip_by_value(tf.nn.softmax(y1), 1e-10, 1.0)))
cross_entropy4 = tf.reduce_mean(-tf.reduce_sum(input*tf.log(tf.clip_by_value(tf.nn.softmax(y1), 1e-10, 1.0)),reduction_indices = [1] ) )
cross_entropy5 = -tf.reduce_sum(input * tf.log(tf.nn.softmax(y1)))
tf.global_variables_initializer().run()
print("cross_entropy1 = ",sess.run(cross_entropy1,{input:input_label,y1:y_conv}))
print("cross_entropy2 = ",sess.run(cross_entropy2,{input:input_label,y1:y_conv}))
print("cross_entropy3 = ",sess.run(cross_entropy3,{input:input_label,y1:y_conv}))
print("cross_entropy4 = ",sess.run(cross_entropy4,{input:input_label,y1:y_conv}))
print("cross_entropy5 = ",sess.run(cross_entropy5,{input:input_label,y1:y_conv}))