Я пытаюсь создать модель внимания с помощью Bi-LSTM, используя вложения слов.Я сталкивался с Как добавить механизм внимания в керасах? , https://github.com/philipperemy/keras-attention-mechanism/blob/master/attention_lstm.py и https://github.com/keras-team/keras/issues/4962.
Однако я не совсем понимаю реализацию Attention-Based Bidirectional Long Short-Term Memory Networks for Relation Classification
,Итак,
_input = Input(shape=[max_length], dtype='int32')
# get the embedding layer
embedded = Embedding(
input_dim=30000,
output_dim=300,
input_length=100,
trainable=False,
mask_zero=False
)(_input)
activations = Bidirectional(LSTM(20, return_sequences=True))(embedded)
# compute importance for each step
attention = Dense(1, activation='tanh')(activations)
Я запутался здесь относительно того, какое уравнение к чему из статьи.
attention = Flatten()(attention)
attention = Activation('softmax')(attention)
Что будет делать RepeatVector?
attention = RepeatVector(20)(attention)
attention = Permute([2, 1])(attention)
sent_representation = merge([activations, attention], mode='mul')
Теперь, опять же, я не уверен, почему эта строка здесь.
sent_representation = Lambda(lambda xin: K.sum(xin, axis=-2), output_shape=(units,))(sent_representation)
Так как у меня есть два класса, у меня будет окончательный softmax как:
probabilities = Dense(2, activation='softmax')(sent_representation)