看完这个例子你就懂了
# batch_size是2,时间步是2
ax = [[1,2,3,4],[2,3,4,5],[2,2,4,5],[2,1,4,5]]# 2*2,4
bx = [[2,1],[2,3]]
ax = tf.convert_to_tensor(ax,dtype=tf.float32)
bx = tf.convert_to_tensor(bx)
z = tf.nn.sparse_softmax_cross_entropy_with_logits(logits=ax, labels=tf.reshape(bx, [-1]))
with tf.Session() as sess:
print(sess.run(ax))
print(sess.run(z))
输出结果
[[1. 2. 3. 4.]
[2. 3. 4. 5.]
[2. 2. 4. 5.]
[2. 1. 4. 5.]]
[1.4401897 2.4401896 1.3835287 0.3618491]
本文通过具体实例,详细介绍了在TensorFlow中如何使用sparse_softmax_cross_entropy_with_logits函数进行交叉熵损失计算。该函数适用于分类任务,特别是在多分类场景下,能够直接接受整数标签作为输入,简化了计算过程。
1118

被折叠的 条评论
为什么被折叠?



