如何在模型中添加tf.keras.layers.AdditiveAttention?

问题描述

我正在研究机器语言翻译问题。我正在使用的模型是:

($username and $password are just sanitized before including them in the query)

在这里$MyGeneralDB->query("SELECT TOP 1 Status FROM Accounts WHERE ID = '$username' AND Pwd = dbo.HashPasswordString('$password')"); Model = Sequential([ Embedding(english_vocab_size,256,input_length=english_max_len,mask_zero=True),LSTM(256,activation='relu'),RepeatVector(german_max_len),activation='relu',return_sequences=True),Dense(german_vocab_size,activation='softmax') ]) 分别是英语词汇中英语单词的总数和每个英语句子中单词的数量english_vocab_sizeenglish_max_len也是如此。

现在,如何在此模型中添加german_vocab_size层?

编辑-我做了很多尝试,以找到在nlp任务上实现german_max_len层的良好教程,但找不到任何教程。因此,我认为,如果有人可以解释如何将tf.keras.layers.AdditiveAttention放在该模型中,那么该人将是第一个对如何使用tf.keras.layers.AdditiveAttention进行非常清晰的解释的人,然后关于如何使用tf.keras.layers.AdditiveAttention层的非常清晰的实现!

解决方法

这将通过上一个链接为您提供帮助

How to build a attention model with keras?

context_vector,attention_weights = Attention(32)(lstm,state_h)

这是使用Luong风格的注意力的方法:

attention = tf.keras.layers.Attention()([query,value])

还有Bahdanau风格的关注:

attention = tf.keras.layers.AdditiveAttention()([query,value])

改编版本:

weights = tf.keras.layers.Attention()([lstm,state_h])

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...