如何从关注类中获取注意力量

pra*_*jan 5 python keras attention-model

class AttLayer(Layer):
    def __init__(self, **kwargs):
        self.init = initializations.get('normal')
        #self.input_spec = [InputSpec(ndim=3)]
        super(AttLayer, self).__init__(** kwargs)

    def build(self, input_shape):
        assert len(input_shape)==3
        #self.W = self.init((input_shape[-1],1))
        self.W = self.init((input_shape[-1],))
        #self.input_spec = [InputSpec(shape=input_shape)]
        self.trainable_weights = [self.W]
        super(AttLayer, self).build(input_shape)  # be sure you call this somewhere!

    def call(self, x, mask=None):
        eij = K.tanh(K.dot(x, self.W))

        ai = K.exp(eij)
        weights = ai/K.sum(ai, axis=1).dimshuffle(0,'x')

        weighted_input = x*weights.dimshuffle(0,1,'x')
        return weighted_input.sum(axis=1)

    def get_output_shape_for(self, input_shape):
        return (input_shape[0], input_shape[-1])
Run Code Online (Sandbox Code Playgroud)

我有兴趣从课堂上获得注意力,而不是自我.W(图层的重量).有人可以告诉我,我该怎么办?

这是我做的: MAX_SENT_LENGTH=40

图像有我写的代码

当我尝试将模型创建为: sentEncoder =Model(sentence_input,weighted_inp)

它会引发以下错误:

模型的输出张量必须是Keras张量.找到:Sum {axis = 1,acc_dtype = float64} .0