看完这个例子你就懂了
# 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]