A Module that takes two matrices as input and returns a matrix of attentions.

class allennlp.modules.matrix_attention.MatrixAttention(similarity_function: allennlp.modules.similarity_functions.similarity_function.SimilarityFunction = None) → None[source]

Bases: torch.nn.modules.module.Module

This Module takes two matrices as input and returns a matrix of attentions.

We compute the similarity between each row in each matrix and return unnormalized similarity scores. Because these scores are unnormalized, we don’t take a mask as input; it’s up to the caller to deal with masking properly when this output is used.

By default similarity is computed with a dot product, but you can alternatively use a parameterized similarity function if you wish.

This is largely similar to using TimeDistributed(Attention), except the result is unnormalized. You should use this instead of TimeDistributed(Attention) if you want to compute multiple normalizations of the attention matrix.

  • matrix_1: (batch_size, num_rows_1, embedding_dim)
  • matrix_2: (batch_size, num_rows_2, embedding_dim)
  • (batch_size, num_rows_1, num_rows_2)

similarity_function: ``SimilarityFunction``, optional (default=``DotProductSimilarity``)

The similarity function to use when computing the attention.

forward(matrix_1: torch.FloatTensor, matrix_2: torch.FloatTensor) → torch.FloatTensor[source]

Defines the computation performed at every call.

Should be overriden by all subclasses.

classmethod from_params(params: allennlp.common.params.Params) → allennlp.modules.matrix_attention.MatrixAttention[source]