Exemple #1
0
        if span_start_logits.dim() != 2 or span_end_logits.dim() != 2:
            raise ValueError(
                u"Input shapes must be (batch_size, passage_length)")
        batch_size, passage_length = span_start_logits.size()
        max_span_log_prob = [-1e20] * batch_size
        span_start_argmax = [0] * batch_size
        best_word_span = span_start_logits.new_zeros((batch_size, 2),
                                                     dtype=torch.long)

        span_start_logits = span_start_logits.detach().cpu().numpy()
        span_end_logits = span_end_logits.detach().cpu().numpy()

        for b in range(batch_size):  # pylint: disable=invalid-name
            for j in range(passage_length):
                val1 = span_start_logits[b, span_start_argmax[b]]
                if val1 < span_start_logits[b, j]:
                    span_start_argmax[b] = j
                    val1 = span_start_logits[b, j]

                val2 = span_end_logits[b, j]

                if val1 + val2 > max_span_log_prob[b]:
                    best_word_span[b, 0] = span_start_argmax[b]
                    best_word_span[b, 1] = j
                    max_span_log_prob[b] = val1 + val2
        return best_word_span


BidirectionalAttentionFlow = Model.register(u"bidaf")(
    BidirectionalAttentionFlow)
                            action_strings, add_var_function=False)
                        self._has_logical_form(1.0)
                    except ParsingError:
                        self._has_logical_form(0.0)
                        logical_form = u'Error producing logical form'
                    if example_lisp_string:
                        self._denotation_accuracy(logical_form,
                                                  example_lisp_string[i])
                    outputs[u'best_action_sequence'].append(action_strings)
                    outputs[u'logical_form'].append(logical_form)
                    outputs[u'debug_info'].append(
                        best_final_states[i][0].debug_info[0])  # type: ignore
                    outputs[u'entities'].append(world[i].table_graph.entities)
                else:
                    outputs[u'logical_form'].append(u'')
                    self._has_logical_form(0.0)
                    if example_lisp_string:
                        self._denotation_accuracy(None, example_lisp_string[i])
            if metadata is not None:
                outputs[u"question_tokens"] = [
                    x[u"question_tokens"] for x in metadata
                ]
                outputs[u"original_table"] = [
                    x[u"original_table"] for x in metadata
                ]
            return outputs


WikiTablesMmlSemanticParser = Model.register(u"wikitables_mml_parser")(
    WikiTablesMmlSemanticParser)
Exemple #3
0
        if metadata is not None:
            output[u"words"] = [x[u"words"] for x in metadata]
        return output

    #overrides
    def decode(self, output_dict):
        u"""
        Converts the tag ids to the actual tags.
        ``output_dict["tags"]`` is a list of lists of tag_ids,
        so we use an ugly nested list comprehension.
        """
        output_dict[u"tags"] = [[
            self.vocab.get_token_from_index(tag,
                                            namespace=self.label_namespace)
            for tag in instance_tags
        ] for instance_tags in output_dict[u"tags"]]

        return output_dict

    #overrides
    def get_metrics(self, reset=False):
        metric_dict = self.span_metric.get_metric(reset=reset)
        if self._verbose_metrics:
            return metric_dict
        else:
            return dict((x, y) for x, y in list(metric_dict.items())
                        if u"overall" in x)


CrfTagger = Model.register(u"crf_tagger")(CrfTagger)
Exemple #4
0
            torch.cat(matching_vector_premise, dim=2))
        matching_vector_cat_hypothesis = self.dropout(
            torch.cat(matching_vector_hypothesis, dim=2))

        # aggregate the matching vectors
        aggregated_premise = self.dropout(
            self.aggregator(matching_vector_cat_premise, mask_premise))
        aggregated_hypothesis = self.dropout(
            self.aggregator(matching_vector_cat_hypothesis, mask_hypothesis))

        # the final forward layer
        logits = self.classifier_feedforward(
            torch.cat([aggregated_premise, aggregated_hypothesis], dim=-1))

        output_dict = {u'logits': logits}
        if label is not None:
            loss = self.loss(logits, label)
            for metric in list(self.metrics.values()):
                metric(logits, label)
            output_dict[u"loss"] = loss

        return output_dict

    #overrides
    def get_metrics(self, reset=False):
        return dict((metric_name, metric.get_metric(reset))
                    for metric_name, metric in list(self.metrics.items()))


BiMpm = Model.register(u"bimpm")(BiMpm)
Exemple #5
0
        u"""
        Dependency evaluation excludes words are punctuation.
        Here, we create a new mask to exclude word indices which
        have a "punctuation-like" part of speech tag.

        Parameters
        ----------
        mask : ``torch.LongTensor``, required.
            The original mask.
        pos_tags : ``torch.LongTensor``, required.
            The pos tags for the sequence.

        Returns
        -------
        A new mask, where any indices equal to labels
        we should be ignoring are masked.
        """
        new_mask = mask.detach()
        for label in self._pos_to_ignore:
            label_mask = pos_tags.eq(label).long()
            new_mask = new_mask * (1 - label_mask)
        return new_mask

    #overrides
    def get_metrics(self, reset=False):
        return self._attachment_scores.get_metric(reset)


BiaffineDependencyParser = Model.register(u"biaffine_parser")(
    BiaffineDependencyParser)
Exemple #6
0
    # The logic here requires a custom from_params.
    @classmethod
    def from_params(cls, vocab, params):  # type: ignore
        # pylint: disable=arguments-differ
        if vocab:
            raise ConfigurationError(u"vocab should be None")

        submodels = []
        paths = params.pop(u"submodels")
        for path in paths:
            submodels.append(load_archive(path).model)

        return cls(submodels=submodels)


BidafEnsemble = Model.register(u"bidaf-ensemble")(BidafEnsemble)


def ensemble(subresults):
    u"""
    Identifies the best prediction given the results from the submodels.

    Parameters
    ----------
    index : int
        The index within this index to ensemble

    subresults : List[Dict[str, torch.Tensor]]

    Returns
    -------
Exemple #7
0
        output_dict = {
            u"label_logits": label_logits,
            u"label_probs": label_probs,
            u"h2p_attention": h2p_attention,
            u"p2h_attention": p2h_attention
        }

        if label is not None:
            loss = self._loss(label_logits, label.long().view(-1))
            self._accuracy(label_logits, label)
            output_dict[u"loss"] = loss

        if metadata is not None:
            output_dict[u"premise_tokens"] = [
                x[u"premise_tokens"] for x in metadata
            ]
            output_dict[u"hypothesis_tokens"] = [
                x[u"hypothesis_tokens"] for x in metadata
            ]

        return output_dict

    def get_metrics(self, reset=False):
        return {
            u'accuracy': self._accuracy.get_metric(reset),
        }


DecomposableAttention = Model.register(u"decomposable_attention")(
    DecomposableAttention)
Exemple #8
0
        if u"activations" in output_layer_params:
            output_layer = FeedForward.from_params(output_layer_params)
        else:
            output_layer = Maxout.from_params(output_layer_params)

        elmo = params.pop(u"elmo", None)
        if elmo is not None:
            elmo = Elmo.from_params(elmo)
        use_input_elmo = params.pop_bool(u"use_input_elmo", False)
        use_integrator_output_elmo = params.pop_bool(u"use_integrator_output_elmo", False)

        initializer = InitializerApplicator.from_params(params.pop(u'initializer', []))
        regularizer = RegularizerApplicator.from_params(params.pop(u'regularizer', []))
        params.assert_empty(cls.__name__)

        return cls(vocab=vocab,
                   text_field_embedder=text_field_embedder,
                   embedding_dropout=embedding_dropout,
                   pre_encode_feedforward=pre_encode_feedforward,
                   encoder=encoder,
                   integrator=integrator,
                   integrator_dropout=integrator_dropout,
                   output_layer=output_layer,
                   elmo=elmo,
                   use_input_elmo=use_input_elmo,
                   use_integrator_output_elmo=use_integrator_output_elmo,
                   initializer=initializer,
                   regularizer=regularizer)

BiattentiveClassificationNetwork = Model.register(u"bcn")(BiattentiveClassificationNetwork)
        checklist_cost = self._checklist_cost_weight * checklist_cost
        action_history = state.action_history[0]
        batch_index = state.batch_indices[0]
        action_strings = [
            state.possible_actions[batch_index][i][0] for i in action_history
        ]
        logical_form = state.world[batch_index].get_logical_form(
            action_strings)
        lisp_string = state.example_lisp_string[batch_index]
        if self._denotation_accuracy.evaluate_logical_form(
                logical_form, lisp_string):
            cost = checklist_cost
        else:
            cost = checklist_cost + (
                1 - self._checklist_cost_weight) * denotation_cost
        return cost

    #overrides
    def get_metrics(self, reset=False):
        u"""
        The base class returns a dict with dpd accuracy, denotation accuracy, and logical form
        percentage metrics. We add the agenda coverage metric here.
        """
        metrics = super(WikiTablesErmSemanticParser, self).get_metrics(reset)
        metrics[u"agenda_coverage"] = self._agenda_coverage.get_metric(reset)
        return metrics


WikiTablesErmSemanticParser = Model.register(u"wikitables_erm_parser")(
    WikiTablesErmSemanticParser)
Exemple #10
0
        is learning. It may be inefficient to call it while training the model on real data.
        """
        if len(state.batch_indices) == 1 and state.is_finished():
            costs = [float(self._get_state_cost(state).detach().cpu().numpy())]
        else:
            costs = []
        model_scores = [float(score.detach().cpu().numpy()) for score in state.score]
        all_actions = state.possible_actions[0]
        action_sequences = [[self._get_action_string(all_actions[action]) for action in history]
                            for history in state.action_history]
        agenda_sequences = []
        all_agenda_indices = []
        for agenda, checklist_target in izip(state.terminal_actions, state.checklist_target):
            agenda_indices = []
            for action, is_wanted in izip(agenda, checklist_target):
                action_int = int(action.detach().cpu().numpy())
                is_wanted_int = int(is_wanted.detach().cpu().numpy())
                if is_wanted_int != 0:
                    agenda_indices.append(action_int)
            agenda_sequences.append([self._get_action_string(all_actions[action])
                                     for action in agenda_indices])
            all_agenda_indices.append(agenda_indices)
        return {u"agenda": agenda_sequences,
                u"agenda_indices": all_agenda_indices,
                u"history": action_sequences,
                u"history_indices": state.action_history,
                u"costs": costs,
                u"scores": model_scores}

NlvrCoverageSemanticParser = Model.register(u"nlvr_coverage_parser")(NlvrCoverageSemanticParser)
Exemple #11
0
            (batch_size, num_spans_to_keep, max_antecedents).
        antecedent_mention_scores: ``torch.FloatTensor``, required.
            Mention scores for every antecedent. Has shape
            (batch_size, num_spans_to_keep, max_antecedents).
        antecedent_log_mask: ``torch.FloatTensor``, required.
            The log of the mask for valid antecedents.

        Returns
        -------
        coreference_scores: ``torch.FloatTensor``
            A tensor of shape (batch_size, num_spans_to_keep, max_antecedents + 1),
            representing the unormalised score for each (span, antecedent) pair
            we considered.

        """
        # Shape: (batch_size, num_spans_to_keep, max_antecedents)
        antecedent_scores = self._antecedent_scorer(
                self._antecedent_feedforward(pairwise_embeddings)).squeeze(-1)
        antecedent_scores += top_span_mention_scores + antecedent_mention_scores
        antecedent_scores += antecedent_log_mask

        # Shape: (batch_size, num_spans_to_keep, 1)
        shape = [antecedent_scores.size(0), antecedent_scores.size(1), 1]
        dummy_scores = antecedent_scores.new_zeros(*shape)

        # Shape: (batch_size, num_spans_to_keep, max_antecedents + 1)
        coreference_scores = torch.cat([dummy_scores, antecedent_scores], -1)
        return coreference_scores

CoreferenceResolver = Model.register(u"coref")(CoreferenceResolver)
Exemple #12
0
        """
        all_labels = self.vocab.get_index_to_token_vocabulary(u"labels")
        num_labels = len(all_labels)
        transition_matrix = torch.zeros([num_labels, num_labels])

        for i, previous_label in list(all_labels.items()):
            for j, label in list(all_labels.items()):
                # I labels can only be preceded by themselves or
                # their corresponding B tag.
                if i != j and label[
                        0] == u'I' and not previous_label == u'B' + label[1:]:
                    transition_matrix[i, j] = float(u"-inf")
        return transition_matrix


