MachineLP的Github(欢迎follow):https://github.com/MachineLP
下面是单层rnn+attention的代码,若考虑多层rnn请参考博主的:tf.contrib.rnn.static_rnn与tf.nn.dynamic_rnn区别
def attention(inputs, attention_size, time_major=False):
if isinstance(inputs, tuple):
# In case of Bi-RNN, concatenate the forward and the backward RNN outputs.
inputs = tf.concat(inputs, 2)
if time_major:
# (T,B,D) => (B,T,D)
inputs = tf