如何在 python 中将注意力层添加到构建为顺序 keras 模型的 LSTM 自动编码器?

How to add an attention layer to LSTM autoencoder built as sequential keras model in python?

所以我想为序列数据构建一个自动编码器模型。我已经开始在 python 中构建顺序 keras 模型,现在我想在中间添加一个注意力层,但不知道如何处理。到目前为止我的模型:

from keras.layers import LSTM, TimeDistributed, RepeatVector, Layer
from keras.models import Sequential
import keras.backend as K

model = Sequential()
model.add(LSTM(20, activation="relu", input_shape=(time_steps,n_features), return_sequences=False))
model.add(RepeatVector(time_steps, name="bottleneck_output"))
model.add(LSTM(30, activation="relu", return_sequences=True))
model.add(TimeDistributed(Dense(n_features)))

model.compile(optimizer="adam", loss="mae")

到目前为止,我已经尝试添加从 here

复制的注意力函数
class attention(Layer):
    def __init__(self,**kwargs):
        super(attention,self).__init__(**kwargs)

    def build(self,input_shape):
        self.W=self.add_weight(name="att_weight",shape=(input_shape[-1],1),initializer="normal")
        self.b=self.add_weight(name="att_bias",shape=(input_shape[1],1),initializer="zeros")        
        super(attention, self).build(input_shape)

    def call(self,x):
        et=K.squeeze(K.tanh(K.dot(x,self.W)+self.b),axis=-1)
        at=K.softmax(et)
        at=K.expand_dims(at,axis=-1)
        output=x*at
        return K.sum(output,axis=1)

    def compute_output_shape(self,input_shape):
        return (input_shape[0],input_shape[-1])

    def get_config(self):
        return super(attention,self).get_config()

并将其添加到第一个 LSTM 之后,重复向量之前,即:

model = Sequential()
model.add(LSTM(20, activation="relu", input_shape=(time_steps,n_features), return_sequences=False))
model.add(attention()) # this is added
model.add(RepeatVector(time_steps, name="bottleneck_output"))
model.add(LSTM(30, activation="relu", return_sequences=True))
model.add(TimeDistributed(Dense(n_features)))

model.compile(optimizer="adam", loss="mae")

但是代码给出了错误,因为尺寸不知何故不合适,问题在于将 attention() 的输出放到重复向量中:

ValueError: Input 0 is incompatible with layer bottleneck_output: expected ndim=2, found ndim=1

... 但是根据 model.summary() 注意力层的输出维度是 (None, 20),这对于第一个 lstm_1 层也是一样的。该代码在没有注意力层的情况下工作。

我也希望得到一些解释,为什么解决方案就是问题的解决方案,我对 python 还很陌生,并且无法理解 class attention() 正在做什么。我只是复制它并尝试使用它,但它不起作用....

好的,我解决了。第一个 LSTM 层必须有 return_sequence = True。然后按原样工作。