SemanticRoleLabeler = Model.register(u"srl")(SemanticRoleLabeler)


def write_to_conll_eval_file(prediction_file, gold_file, verb_index, sentence,
                             prediction, gold_labels):
    u"""
    Prints predicate argument predictions and gold labels for a single verbal
    predicate in a sentence to two provided file references.

    Parameters
    ----------
    prediction_file : TextIO, required.
        A file reference to print predictions to.
    gold_file : TextIO, required.
        A file reference to print gold labels to.
    verb_index : Optional[int], required.
Exemple #13
0
from allennlp.training import Trainer
from allennlp.models.model import Model
from allennlp.common.params import Params
from allennlp.commands import fine_tune

from mtl.models.ABSASharedNetwork import ABSASharedNetwork
from mtl.dataset_readers.ABSADatasetReader import ABSADatasetReader
from mtl.dataset_readers.ACSADatasetReader import ACSADatasetReader

config = Params.from_file(os.path.join('checkpoint5', 'config.json'))
# print(config.get('model'))
# config.loading_from_archive = True

weights_path = os.path.join('checkpoint5', 'best.th')

Model.register('absa')
model = ABSASharedNetwork._load(config.duplicate(),
                                weights_file=weights_path,
                                serialization_dir='checkpoint5',
                                cuda_device=0)


def fine_tune(args):
    source_model = model.to(args.device)

    discriminator = nn.Sequential(nn.Linear(200, 50), nn.ReLU(),
                                  nn.Linear(50, 1)).to(args.device)

    source_reader = ACSADatasetReader(max_sequence_len=args.max_seq_len)
    target_reader = ABSADatasetReader(max_sequence_len=args.max_seq_len)
        u"""
        Does a simple position-wise argmax over each token, converts indices to string labels, and
        adds a ``"tags"`` key to the dictionary with the result.
        """
        all_predictions = output_dict[u'class_probabilities']
        all_predictions = all_predictions.cpu().data.numpy()
        if all_predictions.ndim == 3:
            predictions_list = [
                all_predictions[i] for i in range(all_predictions.shape[0])
            ]
        else:
            predictions_list = [all_predictions]
        all_tags = []
        for predictions in predictions_list:
            argmax_indices = numpy.argmax(predictions, axis=-1)
            tags = [
                self.vocab.get_token_from_index(x, namespace=u"labels")
                for x in argmax_indices
            ]
            all_tags.append(tags)
        output_dict[u'tags'] = all_tags
        return output_dict

    #overrides
    def get_metrics(self, reset=False):
        return dict((metric_name, metric.get_metric(reset))
                    for metric_name, metric in list(self.metrics.items()))


SimpleTagger = Model.register(u"simple_tagger")(SimpleTagger)
            # the left hand side is a constituent.
            for split in range(end - 1, start, -1):
                if (start, split) in spans_to_labels:
                    argmax_split = split
                    break

            left_trees = assemble_subtree(start, argmax_split)
            right_trees = assemble_subtree(argmax_split, end)
            children = left_trees + right_trees
            if labels is not None:
                while labels:
                    children = [Tree(labels.pop(), children)]
            return children

        tree = assemble_subtree(0, len(sentence))
        return tree[0]

    #overrides
    def get_metrics(self, reset=False):
        all_metrics = {}
        all_metrics[u"tag_accuracy"] = self.tag_accuracy.get_metric(
            reset=reset)
        if self._evalb_score is not None:
            evalb_metrics = self._evalb_score.get_metric(reset=reset)
            all_metrics.update(evalb_metrics)
        return all_metrics


SpanConstituencyParser = Model.register(u"constituency_parser")(
    SpanConstituencyParser)
Exemple #16
0
    #overrides
    def decode(self, output_dict):
        u"""
        This method overrides ``Model.decode``, which gets called after ``Model.forward``, at test
        time, to finalize predictions. The logic for the decoder part of the encoder-decoder lives
        within the ``forward`` method.

        This method trims the output predictions to the first end symbol, replaces indices with
        corresponding tokens, and adds a field called ``predicted_tokens`` to the ``output_dict``.
        """
        predicted_indices = output_dict[u"predictions"]
        if not isinstance(predicted_indices, numpy.ndarray):
            predicted_indices = predicted_indices.detach().cpu().numpy()
        all_predicted_tokens = []
        for indices in predicted_indices:
            indices = list(indices)
            # Collect indices till the first end_symbol
            if self._end_index in indices:
                indices = indices[:indices.index(self._end_index)]
            predicted_tokens = [
                self.vocab.get_token_from_index(
                    x, namespace=self._target_namespace) for x in indices
            ]
            all_predicted_tokens.append(predicted_tokens)
        output_dict[u"predicted_tokens"] = all_predicted_tokens
        return output_dict


SimpleSeq2Seq = Model.register(u"simple_seq2seq")(SimpleSeq2Seq)
Exemple #17
0
        classes = []
        for prediction in predictions_list:
            label_idx = prediction.argmax(dim=-1).item()
            label_str = self.vocab.get_index_to_token_vocabulary(
                self._label_namespace).get(label_idx, str(label_idx))
            classes.append(label_str)
        output_dict["label"] = classes
        tokens = []
        for instance_tokens in output_dict["token_ids"]:
            tokens.append([
                self.vocab.get_token_from_index(token_id.item(),
                                                namespace=self._namespace)
                for token_id in instance_tokens
            ])
        output_dict["tokens"] = tokens
        return output_dict

    def load_state_dict(self,
                        state_dict: Union[Dict[str, torch.Tensor],
                                          Dict[str, torch.Tensor]],
                        strict: bool = False):
        return super().load_state_dict(state_dict, strict=strict)

    default_predictor = "glue-numeric"


Model.register("arp_classifier",
               constructor="from_partial_objects")(ArpClassifier)
Model.register("warp_classifier",
               constructor="from_partial_objects")(ArpClassifier)
Exemple #18
0
    def _update_metrics(self, action_strings, worlds, label_strings):
        # TODO(pradeep): Move this to the base class.
        # TODO(pradeep): Using only the best decoded sequence. Define metrics for top-k sequences?
        batch_size = len(worlds)
        for i in range(batch_size):
            instance_action_strings = action_strings[i]
            sequence_is_correct = [False]
            if instance_action_strings:
                instance_label_strings = label_strings[i]
                instance_worlds = worlds[i]
                # Taking only the best sequence.
                sequence_is_correct = self._check_denotation(
                    instance_action_strings[0], instance_label_strings,
                    instance_worlds)
            for correct_in_world in sequence_is_correct:
                self._denotation_accuracy(1 if correct_in_world else 0)
            self._consistency(1 if all(sequence_is_correct) else 0)

    #overrides
    def get_metrics(self, reset=False):
        return {
            u'denotation_accuracy':
            self._denotation_accuracy.get_metric(reset),
            u'consistency': self._consistency.get_metric(reset)
        }


NlvrDirectSemanticParser = Model.register(u"nlvr_direct_parser")(
    NlvrDirectSemanticParser)