注意层如何在喀拉拉邦实现?

时间:2019-07-11 10:27:58

标签: python keras deep-learning tf.keras attention-model

我正在学习关于注意力模型及其在喀拉拉邦的实现。在搜索过程中,我遇到了这两种方法firstsecond,使用它们我们可以在keras中创建关注层

# First method

class Attention(tf.keras.Model):
    def __init__(self, units):
        super(Attention, self).__init__()
        self.W1 = tf.keras.layers.Dense(units)
        self.W2 = tf.keras.layers.Dense(units)
        self.V = tf.keras.layers.Dense(1)

    def call(self, features, hidden):
        hidden_with_time_axis = tf.expand_dims(hidden, 1)
        score = tf.nn.tanh(self.W1(features) + self.W2(hidden_with_time_axis))
        attention_weights = tf.nn.softmax(self.V(score), axis=1)
        context_vector = attention_weights * features
        context_vector = tf.reduce_sum(context_vector, axis=1)

        return context_vector, attention_weights

# Second method

activations = LSTM(units, return_sequences=True)(embedded)

# compute importance for each step
attention = Dense(1, activation='tanh')(activations)
attention = Flatten()(attention)
attention = Activation('softmax')(attention)
attention = RepeatVector(units)(attention)
attention = Permute([2, 1])(attention)

sent_representation = merge([activations, attention], mode='mul')

math behind attention model

enter image description here

如果我们看一下第一种方法,那么它在某种程度上是注意力数学的直接实现,而第二种方法在互联网上的点击率却更高。

我真正的怀疑是第二种方法的这些方面

attention = RepeatVector(units)(attention)
attention = Permute([2, 1])(attention)
sent_representation = merge([activations, attention], mode='mul')
  • 哪个是值得关注的正确实施方式?
  • 第二种方法在RepeatVectorPermute层后面的直觉是什么?
  • 在第一种方法中,W1W2是权重;为什么在这里将稠密的层视为权重?
  • 为什么V值被视为单个单位密集层?
  • V(score)是做什么的?

1 个答案:

答案 0 :(得分:2)

  

哪种方法值得关注?

我建议以下内容:

https://github.com/tensorflow/models/blob/master/official/transformer/model/attention_layer.py#L24

上面的多标题Attention层实现了一个巧妙的技巧:调整矩阵的形状,以便将其成形为(batch_size,heads,time_steps,features / heads)而不是成形为(batch_size,head_time_steps,features / heads)和然后在“功能/标题”块上执行计算。

  

第二种方法中的RepeatVector和Permute层背后的直觉是什么?

您的代码不完整...您的代码中缺少矩阵乘法(您没有显示正在使用的Attention层)。这可能会修改结果的形状,并且此代码试图以某种方式恢复正确的形状。这可能不是最好的方法。

  

在第一种方法中,W1,W2是权重;为什么在这里将密集的层视为权重?

密集层是一组权重...您的问题有点含糊。

  

为什么将V值视为单个单位致密层?

这是一个非常奇怪的选择,与我对本文的阅读以及我所看到的实现都不相符。