Luong 的 Concat 对齐分数问题
Luong's Concat Alignment Score Issue
我一直在努力理解注意力机制是如何工作的。当前查看 tfjs-examples/date-conversion-attention example. I've found out that in the example the dot product alignment score
(from Effective Approaches to Attention-based Neural Machine Translation
) 正在使用中。
所以这个表达式 表示为
let attention = tf.layers.dot({axes: [2, 2]}).apply([decoder, encoder]);
在code。
解码器 (h_t
) 的形状为 [10,64],编码器 (h_s
) 的形状为 [12,64],因此结果的形状为 [10,12]。到目前为止一切顺利。
现在我正在尝试实现 concat alignment score
,它看起来像这样
.
所以首先要做的是连接 h_t
和 h_s
。但是,它们的形状不同,所以我不知道如何进行。我应该以某种方式重塑张量吗?如果可以,形状是什么?
我一直在谷歌搜索以了解其他人是如何做到这一点的,并找到了 this。
#For concat scoring, decoder hidden state and encoder outputs are concatenated first
out = torch.tanh(self.fc(decoder_hidden+encoder_outputs))
但这似乎不对,因为它们对值求和而不是串联。
如有任何指导,我们将不胜感激。
更新 这是模型摘要:
__________________________________________________________________________________________________
Layer (type) Output shape Param # Receives inputs
==================================================================================================
input1 (InputLayer) [null,12] 0
__________________________________________________________________________________________________
embedding_Embedding1 (Embedding [null,12,64] 2240 input1[0][0]
__________________________________________________________________________________________________
input2 (InputLayer) [null,10] 0
__________________________________________________________________________________________________
lstm_LSTM1 (LSTM) [null,12,64] 33024 embedding_Embedding1[0][0]
__________________________________________________________________________________________________
embedding_Embedding2 (Embedding [null,10,64] 832 input2[0][0]
__________________________________________________________________________________________________
encoderLast (GetLastTimestepLay [null,64] 0 lstm_LSTM1[0][0]
__________________________________________________________________________________________________
lstm_LSTM2 (LSTM) [null,10,64] 33024 embedding_Embedding2[0][0]
encoderLast[0][0]
encoderLast[0][0]
__________________________________________________________________________________________________
dot_Dot1 (Dot) [null,10,12] 0 lstm_LSTM2[0][0]
lstm_LSTM1[0][0]
__________________________________________________________________________________________________
attention (Activation) [null,10,12] 0 dot_Dot1[0][0]
__________________________________________________________________________________________________
context (Dot) [null,10,64] 0 attention[0][0]
lstm_LSTM1[0][0]
__________________________________________________________________________________________________
concatenate_Concatenate1 (Conca [null,10,128] 0 context[0][0]
lstm_LSTM2[0][0]
__________________________________________________________________________________________________
time_distributed_TimeDistribute [null,10,64] 8256 concatenate_Concatenate1[0][0]
__________________________________________________________________________________________________
time_distributed_TimeDistribute [null,10,13] 845 time_distributed_TimeDistributed1
==================================================================================================
Total params: 78221
Trainable params: 78221
Non-trainable params: 0
__________________________________________________________________________________________________
首先,要让 tf.layers.dot
起作用,两个输入的形状应该相同。
要执行串联,您可以使用 tf.concat([h_t, h_s])
。新形状将取决于执行串联的轴。
假设 h_t
和 h_s
的形状都是 [a, b]
,如果在轴 0 上进行串联,那么新的形状将是 [2a, b]
如果在轴 1 上完成,则生成的形状将为 [a, 2b]
然后您可以将 tf.tanh
应用于输入或创建一个自定义层来为您完成。
更新:
由于 tf.layers.dot 是在碰巧在第二个轴(轴 = 1)上不匹配的 3d 数据上执行的,因此只能在该轴上进行串联,结果形状将是 [ 1, 10 + 12, 64 ]
我一直在努力理解注意力机制是如何工作的。当前查看 tfjs-examples/date-conversion-attention example. I've found out that in the example the dot product alignment score
(from Effective Approaches to Attention-based Neural Machine Translation
) 正在使用中。
所以这个表达式
let attention = tf.layers.dot({axes: [2, 2]}).apply([decoder, encoder]);
在code。
解码器 (h_t
) 的形状为 [10,64],编码器 (h_s
) 的形状为 [12,64],因此结果的形状为 [10,12]。到目前为止一切顺利。
现在我正在尝试实现 concat alignment score
,它看起来像这样
所以首先要做的是连接 h_t
和 h_s
。但是,它们的形状不同,所以我不知道如何进行。我应该以某种方式重塑张量吗?如果可以,形状是什么?
我一直在谷歌搜索以了解其他人是如何做到这一点的,并找到了 this。
#For concat scoring, decoder hidden state and encoder outputs are concatenated first
out = torch.tanh(self.fc(decoder_hidden+encoder_outputs))
但这似乎不对,因为它们对值求和而不是串联。
如有任何指导,我们将不胜感激。
更新 这是模型摘要:
__________________________________________________________________________________________________
Layer (type) Output shape Param # Receives inputs
==================================================================================================
input1 (InputLayer) [null,12] 0
__________________________________________________________________________________________________
embedding_Embedding1 (Embedding [null,12,64] 2240 input1[0][0]
__________________________________________________________________________________________________
input2 (InputLayer) [null,10] 0
__________________________________________________________________________________________________
lstm_LSTM1 (LSTM) [null,12,64] 33024 embedding_Embedding1[0][0]
__________________________________________________________________________________________________
embedding_Embedding2 (Embedding [null,10,64] 832 input2[0][0]
__________________________________________________________________________________________________
encoderLast (GetLastTimestepLay [null,64] 0 lstm_LSTM1[0][0]
__________________________________________________________________________________________________
lstm_LSTM2 (LSTM) [null,10,64] 33024 embedding_Embedding2[0][0]
encoderLast[0][0]
encoderLast[0][0]
__________________________________________________________________________________________________
dot_Dot1 (Dot) [null,10,12] 0 lstm_LSTM2[0][0]
lstm_LSTM1[0][0]
__________________________________________________________________________________________________
attention (Activation) [null,10,12] 0 dot_Dot1[0][0]
__________________________________________________________________________________________________
context (Dot) [null,10,64] 0 attention[0][0]
lstm_LSTM1[0][0]
__________________________________________________________________________________________________
concatenate_Concatenate1 (Conca [null,10,128] 0 context[0][0]
lstm_LSTM2[0][0]
__________________________________________________________________________________________________
time_distributed_TimeDistribute [null,10,64] 8256 concatenate_Concatenate1[0][0]
__________________________________________________________________________________________________
time_distributed_TimeDistribute [null,10,13] 845 time_distributed_TimeDistributed1
==================================================================================================
Total params: 78221
Trainable params: 78221
Non-trainable params: 0
__________________________________________________________________________________________________
首先,要让 tf.layers.dot
起作用,两个输入的形状应该相同。
要执行串联,您可以使用 tf.concat([h_t, h_s])
。新形状将取决于执行串联的轴。
假设 h_t
和 h_s
的形状都是 [a, b]
,如果在轴 0 上进行串联,那么新的形状将是 [2a, b]
如果在轴 1 上完成,则生成的形状将为 [a, 2b]
然后您可以将 tf.tanh
应用于输入或创建一个自定义层来为您完成。
更新:
由于 tf.layers.dot 是在碰巧在第二个轴(轴 = 1)上不匹配的 3d 数据上执行的,因此只能在该轴上进行串联,结果形状将是 [ 1, 10 + 12, 64 ]