loss1为原生态
loss2为tensorflow封装
import numpy as np
import tensorflow as tf
labels = tf.one_hot(np.array([0,1,2,3],np.float32),4)
b = np.array([[0.1,1.1,2.8,0.75]] * 4,np.float32)
s = tf.nn.softmax(b)
c = -tf.log(s) * labels
loss1 = tf.reduce_mean(tf.reduce_sum(c,axis=1))
loss2 = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2(logits=b,labels=labels))
sess = tf.Session()
print(sess.run(loss1))
print(sess.run(loss2))
运行结果:
1.9335859
1.9335859