How to add an attention mechanism in keras?

后端 未结 4 718
南方客
南方客 2020-12-02 08:49

I\'m currently using this code that i get from one discussion on github Here\'s the code of the attention mechanism:

_input = Input(shape=[max_length], dtype         


        
4条回答
  •  难免孤独
    2020-12-02 09:08

    If you want to have an attention along the time dimension, then this part of your code seems correct to me:

    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')
    

    You've worked out the attention vector of shape (batch_size, max_length):

    attention = Activation('softmax')(attention)
    

    I've never seen this code before, so I can't say if this one is actually correct or not:

    K.sum(xin, axis=-2)
    

    Further reading (you might have a look):

    • https://github.com/philipperemy/keras-visualize-activations

    • https://github.com/philipperemy/keras-attention-mechanism

提交回复
热议问题