Esempio n. 1
0
    def inference_run(self):
        print("Test accuracy starting ... ")
        print(self.reader.typeOfReader)
        assert self.reader.typeOfReader == "inference"
        # assert self.reader.batch_size == 1
        self.reader.reset_test()
        numInstances = 0

        # For types: List contains numpy matrices of row_size = BatchSize
        predLabelScoresnumpymat_list = []
        # For EL : Lists contain one list per mention
        condProbs_list = []  # Crosswikis conditional priors
        widIdxs_list = []  # Candidate WID IDXs (First is true)
        contextProbs_list = []  # Predicted Entity prob using context

        while self.reader.epochs < 1:
            (left_batch, left_lengths, right_batch, right_lengths,
             coherence_batch, wid_idxs_batch,
             wid_cprobs_batch) = self.reader.next_test_batch()

            # Candidates for entity linking
            # feed_dict = {self.sampled_entity_ids: wid_idxs_batch,
            #              self.entity_priors: wid_cprobs_batch}
            feed_dict = {self.sampled_entity_ids: wid_idxs_batch}
            # Required Context
            if self.textcontext:
                if not self.pretrain_word_embed:
                    context_dict = {
                        self.left_batch: left_batch,
                        self.right_batch: right_batch,
                        self.left_lengths: left_lengths,
                        self.right_lengths: right_lengths
                    }
                else:
                    context_dict = {
                        self.left_context_embeddings: left_batch,
                        self.right_context_embeddings: right_batch,
                        self.left_lengths: left_lengths,
                        self.right_lengths: right_lengths
                    }
                feed_dict.update(context_dict)
            if self.coherence:
                coherence_dict = {
                    self.coherence_indices: coherence_batch[0],
                    self.coherence_values: coherence_batch[1],
                    self.coherence_matshape: coherence_batch[2]
                }
                feed_dict.update(coherence_dict)

            fetch_tensors = [
                self.labeling_model.label_probs,
                self.posterior_model.entity_posteriors
            ]

            fetches = self.sess.run(fetch_tensors, feed_dict=feed_dict)

            [label_sigms, context_probs] = fetches

            predLabelScoresnumpymat_list.append(label_sigms)
            condProbs_list.extend(wid_cprobs_batch)
            widIdxs_list.extend(wid_idxs_batch)
            contextProbs_list.extend(context_probs.tolist())
            numInstances += self.reader.batch_size

        print("Num of instances {}".format(numInstances))
        # print("Starting Type and EL Evaluations ... ")
        pred_TypeSetsList = evaluate_types.evaluate(
            predLabelScoresnumpymat_list, self.reader.idx2label)
        (evWTs, sortedContextWTs) = evaluate_inference.evaluateEL(
            condProbs_list,
            widIdxs_list,
            contextProbs_list,
            self.reader.idx2knwid,
            self.reader.wid2WikiTitle,
            verbose=False)

        return (predLabelScoresnumpymat_list, widIdxs_list, condProbs_list,
                contextProbs_list, evWTs, pred_TypeSetsList)
Esempio n. 2
0
    def inference_run(self):
        assert self.reader.typeOfReader == "inference"
        # assert self.reader.batch_size == 1
        self.reader.reset_test()
        numInstances = 0

        # For types: List contains numpy matrices with row_size = BatchSize
        predLabelScoresnumpymat_list = []
        # For EL : Lists contain one list per mention
        widIdxs_list = []  # Candidate WID IDXs (First is true)
        condProbs_list = []  # Crosswikis conditional priors
        contextProbs_list = []  # Predicted Entity prob using context

        while self.reader.epochs < 1:
            (
                left_batch,
                left_lengths,
                right_batch,
                right_lengths,
                coherence_batch,
                wid_idxs_batch,
                wid_cprobs_batch,
            ) = self.reader.next_test_batch()
            if left_batch is None:
                return (None, None, None, None, None, None, None)

            # Candidates for entity linking
            # feed_dict = {self.sampled_entity_ids: wid_idxs_batch,
            #              self.entity_priors: wid_cprobs_batch}
            feed_dict = {self.sampled_entity_ids: wid_idxs_batch}
            # Required Context
            if self.textcontext:
                if not self.pretrain_word_embed:
                    context_dict = {
                        self.left_batch: left_batch,
                        self.right_batch: right_batch,
                        self.left_lengths: left_lengths,
                        self.right_lengths: right_lengths,
                    }
                else:
                    context_dict = {
                        self.left_context_embeddings: left_batch,
                        self.right_context_embeddings: right_batch,
                        self.left_lengths: left_lengths,
                        self.right_lengths: right_lengths,
                    }
                feed_dict.update(context_dict)
            if self.coherence:
                coherence_dict = {
                    self.coherence_indices: coherence_batch[0],
                    self.coherence_values: coherence_batch[1],
                    self.coherence_matshape: coherence_batch[2],
                }
                feed_dict.update(coherence_dict)

            fetch_tensors = [
                self.labeling_model.label_probs,
                self.posterior_model.entity_posteriors,
            ]

            fetches = self.sess.run(fetch_tensors, feed_dict=feed_dict)

            [label_sigms, context_probs] = fetches

            predLabelScoresnumpymat_list.append(label_sigms)
            condProbs_list.extend(wid_cprobs_batch)
            widIdxs_list.extend(wid_idxs_batch)
            contextProbs_list.extend(context_probs.tolist())
            numInstances += self.reader.batch_size

        # print("Num of instances {}".format(numInstances))
        # print("Starting Type and EL Evaluations ... ")
        # pred_TypeSetsList: [B, Types], For each mention, list of pred types
        pred_TypeSetsList = evaluate_types.evaluate(
            predLabelScoresnumpymat_list, self.reader.idx2label)

        # evWTs:For each mention: Contains a list of [WTs, WIDs, Probs]
        # Each element above has (MaxPrior, MaxContext, MaxJoint)
        # sortedContextWTs: Titles sorted in decreasing context prob
        (jointProbs_list, evWTs,
         sortedContextWTs) = evaluate_inference.evaluateEL(
             condProbs_list,
             widIdxs_list,
             contextProbs_list,
             self.reader.idx2knwid,
             self.reader.wid2WikiTitle,
             verbose=False,
         )

        return (
            predLabelScoresnumpymat_list,
            widIdxs_list,
            condProbs_list,
            contextProbs_list,
            jointProbs_list,
            evWTs,
            pred_TypeSetsList,
        )