如何在Keras上的seq2seq模型中添加关注层

Osm*_*Osm 5 nlp deep-learning lstm keras attention-model

基于这个文章,我写了这个模式:

enc_in=Input(shape=(None,in_alphabet_len))
lstm=LSTM(lstm_dim,return_sequences=True,return_state=True,use_bias=False)
enc_out,h,c=lstm(enc_in)
dec_in=Input(shape=(None,in_alphabet_len))
decoder,_,_=LSTM(decoder_dim,return_sequences=True,return_state=True)(dec_in,initial_state=[h,c])
decoder=Dense(units=in_alphabet_len,activation='softmax')(decoder)
model=Model([enc_in,dec_in],decoder) 
Run Code Online (Sandbox Code Playgroud)

如何在解码器之前为该模型添加关注层?

El *_*ikh 0

您可以使用这个存储库

  1. 你需要 pip install keras-self-attention
  2. 导入层from keras_self_attention import SeqSelfAttention
    • 如果你想使用 tf.keras 而不是 keras,请在导入前添加以下内容os.environ['TF_KERAS'] = '1'
    • 确保您使用 keras 时省略之前的标志,因为这会导致不一致
  3. 由于您使用的是 keras 功能 API,

    enc_out, h, c = lstm()(enc_in)
    att = SeqSelfAttention()(enc_out)
    dec_in = Input(shape=(None, in_alphabet_len))(att)
    
    Run Code Online (Sandbox Code Playgroud)

    我希望这能回答您的问题以及未来的读者