简体   繁体   中英

Attention in Keras : How to add different attention mechanism in keras Dense layer?

I am new in Keras and I am trying to build a simple autoencoder in keras with attention layers :

Here what I tried :

data = Input(shape=(w,), dtype=np.float32, name='input_da')
noisy_data = Dropout(rate=0.2, name='drop1')(data)

encoded = Dense(256, activation='relu',
            name='encoded1', **kwargs)(noisy_data)
encoded = Lambda(mvn, name='mvn1')(encoded)

encoded = Dense(128, activation='relu',
            name='encoded2', **kwargs)(encoded)

encoded = Lambda(mvn, name='mvn2')(encoded)
encoded = Dropout(rate=0.5, name='drop2')(encoded)


encoder = Model([data], encoded)
encoded1 = encoder.get_layer('encoded1')
encoded2 = encoder.get_layer('encoded2')


decoded = DenseTied(256, tie_to=encoded2, transpose=True,
            activation='relu', name='decoded2')(encoded)
decoded = Lambda(mvn, name='new_mv')(decoded)


decoded = DenseTied(w, tie_to=encoded1, transpose=True,
            activation='linear', name='decoded1')(decoded)

And it looks like this:

_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
data (InputLayer)            (None, 2693)              0         
_________________________________________________________________
drop1 (Dropout)              (None, 2693)              0         
_________________________________________________________________
encoded1 (Dense)             (None, 256)               689664    
_________________________________________________________________
mvn1 (Lambda)                (None, 256)               0         
_________________________________________________________________
encoded2 (Dense)             (None, 128)               32896     
_________________________________________________________________
mvn2 (Lambda)                (None, 128)               0         
_________________________________________________________________
drop2 (Dropout)              (None, 128)               0         
_________________________________________________________________
decoded2 (DenseTied)         (None, 256)               256       
_________________________________________________________________
mvn3 (Lambda)                (None, 256)               0         
_________________________________________________________________
decoded1 (DenseTied)         (None, 2693)              2693      
=================================================================

Where I can add attention layer in this model? should I add after first encoded_output and before second encoded input?

encoded = Lambda(mvn, name='mvn1')(encoded)

    Here?

encoded = Dense(128, activation='relu',
            name='encoded2', **kwargs)(encoded)

also I was going though this beautiful lib :

https://github.com/CyberZHG/keras-self-attention

They have implemented various types of attention mechanisms but it's for sequential models. How I can add those attention in my model?

I tried with very simple attention :

encoded = Dense(256, activation='relu',
        name='encoded1', **kwargs)(noisy_data)


encoded = Lambda(mvn, name='mvn1')(encoded)

attention_probs = Dense(256, activation='softmax', name='attention_vec')(encoded)
attention_mul = multiply([encoded, attention_probs], name='attention_mul')
attention_mul = Dense(256)(attention_mul)

print(attention_mul.shape)

encoded = Dense(128, activation='relu',
        name='encoded2', **kwargs)(attention_mul)

is it at right place and can I add any other attention mechanism with this model?

I guess what you're doing is a correct way of adding attention, because attention in itself is nothing but can be visualized as weights of a dense layer. Also, I guess applying attention just after encoder is the right thing to do, as it will apply attention to the most "informative" part of the data distribution necessary for your task.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM