Exemplo n.º 1
0
    def from_params(cls, vocab: Vocabulary, params: Params) -> 'TreeAttention':
        embedder_params = params.pop("text_field_embedder")
        text_field_embedder = TextFieldEmbedder.from_params(vocab, embedder_params)

        premise_encoder_params = params.pop("premise_encoder", None)
        premise_encoder = Seq2SeqEncoder.from_params(premise_encoder_params)

        attention_similarity = SimilarityFunction.from_params(params.pop('attention_similarity'))
        phrase_probability = FeedForward.from_params(params.pop('phrase_probability'))
        edge_probability = FeedForward.from_params(params.pop('edge_probability'))

        edge_embedding = Embedding.from_params(vocab, params.pop('edge_embedding'))
        use_encoding_for_node = params.pop('use_encoding_for_node')
        ignore_edges = params.pop('ignore_edges', False)

        init_params = params.pop('initializer', None)
        initializer = (InitializerApplicator.from_params(init_params)
                       if init_params is not None
                       else InitializerApplicator())

        return cls(vocab=vocab,
                   text_field_embedder=text_field_embedder,
                   phrase_probability=phrase_probability,
                   edge_probability=edge_probability,
                   premise_encoder=premise_encoder,
                   edge_embedding=edge_embedding,
                   use_encoding_for_node=use_encoding_for_node,
                   attention_similarity=attention_similarity,
                   ignore_edges=ignore_edges,
                   initializer=initializer)
Exemplo n.º 2
0
 def from_params(cls, params: Params) -> 'MultiHeadedSimilarity':
     num_heads = params.pop_int("num_heads")
     tensor_1_dim = params.pop_int("tensor_1_dim")
     tensor_1_projected_dim = params.pop_int("tensor_1_projected_dim", None)
     tensor_2_dim = params.pop_int("tensor_2_dim", None)
     tensor_2_projected_dim = params.pop_int("tensor_1_projected_dim", None)
     internal_similarity = SimilarityFunction.from_params(params.pop("internal_similarity", {}))
     params.assert_empty(cls.__name__)
     return cls(num_heads=num_heads,
                tensor_1_dim=tensor_1_dim,
                tensor_1_projected_dim=tensor_1_projected_dim,
                tensor_2_dim=tensor_2_dim,
                tensor_2_projected_dim=tensor_2_projected_dim,
                internal_similarity=internal_similarity)
Exemplo n.º 3
0
 def from_params(cls, params: Params) -> 'MultiHeadedSimilarity':
     num_heads = params.pop_int("num_heads")
     tensor_1_dim = params.pop_int("tensor_1_dim")
     tensor_1_projected_dim = params.pop_int("tensor_1_projected_dim", None)
     tensor_2_dim = params.pop_int("tensor_2_dim", None)
     tensor_2_projected_dim = params.pop_int("tensor_1_projected_dim", None)
     internal_similarity = SimilarityFunction.from_params(
         params.pop("internal_similarity", {}))
     params.assert_empty(cls.__name__)
     return cls(num_heads=num_heads,
                tensor_1_dim=tensor_1_dim,
                tensor_1_projected_dim=tensor_1_projected_dim,
                tensor_2_dim=tensor_2_dim,
                tensor_2_projected_dim=tensor_2_projected_dim,
                internal_similarity=internal_similarity)
