def __init__(
        self,
        # decoding_threshold: float = 0.05,
        objective: str = "softmax_with_null",
        uncertainty_factor: float = None,  # only used with softmax/null
        sparsemax_gamma: float = 1.0,  # only used with sparsemax
        skip_metrics_during_training: bool = False,
        prob_metric: BinaryF1 = BinaryF1(),
        score_metric: BinaryF1 = BinaryF1([-1, 0, 1, 2, 4, 6, 8, 12, 16])):
        super(SetDensityClassifier, self).__init__()

        if objective not in objective_values:
            raise ConfigurationError(
                "SetDensityClassifier objective was %s, must be one of %s" %
                (objective, objective_values))
        self._objective = objective

        self._uncertainty_factor = uncertainty_factor
        self._sparsemax_gamma = sparsemax_gamma
        self._skip_metrics_during_training = skip_metrics_during_training

        self._gold_recall_metric = MomentsMetric()
        self._prob_metric = prob_metric

        if objective == "softmax_with_null":
            self._score_metric = score_metric
            self._kl_divergence_metric = MomentsMetric()
            self._null_prob_metric = MomentsMetric()
        elif objective == "sparsemax":
            self._zero_metric = BinaryF1([0.0])
        else:
            raise ConfigurationError("should never happen")
    def __init__(self,
                 vocab: Vocabulary,
                 sentence_encoder: SentenceEncoder,
                 qarg_ffnn: FeedForward,
                 initializer: InitializerApplicator = InitializerApplicator(),
                 regularizer: Optional[RegularizerApplicator] = None):
        super(ClauseAndSpanToAnswerSlotModel,
              self).__init__(vocab, regularizer)
        self._sentence_encoder = sentence_encoder
        self._qarg_ffnn = qarg_ffnn

        self._clause_embedding = Embedding(
            vocab.get_vocab_size("abst-clause-labels"),
            self._qarg_ffnn.get_input_dim())
        self._span_extractor = EndpointSpanExtractor(
            input_dim=self._sentence_encoder.get_output_dim(),
            combination="x,y")
        self._span_hidden = TimeDistributed(
            Linear(2 * self._sentence_encoder.get_output_dim(),
                   self._qarg_ffnn.get_input_dim()))
        self._predicate_hidden = Linear(
            self._sentence_encoder.get_output_dim(),
            self._qarg_ffnn.get_input_dim())
        self._qarg_predictor = Linear(self._qarg_ffnn.get_output_dim(),
                                      self.vocab.get_vocab_size("qarg-labels"))
        self._metric = BinaryF1()
Beispiel #3
0
    def __init__(self,
                 vocab: Vocabulary,
                 text_field_embedder: TextFieldEmbedder,
                 stacked_encoder: Seq2SeqEncoder = None,
                 predicate_feature_dim: int = 0,
                 embedding_dropout: float = 0.0,
                 initializer: InitializerApplicator = InitializerApplicator(),
                 regularizer: Optional[RegularizerApplicator] = None):
        super(SentenceEncoder, self).__init__()
        self._text_field_embedder = text_field_embedder
        self._stacked_encoder = stacked_encoder
        self._predicate_feature_dim = predicate_feature_dim
        self._embedding_dropout = Dropout(p=embedding_dropout)

        if self._predicate_feature_dim > 0:
            self._predicate_feature_embedding = Embedding(
                2, predicate_feature_dim)

        if self._stacked_encoder is not None:
            embedding_dim_with_predicate_feature = self._text_field_embedder.get_output_dim(
            ) + self._predicate_feature_dim
            if embedding_dim_with_predicate_feature != self._stacked_encoder.get_input_dim(
            ):
                raise ConfigurationError(
                    ("Input dimension of sentence encoder (%s) must be " % self._stacked_encoder.get_input_dim()) + \
                    ("the sum of predicate feature dim and text embedding dim (%s)." % (embedding_dim_with_predicate_feature)))

        self._metric = BinaryF1()
Beispiel #4
0
    def __init__(self,
                 vocab: Vocabulary,
                 sentence_encoder: SentenceEncoder,
                 tan_ffnn: FeedForward,
                 inject_predicate: bool = False,
                 initializer: InitializerApplicator = InitializerApplicator(),
                 regularizer: Optional[RegularizerApplicator] = None):
        super(SpanToTanModel, self).__init__(vocab, regularizer)
        self._sentence_encoder = sentence_encoder
        self._tan_ffnn = tan_ffnn
        self._inject_predicate = inject_predicate

        self._span_extractor = EndpointSpanExtractor(
            input_dim=self._sentence_encoder.get_output_dim(),
            combination="x,y")
        prediction_input_dim = (3 * self._sentence_encoder.get_output_dim()
                                ) if self._inject_predicate else (
                                    2 *
                                    self._sentence_encoder.get_output_dim())
        self._tan_pred = TimeDistributed(
            Sequential(
                Linear(prediction_input_dim, self._tan_ffnn.get_input_dim()),
                ReLU(), self._tan_ffnn,
                Linear(self._tan_ffnn.get_output_dim(),
                       self.vocab.get_vocab_size("tan-string-labels"))))
        self._metric = BinaryF1()
Beispiel #5
0
    def __init__(self,
                 vocab: Vocabulary,
                 sentence_encoder: SentenceEncoder,
                 question_encoder: SlotSequenceEncoder,
                 span_selector: PruningSpanSelector,
                 classify_invalids: bool = True,
                 invalid_hidden_dim: int = 100,
                 initializer: InitializerApplicator = InitializerApplicator(),
                 regularizer: Optional[RegularizerApplicator] = None):
        super(QuestionToSpanModel, self).__init__(vocab, regularizer)
        self._sentence_encoder = sentence_encoder
        self._question_encoder = question_encoder
        self._span_selector = span_selector
        self._classify_invalids = classify_invalids
        self._invalid_hidden_dim = invalid_hidden_dim

        injected_embedding_dim = self._sentence_encoder.get_output_dim(
        ) + self._question_encoder.get_output_dim()
        extra_input_dim = self._span_selector.get_extra_input_dim()
        if injected_embedding_dim != extra_input_dim:
            raise ConfigurationError(
                "Sum of pred rep and question embedding dim %s did not match span selector injection dim of %s"
                % (injected_embedding_dim, extra_input_dim))

        if self._classify_invalids:
            self._invalid_pred = Sequential(
                Linear(extra_input_dim, self._invalid_hidden_dim), ReLU(),
                Linear(self._invalid_hidden_dim, 1))
            self._invalid_metric = BinaryF1()
    def __init__(self,
                 label_selection_policy="union",
                 skip_metrics_during_training: bool = False,
                 metric: BinaryF1 = BinaryF1()):
        super(SetBinaryClassifier, self).__init__()

        if label_selection_policy not in label_selection_policy_values:
            raise ConfigurationError(
                "Label selection policy must be one of: " +
                str(label_selection_policy_values))

        self._label_selection_policy = label_selection_policy
        self._skip_metrics_during_training = skip_metrics_during_training
        self._metric = metric
        self._kl_divergence_metric = MomentsMetric()
Beispiel #7
0
    def __init__(self, vocab: Vocabulary,
                 sentence_encoder: SentenceEncoder,
                 num_frames: int = 100,
                 initializer: InitializerApplicator = InitializerApplicator(),
                 regularizer: Optional[RegularizerApplicator] = None):
        super(ClauseFrameModel, self).__init__(vocab, regularizer)
        self._num_frames = num_frames
        self._num_clauses = vocab.get_vocab_size("abst-clause-labels")
        self._sentence_encoder = sentence_encoder
        self._frames_matrix = Parameter(data = torch.zeros([self._num_frames, self._num_clauses], dtype = torch.float32))
        self._frame_pred = Linear(self._sentence_encoder.get_output_dim(), self._num_frames)
        self._metric = BinaryF1()
        self._kl_divergence_metric = MomentsMetric()

        initializer(self)
Beispiel #8
0
 def __init__(self,
              vocab: Vocabulary,
              sentence_encoder: SentenceEncoder,
              label_name: str,
              label_namespace: str,
              classifier: SetClassifier,
              initializer: InitializerApplicator = InitializerApplicator(),
              regularizer: Optional[RegularizerApplicator] = None):
     super(MulticlassModel, self).__init__(vocab, regularizer)
     self._sentence_encoder = sentence_encoder
     self._label_name = label_name
     self._label_namespace = label_namespace
     self._classifier = classifier
     self._final_pred = Linear(
         self._sentence_encoder.get_output_dim(),
         self.vocab.get_vocab_size(self._label_namespace))
     self._metric = BinaryF1()