def __init__(self, attention_units, memory, time_major=True):
self.attention_units = attention_units
self.enc_units = memory.get_shape()[-1].value
if time_major:
memory = tf.transpose(memory, perm=(1,0,2))
self.enc_length = tf.shape(memory)[1]
self.batch_size = tf.shape(memory)[0]
self.memory = tf.reshape(memory, (tf.shape(memory)[0], self.enc_length, 1, self.enc_units))
# pre-compute Uahj to minimize the computational cost
with tf.variable_scope('attention'):
Ua = tf.get_variable(name='Ua', shape=(1, 1, self.enc_units, self.attention_units),
initializer=gaussian_initializer(mean=0.0, std=0.001))
self.hidden_feats = tf.nn.conv2d(self.memory, Ua, [1,1,1,1], "SAME")
评论列表
文章目录