How to add an attention mechanism in keras?

后端 未结 4 716
南方客
南方客 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条回答
  •  Happy的楠姐
    2020-12-02 09:00

    I think you can try the following code to add keras self-attention mechanism with LSTM network

        from keras_self_attention import SeqSelfAttention
    
        inputs = Input(shape=(length,))
        embedding = Embedding(vocab_size, EMBEDDING_DIM, weights=[embedding_matrix], input_length=MAX_SEQUENCE_LENGTH, trainable=False)(inputs)
        lstm = LSTM(num_lstm, input_shape=(X[train].shape[0], X[train].shape[1]), return_sequences=True)(embedding)
        attn = SeqSelfAttention(attention_activation='sigmoid')(lstm)
        Flat = Flatten()(attn)
        dense = Dense(32, activation='relu')(Flat)
        outputs = Dense(3, activation='sigmoid')(dense)
        model = Model(inputs=[inputs], outputs=outputs)
        model.compile(loss='binary_crossentropy', optimizer=Adam(0.001), metrics=['accuracy'])
        model.fit(X_train, y_train, epochs=10, batch_size=32,  validation_data=(X_val,y_val), shuffle=True)
    

提交回复
热议问题