attention注意力机制的理解及简单实现(keras实现版本)

42 篇文章 12 订阅
18 篇文章 2 订阅

attention 的本质:其实就是一个加权求和。

问题:k 个d 维的特征向量h i (i=1,2,...,k) 整合这k 个特征向量的信息,变成一个向量h ∗  (仍是d 维)

解决方法 求平均值 -------(mean pooling) 加权平均,即(α i  为权重): h ∗ =∑ k i= α i h i      

   而attention所做的事情就是如何将α i  合理的算出来。      

step 1: 设计一个打分函数f ,针对每个h i  ,计算出一个score s i   。而s i  打分的依据,就是h i  与attention所关注的对象(其实就是一个向量)的相关程度,越相关,所得s i  值越大。      

step 2:对所得到的k 个score s i (i=1,2,...,k) ,通过一个softmax函数,得到最后的权重α i  ,即: α i =softmax(s i )

其代码简单实现版本:

from keras.layers.core import*
from keras.models import Sequential

input_dim = 32
hidden = 32

#The LSTM  model -  output_shape = (batch, step, hidden)
model1 = Sequential()
model1.add(LSTM(input_dim=input_dim, output_dim=hidden, input_length=step, return_sequences=True))

#The weight model  - actual output shape  = (batch, step)
# after reshape : output_shape = (batch, step,  hidden)
model2 = Sequential()
model2.add(Dense(input_dim=input_dim, output_dim=step))
model2.add(Activation('softmax')) # Learn a probability distribution over each  step.
#Reshape to match LSTM's output shape, so that we can do element-wise multiplication.
model2.add(RepeatVector(hidden))
model2.add(Permute(2, 1))

#The final model which gives the weighted sum:
model = Sequential()
model.add(Merge([model1, model2], 'mul'))  # Multiply each element with corresponding weight a[i][j][k] * b[i][j]
model.add(TimeDistributedMerge('sum')) # Sum the weighted elements.

model.compile(loss='mse', optimizer='sgd')

或者以下三行:(自己还未来得及真实复现,只是用别人的代码)

inputs = Input(shape=(input_dims,))
attention_probs = Dense(input_dims, activation='softmax', name='attention_probs')(inputs)
attention_mul = merge([inputs, attention_probs], output_shape=32, name='attention_mul', mode='mul')
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

就是求关注

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值