def __init__(self, vocab: Vocabulary,
                 text_field_embedder: TextFieldEmbedder,
                 gate_sent_encoder: Seq2SeqEncoder,
                 gate_self_attention_layer: Seq2SeqEncoder,
                 bert_projection: FeedForward,
                 span_gate: Seq2SeqEncoder,
                 dropout: float = 0.2,
                 output_att_scores: bool = True,
                 regularizer: Optional[RegularizerApplicator] = None) -> None:

        super(PTNChainBidirectionalAttentionFlow, self).__init__(vocab, regularizer)

        self._text_field_embedder = text_field_embedder

        self._dropout = torch.nn.Dropout(p=dropout)

        self._output_att_scores = output_att_scores

        self._span_gate = span_gate

        self._bert_projection = bert_projection

        #self._gate_sent_encoder = gate_sent_encoder
        self._gate_self_attention_layer = gate_self_attention_layer
        self._gate_sent_encoder = None
        self._gate_self_attention_layer = None

        self._f1_metrics = AttF1Measure(0.5, top_k=False)

        self._loss_trackers = {'loss': Average(),
                               'rl_loss': Average()}

        self.evd_sup_acc_metric = ChainAccuracy()
        self.evd_ans_metric = Average()
        self.evd_beam_ans_metric = Average()
예제 #2
0
    def __init__(self,
                 vocab: Vocabulary,
                 text_field_embedder: TextFieldEmbedder,
                 dropout: float = 0.2,
                 regularizer: Optional[RegularizerApplicator] = None) -> None:

        super(BidirectionalAttentionFlow, self).__init__(vocab, regularizer)

        self._text_field_embedder = text_field_embedder

        encoding_dim = text_field_embedder.get_output_dim()

        self._dropout = torch.nn.Dropout(p=dropout)

        self._squad_metrics = SquadEmAndF1()

        self._categorical_acc = CategoricalAccuracy()

        self._coref_f1_metric = AttF1Measure(0.1)

        self.linear_start = nn.Linear(encoding_dim, 1)

        self.linear_end = nn.Linear(encoding_dim, 1)

        self.linear_type = nn.Linear(encoding_dim, 3)

        self._loss_trackers = {'loss': Average()}
예제 #3
0
def calc_evd_f1(pred_labels, gold_labels):
    evd_metric = AttF1Measure(0.5) # We just use 0.5 as the TH since pred_labels should only contain 0 and 1
    T_P, N_P, N_T = evd_metric(torch.tensor(pred_labels).float(), torch.tensor(gold_labels).float())
    precision = float(T_P) / float(N_P + 1e-13)
    recall = float(T_P) / float(N_T + 1e-13)
    f1 = 2. * ((precision * recall) / (precision + recall + 1e-13))
    return precision, recall, f1
    def __init__(self,
                 vocab: Vocabulary,
                 text_field_embedder: TextFieldEmbedder,
                 phrase_layer: Seq2SeqEncoder,
                 modeling_layer: Seq2SeqEncoder,
                 span_start_encoder: Seq2SeqEncoder,
                 span_end_encoder: Seq2SeqEncoder,
                 dropout: float = 0.2,
                 regularizer: Optional[RegularizerApplicator] = None) -> None:

        super(BidirectionalAttentionFlow, self).__init__(vocab, regularizer)

        self._text_field_embedder = text_field_embedder

        self._phrase_layer = phrase_layer

        encoding_dim = phrase_layer.get_output_dim()

        self._modeling_layer = modeling_layer

        self._dropout = torch.nn.Dropout(p=dropout)

        self._squad_metrics = SquadEmAndF1()

        self._f1_metrics = F1Measure(1)

        self.linear_1 = nn.Sequential(
            nn.Linear(encoding_dim * 4, encoding_dim), nn.ReLU())
        self.linear_2 = nn.Sequential(
            nn.Linear(encoding_dim * 4, encoding_dim), nn.ReLU())

        self.qc_att = BiAttention(encoding_dim, dropout)

        self._coref_f1_metric = AttF1Measure(0.1)

        self._span_start_encoder = span_start_encoder
        self._span_end_encoder = span_end_encoder

        self.linear_start = nn.Linear(encoding_dim, 1)

        self.linear_end = nn.Linear(encoding_dim, 1)

        self.linear_type = nn.Linear(encoding_dim * 3, 3)

        self._loss_trackers = {
            'loss': Average(),
            'start_loss': Average(),
            'end_loss': Average(),
            'type_loss': Average()
        }
    def __init__(self, vocab: Vocabulary,
                 text_field_embedder: TextFieldEmbedder,
                 phrase_layer: Seq2SeqEncoder,
                 phrase_layer_sp: Seq2SeqEncoder,
                 span_start_encoder: Seq2SeqEncoder,
                 span_end_encoder: Seq2SeqEncoder,
                 self_attention_layer: Seq2SeqEncoder,
                 gate_sent_encoder: Seq2SeqEncoder,
                 gate_self_attention_layer: Seq2SeqEncoder,
                 type_encoder: Seq2SeqEncoder,
                 modeling_layer: Seq2SeqEncoder,
                 modeling_layer_sp: Seq2SeqEncoder,
                 span_gate: Seq2SeqEncoder,
                 dropout: float = 0.2,
                 output_att_scores: bool = True,
                 weights_file: str = None,
                 ft_reward: str = "ans",
                 regularizer: Optional[RegularizerApplicator] = None) -> None:

        super(FineTuneRLBidirectionalAttentionFlow, self).__init__(vocab, regularizer)

        self._text_field_embedder = text_field_embedder

        self._phrase_layer = phrase_layer
        self._phrase_layer_sp = phrase_layer_sp

        self._dropout = torch.nn.Dropout(p=dropout)

        self._modeling_layer = modeling_layer
        self._modeling_layer_sp = modeling_layer_sp

        self._span_start_encoder = span_start_encoder
        self._span_end_encoder = span_end_encoder
        self._type_encoder = type_encoder

        self._self_attention_layer = self_attention_layer

        self._output_att_scores = output_att_scores
        self._ft_reward = ft_reward.split('+')

        encoding_dim = span_start_encoder.get_output_dim()

        self._span_gate = span_gate
        self.qc_att = BiAttention(encoding_dim, 0.0)
        self.qc_att_sp = BiAttention(encoding_dim, dropout)
        self._gate_sent_encoder = gate_sent_encoder
        self._gate_self_attention_layer = gate_self_attention_layer

        self.linear_start = nn.Linear(encoding_dim, 1)

        self.linear_end = nn.Linear(encoding_dim, 1)

        self.linear_type = nn.Linear(encoding_dim * 3, 3)

        self._squad_metrics = SquadEmAndF1_RT()

        self._f1_metrics = AttF1Measure(0.5, top_k=False)

        self._loss_trackers = {'loss': Average(),
                               'rl_loss': Average()}

        if self._span_gate.evd_decoder._train_helper_type == 'teacher_forcing':
            raise ValueError("train_helper should not be teacher forcing during fine tune!")
        self.evd_sup_acc_metric = ChainAccuracy()
        self.evd_ans_metric = Average()
        self.evd_beam_ans_metric = Average()
        self.evd_beam2_ans_metric = Average()

        # load the weights
        if weights_file:
            model_state = torch.load(weights_file, map_location=util.device_mapping(0))
            self.load_state_dict(model_state)
            for p in self._text_field_embedder.parameters():
                p.requires_grad = False