Exemplo n.º 4
0
        build the weight matrix 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 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.
    """
    def __init__(self, tensor_1_dim, tensor_2_dim, activation=None):
        super(BilinearSimilarity, self).__init__()
        self._weight_matrix = Parameter(
            torch.Tensor(tensor_1_dim, tensor_2_dim))
        self._bias = Parameter(torch.Tensor(1))
        self._activation = activation or Activation.by_name(u'linear')()
        self.reset_parameters()

    def reset_parameters(self):
        torch.nn.init.xavier_uniform_(self._weight_matrix)
        self._bias.data.fill_(0)

    #overrides
    def forward(self, tensor_1, tensor_2):
        intermediate = torch.matmul(tensor_1, self._weight_matrix)
        result = (intermediate * tensor_2).sum(dim=-1)
        return self._activation(result + self._bias)


BilinearSimilarity = SimilarityFunction.register(u"bilinear")(
    BilinearSimilarity)
Exemplo n.º 5
0
import torch

from allennlp.modules.similarity_functions.similarity_function import SimilarityFunction


class DotProductSimilarity(SimilarityFunction):
    u"""
    This similarity function simply computes the dot product between each pair of vectors, with an
    optional scaling to reduce the variance of the output elements.

    Parameters
    ----------
    scale_output : ``bool``, optional
        If ``True``, we will scale the output by ``math.sqrt(tensor.size(-1))``, to reduce the
        variance in the result.
    """
    def __init__(self, scale_output=False):
        super(DotProductSimilarity, self).__init__()
        self._scale_output = scale_output

    #overrides
    def forward(self, tensor_1, tensor_2):
        result = (tensor_1 * tensor_2).sum(dim=-1)
        if self._scale_output:
            result *= math.sqrt(tensor_1.size(-1))
        return result


DotProductSimilarity = SimilarityFunction.register(u"dot_product")(
    DotProductSimilarity)
Exemplo n.º 6
0
 def from_params(cls, params: Params) -> 'MatrixAttention':
     similarity_function = SimilarityFunction.from_params(params.pop("similarity_function", {}))
     params.assert_empty(cls.__name__)
     return cls(similarity_function=similarity_function)
Exemplo n.º 7
0
from __future__ import division
from __future__ import absolute_import
#overrides
import torch

from allennlp.modules.similarity_functions.similarity_function import SimilarityFunction


class CosineSimilarity(SimilarityFunction):
    u"""
    This similarity function simply computes the cosine similarity between each pair of vectors.  It has
    no parameters.
    """

    #overrides
    def forward(self, tensor_1, tensor_2):
        normalized_tensor_1 = tensor_1 / tensor_1.norm(dim=-1, keepdim=True)
        normalized_tensor_2 = tensor_2 / tensor_2.norm(dim=-1, keepdim=True)
        return (normalized_tensor_1 * normalized_tensor_2).sum(dim=-1)


CosineSimilarity = SimilarityFunction.register(u"cosine")(CosineSimilarity)
Exemplo n.º 8
0
                                     % (tensor_2_projected_dim, num_heads))
        self._tensor_1_projection = Parameter(torch.Tensor(tensor_1_dim, tensor_1_projected_dim))
        self._tensor_2_projection = Parameter(torch.Tensor(tensor_2_dim, tensor_2_projected_dim))
        self.reset_parameters()

    def reset_parameters(self):
        torch.nn.init.xavier_uniform_(self._tensor_1_projection)
        torch.nn.init.xavier_uniform_(self._tensor_2_projection)

    #overrides
    def forward(self, tensor_1              , tensor_2              )                :
        projected_tensor_1 = torch.matmul(tensor_1, self._tensor_1_projection)
        projected_tensor_2 = torch.matmul(tensor_2, self._tensor_2_projection)

        # Here we split the last dimension of the tensors from (..., projected_dim) to
        # (..., num_heads, projected_dim / num_heads), using tensor.view().
        last_dim_size = projected_tensor_1.size(-1) // self.num_heads
        new_shape = list(projected_tensor_1.size())[:-1] + [self.num_heads, last_dim_size]
        split_tensor_1 = projected_tensor_1.view(*new_shape)
        last_dim_size = projected_tensor_2.size(-1) // self.num_heads
        new_shape = list(projected_tensor_2.size())[:-1] + [self.num_heads, last_dim_size]
        split_tensor_2 = projected_tensor_2.view(*new_shape)

        # And then we pass this off to our internal similarity function.  Because the similarity
        # functions don't care what dimension their input has, and only look at the last dimension,
        # we don't need to do anything special here.  It will just compute similarity on the
        # projection dimension for each head, returning a tensor of shape (..., num_heads).
        return self._internal_similarity(split_tensor_1, split_tensor_2)

MultiHeadedSimilarity = SimilarityFunction.register(u"multiheaded")(MultiHeadedSimilarity)
Exemplo n.º 9
0
        activation.
    """
    def __init__(self,
                 tensor_1_dim,
                 tensor_2_dim,
                 combination=u'x,y',
                 activation=None):
        super(LinearSimilarity, self).__init__()
        self._combination = combination
        combined_dim = util.get_combined_dim(combination,
                                             [tensor_1_dim, tensor_2_dim])
        self._weight_vector = Parameter(torch.Tensor(combined_dim))
        self._bias = Parameter(torch.Tensor(1))
        self._activation = activation or Activation.by_name(u'linear')()
        self.reset_parameters()

    def reset_parameters(self):
        std = math.sqrt(6 / (self._weight_vector.size(0) + 1))
        self._weight_vector.data.uniform_(-std, std)
        self._bias.data.fill_(0)

    #overrides
    def forward(self, tensor_1, tensor_2):
        combined_tensors = util.combine_tensors(self._combination,
                                                [tensor_1, tensor_2])
        dot_product = torch.matmul(combined_tensors, self._weight_vector)
        return self._activation(dot_product + self._bias)


LinearSimilarity = SimilarityFunction.register(u"linear")(LinearSimilarity)