(self, tensors)
return tf.matmul(attention_weights, right)
Attention layer.
Attention layer.
Attention layer .
def call(self, tensors):
    """Attention layer."""
    left, right = tensors

    len_left = left.shape[1]
    len_right = right.shape[1]
    tensor_left = tf.expand_dims(left, axis=2)
    tensor_right = tf.expand_dims(right, axis=1)
    tensor_left = tf.tile(tensor_left, [1, 1, len_right, 1])
    tensor_right = tf.tile(tensor_right, [1, len_left, 1, 1])
    tensor_merged = tf.concat([tensor_left, tensor_right], axis=-1)
    middle_output = self.middle_layer(tensor_merged)
    attn_scores = self.attn(middle_output)
    attn_scores = tf.squeeze(attn_scores, axis=3)
    exp_attn_scores = tf.exp(attn_scores -
                             tf.reduce_max(attn_scores, axis=-1, keepdims=True))
    exp_sum = tf.reduce_sum(exp_attn_scores, axis=-1, keepdims=True)
    attention_weights = exp_attn_scores / exp_sum
    return tf.matmul(attention_weights, right)
def call ( self , tensors ) : left , right = tensors len_left = left . shape [ 1 ] len_right = right . shape [ 1 ] tensor_left = tf . expand_dims ( left , axis = 2 ) tensor_right = tf . expand_dims ( right , axis = 1 ) tensor_left = tf . tile ( tensor_left , [ 1 , 1 , len_right , 1 ] ) tensor_right = tf . tile ( tensor_right , [ 1 , len_left , 1 , 1 ] ) tensor_merged = tf . concat ( [ tensor_left , tensor_right ] , axis = - 1 ) middle_output = self . middle_layer ( tensor_merged ) attn_scores = self . attn ( middle_output ) attn_scores = tf . squeeze ( attn_scores , axis = 3 ) exp_attn_scores = tf . exp ( attn_scores - tf . reduce_max ( attn_scores , axis = - 1 , keepdims = True ) ) exp_sum = tf . reduce_sum ( exp_attn_scores , axis = - 1 , keepdims = True ) attention_weights = exp_attn_scores / exp_sum return tf . matmul ( attention_weights , right )