我有以下代码:
# 'a' is indexed as integer 1 and 'and' is indexed as integer 2. Integer 0 is used for padding
vocab = ['a', 'and']
y_true = [[1, 2], [2, 1], [2, 0]]
y_pred = [[[0.95, 0.05], [0.1, 0.8]], [[0.05, 0.95], [0.8, 0.1]], [[0.05, 0.95], [0.1, 0.1]]]
scce = tf.keras.losses.SparseCategoricalCrossentropy()
loss = scce(y_true, y_pred).numpy()
我为创建
y_true
做词汇查找。因为我想使用由0
表示的填充,所以我分别使用1和2作为单词'a'和'and'的索引。
然后我将 y_true 输入到我的 Transformer 并得到
y_pred
。我的 Transformer 将输出单词 a
和 and
的概率分布。每个概率分布的大小是 2,因为我的词汇表中只有两个词。
当我想计算 los 时,我收到错误消息“收到的标签值 2 超出了 [0, 2) 的有效范围。”如何解决这个问题?请记住,整数 0 用于填充,因此它不会附加到
vocab
列表。
我需要吗