Ejemplo n.º 1
0
    def __init__(self,
                 vocab: Vocabulary,
                 embedder: TextFieldEmbedder,
                 question_encoder: Seq2SeqEncoder,
                 passage_encoder: Seq2SeqEncoder,
                 r: float = 0.8,
                 dropout: float = 0.1,
                 initializer: InitializerApplicator = InitializerApplicator(),
                 regularizer: Optional[RegularizerApplicator] = None) -> None:
        super(EvidenceExtraction, self).__init__(vocab, regularizer)

        self._embedder = embedder

        self._question_encoder = question_encoder
        self._passage_encoder = passage_encoder

        # size: 2H
        encoding_dim = question_encoder.get_output_dim()

        self._gru_cell = nn.GRUCell(2 * encoding_dim, encoding_dim)

        self._gate = nn.Linear(2 * encoding_dim, 2 * encoding_dim)

        self._match_layer_1 = nn.Linear(2 * encoding_dim, encoding_dim)
        self._match_layer_2 = nn.Linear(encoding_dim, 1)

        self._question_attention_for_passage = Attention(
            NonlinearSimilarity(encoding_dim))
        self._question_attention_for_question = Attention(
            NonlinearSimilarity(encoding_dim))
        self._passage_attention_for_answer = Attention(
            NonlinearSimilarity(encoding_dim), normalize=False)
        self._passage_attention_for_ranking = Attention(
            NonlinearSimilarity(encoding_dim))

        self._passage_self_attention = Attention(
            NonlinearSimilarity(encoding_dim))
        self._self_gru_cell = nn.GRUCell(2 * encoding_dim, encoding_dim)
        self._self_gate = nn.Linear(2 * encoding_dim, encoding_dim)

        self._answer_net = nn.GRUCell(encoding_dim, encoding_dim)

        self._v_r_Q = nn.Parameter(torch.rand(encoding_dim))
        self._r = r

        self._span_start_accuracy = CategoricalAccuracy()
        self._span_end_accuracy = CategoricalAccuracy()
        self._span_accuracy = BooleanAccuracy()
        self._squad_metrics = SquadEmAndF1()

        if dropout > 0:
            self._dropout = torch.nn.Dropout(p=dropout)
        else:
            self._dropout = lambda x: x

        initializer(self)
Ejemplo n.º 2
0
    def __init__(self,
                 vocab: Vocabulary,
                 embedder: TextFieldEmbedder,
                 question_encoder: Seq2SeqEncoder,
                 passage_encoder: Seq2SeqEncoder,
                 feed_forward: FeedForward,
                 dropout: float = 0.1,
                 num_decoding_steps: int = 40,
                 initializer: InitializerApplicator = InitializerApplicator(),
                 regularizer: Optional[RegularizerApplicator] = None) -> None:
        super(AnswerSynthesis, self).__init__(vocab, regularizer)
        self._vocab = vocab
        self._vocab_size = vocab.get_vocab_size()  # default: tokens
        self._num_decoding_steps = num_decoding_steps
        self._start_token_index = self._vocab.get_token_index(START_SYMBOL)
        self._end_token_index = self._vocab.get_token_index(END_SYMBOL)

        self._embedder = embedder
        self._question_encoder = question_encoder
        self._passage_encoder = passage_encoder

        encoding_dim = question_encoder.get_output_dim()
        embedding_dim = embedder.get_output_dim()

        self._span_start_embedding = nn.Embedding(2, 50)
        self._span_end_embedding = nn.Embedding(2, 50)
        self._gru_decoder = nn.GRUCell(encoding_dim + embedding_dim,
                                       encoding_dim)
        self._feed_forward = feed_forward

        self._attention = Attention(NonlinearSimilarity(encoding_dim))

        self._W_r = nn.Linear(embedding_dim, encoding_dim, bias=False)
        self._U_r = nn.Linear(encoding_dim, encoding_dim, bias=False)
        self._V_r = nn.Linear(encoding_dim, encoding_dim, bias=False)

        self._max_out = Maxout(encoding_dim,
                               num_layers=1,
                               output_dims=int(encoding_dim / 2),
                               pool_sizes=2)
        self._W_o = nn.Linear(int(encoding_dim / 2),
                              self._vocab_size,
                              bias=False)

        self._squad_metrics = SquadEmAndF1()
        #self._predict_acc = CategoricalAccuracy()

        if dropout > 0:
            self._dropout = torch.nn.Dropout(p=dropout)
        else:
            self._dropout = lambda x: x

        initializer(self)
        self._num_iter = 0