Search code examples
pythonkeraslstmattention-model

add an attention mechanism in kersa


I am currently building a model for multimodal emotion recognition i tried to add an attention mechanism usnig custom class below :

class Attention(tf.keras.layers.Layer):
    def __init__(self, **kwargs):
        super(Attention, self).__init__(**kwargs)

    def build(self, input_shape):
        self.We = self.add_weight(shape=(input_shape[-1], 1), initializer='random_normal', trainable=True)
        self.b = self.add_weight(shape=(input_shape[1],1), initializer='zeros', trainable=True)
        super(Attention, self).build(input_shape)

    def call(self, x):
        q = tf.nn.tanh(tf.linalg.matmul(x, self.We) + self.b)
        a = tf.nn.softmax(q, axis=1)
        return tf.reduce_sum(a * x, axis=1)

this class it using in lstm model :

self.features_audio_dim = self.train_x_audio.shape[2] #1611 
audio_input  = Input(shape=(self.sequence_length, self.features_audio_dim), dtype='float32')
lstm_audio = LSTM(128, return_sequences=True,dropout=0.3,recurrent_dropout=0.2)(audio_input)
attention_audio = Attention()(lstm_audio)

I tried to fixe the error, but to no avail the problem is in attention layer

ValueError: Exception encountered when calling layer "attention_8" (type Attention).

Attention layer must be called on a list of inputs, namely [query, value] or [query, value, key]. Received: Tensor("Placeholder:0", shape=(None, 33, 128), dtype=float32).


Solution

  • Actually there is no need to implement Attention layer yourself anymore, you can directly use the Attention layer of keras.
    Check this out: https://keras.io/api/layers/attention_layers/attention/ I see you want to apply a self attention mechanism, in this case the query and value are the same, so you just need something like this:

    attention_audio = Attention()([lstm_audio, lstm_audio])
    

    For more information, just go to the link I gave you.