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

时间:2020-11-03 11:22:31

标签: python keras neural-network lstm attention-model

所以我想为序列数据建立一个自动编码器模型。我已经开始在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")

但是代码给出了错误,因为尺寸某种程度上不合适,问题在于将注意力()的输出重复向量:

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

....但根据model.summary(),注意层的输出维为(None, 20),这与第一lstm_1层相同。该代码无需关注层即可工作。

我也希望得到一些解释,解释为什么解决方案是解决问题的方法,我对python来说还很陌生,并且在理解类attention()在做什么时遇到了问题。我只是复制了它,并尝试使用它,它可能无法正常工作....

1 个答案:

答案 0 :(得分:0)

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