예제 #6
0
    def __init__(self,
                 vocab: Vocabulary,
                 text_field_embedder: TextFieldEmbedder,
                 phrase_layer: Seq2SeqEncoder,
                 phrase_layer_sp: Seq2SeqEncoder,
                 span_start_encoder: Seq2SeqEncoder,
                 span_end_encoder: Seq2SeqEncoder,
                 self_attention_layer: Seq2SeqEncoder,
                 gate_sent_encoder: Seq2SeqEncoder,
                 gate_self_attention_layer: Seq2SeqEncoder,
                 type_encoder: Seq2SeqEncoder,
                 modeling_layer: Seq2SeqEncoder,
                 modeling_layer_sp: Seq2SeqEncoder,
                 span_gate: Seq2SeqEncoder,
                 dropout: float = 0.2,
                 output_att_scores: bool = True,
                 regularizer: Optional[RegularizerApplicator] = None) -> None:

        super(RLBidirectionalAttentionFlow, self).__init__(vocab, regularizer)

        self._text_field_embedder = text_field_embedder

        self._phrase_layer = phrase_layer
        self._phrase_layer_sp = phrase_layer_sp

        self._dropout = torch.nn.Dropout(p=dropout)

        self._modeling_layer = modeling_layer
        self._modeling_layer_sp = modeling_layer_sp

        self._span_start_encoder = span_start_encoder
        self._span_end_encoder = span_end_encoder
        self._type_encoder = type_encoder

        self._self_attention_layer = self_attention_layer

        self._output_att_scores = output_att_scores

        encoding_dim = span_start_encoder.get_output_dim()

        self._span_gate = span_gate
        self.qc_att = BiAttention(encoding_dim, dropout)
        self.qc_att_sp = BiAttention(encoding_dim, dropout)
        self._gate_sent_encoder = gate_sent_encoder
        self._gate_self_attention_layer = gate_self_attention_layer

        self.linear_start = nn.Linear(encoding_dim, 1)

        self.linear_end = nn.Linear(encoding_dim, 1)

        self.linear_type = nn.Linear(encoding_dim * 3, 3)

        self._squad_metrics = SquadEmAndF1_RT()

        self._f1_metrics = AttF1Measure(0.5, top_k=False)

        self._loss_trackers = {
            'loss': Average(),
            'start_loss': Average(),
            'end_loss': Average(),
            'type_loss': Average(),
            'rl_loss': Average()
        }

        self.evd_sup_acc_metric = ChainAccuracy()
        self.evd_ans_metric = Average()
        self.evd_beam_ans_metric = Average()