如何在keras中添加注意力机制?

如何在keras中添加注意力机制?,第1张

如何在keras中添加注意力机制?

如果您想在时间维度上关注,那么这段代码对我来说似乎是正确的:

activations = LSTM(units, return_sequences=True)(embedded)# compute importance for each stepattention = Dense(1, activation='tanh')(activations)attention = Flatten()(attention)attention = Activation('softmax')(attention)attention = RepeatVector(units)(attention)attention = Permute([2, 1])(attention)sent_representation = merge([activations, attention], mode='mul')

您已经计算出shape的注意力向量

(batch_size, max_length)

attention = Activation('softmax')(attention)

我以前从未看过这段代码,所以我不能说这段代码是否正确:

K.sum(xin, axis=-2)


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5644618.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存