allennlp.modules.attention

An attention module that computes the similarity between an input vector and the rows of a matrix.

class allennlp.modules.attention.attention.Attention(normalize: bool = True) → None[source]

Bases: torch.nn.modules.module.Module, allennlp.common.registrable.Registrable

An Attention takes two inputs: a (batched) vector and a matrix, plus an optional mask on the rows of the matrix. We compute the similarity between the vector and each row in the matrix, and then (optionally) perform a softmax over rows using those computed similarities.

Inputs:

  • vector: shape (batch_size, embedding_dim)
  • matrix: shape (batch_size, num_rows, embedding_dim)
  • matrix_mask: shape (batch_size, num_rows), specifying which rows are just padding.

Output:

  • attention: shape (batch_size, num_rows).
Parameters:
normalize : bool, optional (default: True)

If true, we normalize the computed similarities with a softmax, to return a probability distribution for your attention. If false, this is just computing a similarity score.

forward(vector: torch.Tensor, matrix: torch.Tensor, matrix_mask: torch.Tensor = None) → torch.Tensor[source]

Defines the computation performed at every call.

Should be overridden 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.attention.attention.Attention[source]
class allennlp.modules.attention.bilinear_attention.BilinearAttention(vector_dim: int, matrix_dim: int, activation: allennlp.nn.activations.Activation = <function <lambda>.<locals>.<lambda>>, normalize: bool = True) → None[source]

Bases: allennlp.modules.attention.attention.Attention

Computes attention between a vector and a matrix using a bilinear attention function. This function has a matrix of weights W and a bias b, and the similarity between the vector x and the matrix y is computed as x^T W y + b.

Parameters:
vector_dim : int

The dimension of the vector, x, described above. This is x.size()[-1] - the length of the vector that will go into the similarity computation. We need this so we can build the weight matrix correctly.

matrix_dim : int

The dimension of the matrix, y, described above. This is y.size()[-1] - the length of the vector that will go into the similarity computation. We need this so we can build the weight matrix correctly.

activation : Activation, optional (default=linear (i.e. no activation))

An activation function applied after the x^T W y + b calculation. Default is no activation.

normalize : bool, optional (default: True)

If true, we normalize the computed similarities with a softmax, to return a probability distribution for your attention. If false, this is just computing a similarity score.

classmethod from_params(params: allennlp.common.params.Params)[source]
reset_parameters()[source]
class allennlp.modules.attention.cosine_attention.CosineAttention(normalize: bool = True) → None[source]

Bases: allennlp.modules.attention.attention.Attention

Computes attention between a vector and a matrix using cosine similarity.

classmethod from_params(params: allennlp.common.params.Params)[source]
class allennlp.modules.attention.dot_product_attention.DotProductAttention(normalize: bool = True) → None[source]

Bases: allennlp.modules.attention.attention.Attention

Computes attention between a vector and a matrix using dot product.

classmethod from_params(params: allennlp.common.params.Params)[source]
class allennlp.modules.attention.legacy_attention.LegacyAttention(similarity_function: allennlp.modules.similarity_functions.similarity_function.SimilarityFunction = None, normalize: bool = True) → None[source]

Bases: allennlp.modules.attention.attention.Attention

Computes attention between a vector and a matrix using a similarity function. This should be considered deprecated, as it consumes more memory than the specialized attention modules.

classmethod from_params(params: allennlp.common.params.Params) → allennlp.modules.attention.attention.Attention[source]
class allennlp.modules.attention.linear_attention.LinearAttention(tensor_1_dim: int, tensor_2_dim: int, combination: str = 'x, y', activation: allennlp.nn.activations.Activation = <function <lambda>.<locals>.<lambda>>, normalize: bool = True) → None[source]

Bases: allennlp.modules.attention.attention.Attention

This Attention module performs a dot product between a vector of weights and some combination of the two input vectors, followed by an (optional) activation function. The combination used is configurable.

If the two vectors are x and y, we allow the following kinds of combinations: x, y, x*y, x+y, x-y, x/y, where each of those binary operations is performed elementwise. You can list as many combinations as you want, comma separated. For example, you might give x,y,x*y as the combination parameter to this class. The computed similarity function would then be w^T [x; y; x*y] + b, where w is a vector of weights, b is a bias parameter, and [;] is vector concatenation.

Note that if you want a bilinear similarity function with a diagonal weight matrix W, where the similarity function is computed as x * w * y + b (with w the diagonal of W), you can accomplish that with this class by using “x*y” for combination.

Parameters:
tensor_1_dim : int

The dimension of the first tensor, x, described above. This is x.size()[-1] - the length of the vector that will go into the similarity computation. We need this so we can build weight vectors correctly.

tensor_2_dim : int

The dimension of the second tensor, y, described above. This is y.size()[-1] - the length of the vector that will go into the similarity computation. We need this so we can build weight vectors correctly.

combination : str, optional (default=”x,y”)

Described above.

activation : Activation, optional (default=linear (i.e. no activation))

An activation function applied after the w^T * [x;y] + b calculation. Default is no activation.

classmethod from_params(params: allennlp.common.params.Params) → allennlp.modules.attention.attention.Attention[source]
reset_parameters()[source]