attention机制

attention包含两种,一种是空间上的,一种是时序上的
seq2seq:
encoder-decoder存在的问题:中间的存储向量的尺寸固定
通过attention机制解决
计算每个时刻的输出与最后时刻输出的相似度,再进行softmax得到一个向量

https://www.cnblogs.com/hiyoung/p/9860561.html

fw_cell = tf.contrib.rnn.BasicLSTMCell(n_hidden)##正向的LSTM
bw_cell = tf.contrib.rnn.BasicLSTMCell(n_hidden)##反向的LSTM

hidden_outputs, final_state = tf.nn.bidirectional_dynamic_rnn(fw_cell, bw_cell, cell_in, dtype=tf.float32)
# hidden_outputs[2,batch_size,n_step,hidden_size]
# final_state [2,2,batch_size,hidden_size]
output = tf.concat([hidden_outputs[0], hidden_outputs[1]], 2)  # 6,3,10   第一个step和第二个step 的隐藏层输出
final_state = tf.concat([final_state[1][0], final_state[1][1]], 1)  # 6,10    正反向最后时刻输出
final_state = tf.expand_dims(final_state, 2)
attn_weight = tf.squeeze(tf.matmul(output, final_state), 2)  # 6,3
soft_attn_weights = tf.nn.softmax(attn_weight, 1)  # 6,3
context = tf.matmul(tf.transpose(output, [0, 2, 1]), tf.expand_dims(soft_attn_weights, 2))  # 6,10,1
context = tf.squeeze(context, 2)  # 6,10
model = tf.matmul(context, out)  # batch n_class

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值