了解 LSTM 架构中的密集层(标签和逻辑)
Understanding dense layer in LSTM architecture (labels & logits)
我正在研究这个笔记本 -- https://github.com/aamini/introtodeeplearning/blob/master/lab1/solutions/Part2_Music_Generation_Solution.ipynb -- 我们在其中使用嵌入层、LSTM 和带有 softmax 的最终密集层来生成音乐。
但是,我对我们如何计算损失感到有点困惑;据我了解,在这个笔记本中(在 compute_loss() 中),在任何给定的批次中,我们正在将预期标签(即注释本身)与 logits(即来自密集层的预测)进行比较。但是,这些预测不应该是概率分布吗?我们什么时候真正选择我们预测的标签?
稍微澄清一下我的问题:如果我们的标签形状是 (batch_size, # of time steps),而我们的 logits 形状是 (batch_size, # of time步骤,vocab_size),在 compute_loss() 函数中的什么时候我们实际上为每个时间步长选择了一个标签?
简短的回答是 Keras 损失函数 sparse_categorical_crossentropy()
可以满足您的所有需求。
在 LSTM 模型的每个时间步,该损失函数内的顶层密集层和 softmax 函数共同生成模型词汇表的概率分布,在本例中为音符。假设词汇表包含音符 A、B、C、D。那么生成的一种可能的概率分布是:[0.01, 0.70, 0.28, 0.01]
,这意味着模型将大量概率放在音符 B(索引 1)上,如下所示:
Label: A B C D
---- ---- ---- ---- ----
Index: 0 1 2 3
---- ---- ---- ---- ----
Prob: 0.01 0.70 0.28 0.01
假设真正的音符应该是 C,它由数字 2 表示,因为它在分布数组中的索引 2(索引从 0 开始)。要衡量预测分布与真实值分布之间的差异,请使用 sparse_categorical_crossentropy()
函数生成表示损失的浮点数。
可以在 this TensorFlow documentation page 上找到更多信息。在该页面上,他们有示例:
y_true = [1, 2]
y_pred = [[0.05, 0.95, 0], [0.1, 0.8, 0.1]]
loss = tf.keras.losses.sparse_categorical_crossentropy(y_true, y_pred)
您可以在该示例中看到有一批两个实例。对于第一个实例,真实标签为 1
,预测分布为 [0.05, 0.95, 0]
,对于第二个实例,真实标签为 2
,而预测分布为 [0.1, 0.8, 0.1]
.
这个函数在 2.5 节的 Jupyter Notebook 中使用:
To train our model on this classification task, we can use a form of the crossentropy loss (negative log likelihood loss). Specifically, we will use the sparse_categorical_crossentropy loss, as it utilizes integer targets for categorical classification tasks. We will want to compute the loss using the true targets -- the labels -- and the predicted targets -- the logits.
所以直接回答你的问题:
it is my understanding that in this notebook (in compute_loss()), in any given batch, we are comparing expected labels (which are the notes themselves) to the logits (i.e. predictions from the dense layer).
是的,你的理解是正确的。
However, aren't these predictions supposed to be a probability distribution?
是的,他们是。
When are we actually selecting the label that we are predicting against?
它是在 sparse_categorical_crossentropy()
函数内部完成的。如果您的分布是 [0.05, 0.95, 0]
,那么这隐含地意味着该函数预测索引 0 的概率为 0.05,索引 1 的概率为 0.95,索引 3 的概率为 0.0。
A little more clarification on my question: if the shape of our labels is (batch_size, # of time steps), and the shape of our logits is (batch_size, # of time steps, vocab_size), at what point in the compute_loss() function are we actually selecting a label for each time step?
它在那个函数里面。
我正在研究这个笔记本 -- https://github.com/aamini/introtodeeplearning/blob/master/lab1/solutions/Part2_Music_Generation_Solution.ipynb -- 我们在其中使用嵌入层、LSTM 和带有 softmax 的最终密集层来生成音乐。
但是,我对我们如何计算损失感到有点困惑;据我了解,在这个笔记本中(在 compute_loss() 中),在任何给定的批次中,我们正在将预期标签(即注释本身)与 logits(即来自密集层的预测)进行比较。但是,这些预测不应该是概率分布吗?我们什么时候真正选择我们预测的标签?
稍微澄清一下我的问题:如果我们的标签形状是 (batch_size, # of time steps),而我们的 logits 形状是 (batch_size, # of time步骤,vocab_size),在 compute_loss() 函数中的什么时候我们实际上为每个时间步长选择了一个标签?
简短的回答是 Keras 损失函数 sparse_categorical_crossentropy()
可以满足您的所有需求。
在 LSTM 模型的每个时间步,该损失函数内的顶层密集层和 softmax 函数共同生成模型词汇表的概率分布,在本例中为音符。假设词汇表包含音符 A、B、C、D。那么生成的一种可能的概率分布是:[0.01, 0.70, 0.28, 0.01]
,这意味着模型将大量概率放在音符 B(索引 1)上,如下所示:
Label: A B C D
---- ---- ---- ---- ----
Index: 0 1 2 3
---- ---- ---- ---- ----
Prob: 0.01 0.70 0.28 0.01
假设真正的音符应该是 C,它由数字 2 表示,因为它在分布数组中的索引 2(索引从 0 开始)。要衡量预测分布与真实值分布之间的差异,请使用 sparse_categorical_crossentropy()
函数生成表示损失的浮点数。
可以在 this TensorFlow documentation page 上找到更多信息。在该页面上,他们有示例:
y_true = [1, 2]
y_pred = [[0.05, 0.95, 0], [0.1, 0.8, 0.1]]
loss = tf.keras.losses.sparse_categorical_crossentropy(y_true, y_pred)
您可以在该示例中看到有一批两个实例。对于第一个实例,真实标签为 1
,预测分布为 [0.05, 0.95, 0]
,对于第二个实例,真实标签为 2
,而预测分布为 [0.1, 0.8, 0.1]
.
这个函数在 2.5 节的 Jupyter Notebook 中使用:
To train our model on this classification task, we can use a form of the crossentropy loss (negative log likelihood loss). Specifically, we will use the sparse_categorical_crossentropy loss, as it utilizes integer targets for categorical classification tasks. We will want to compute the loss using the true targets -- the labels -- and the predicted targets -- the logits.
所以直接回答你的问题:
it is my understanding that in this notebook (in compute_loss()), in any given batch, we are comparing expected labels (which are the notes themselves) to the logits (i.e. predictions from the dense layer).
是的,你的理解是正确的。
However, aren't these predictions supposed to be a probability distribution?
是的,他们是。
When are we actually selecting the label that we are predicting against?
它是在 sparse_categorical_crossentropy()
函数内部完成的。如果您的分布是 [0.05, 0.95, 0]
,那么这隐含地意味着该函数预测索引 0 的概率为 0.05,索引 1 的概率为 0.95,索引 3 的概率为 0.0。
A little more clarification on my question: if the shape of our labels is (batch_size, # of time steps), and the shape of our logits is (batch_size, # of time steps, vocab_size), at what point in the compute_loss() function are we actually selecting a label for each time step?
它在那个函数里面。