# allennlp.modules.matrix_attention¶

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.

Input:
• matrix_1: (batch_size, num_rows_1, embedding_dim)
• matrix_2: (batch_size, num_rows_2, embedding_dim)
Output:
• (batch_size, num_rows_1, num_rows_2)
Parameters: 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.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

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