Beispiel #1
0
    def _predict_tree(self, session, model, sent, debug = False):
        """ Generate a greedy decoding parsing of a sent object with list of [word, tag]
            Return: dep parse tree, dep-label is in its id
        """
        target_num = len(self.rev_arc_labels)
        #print ("target number is %d" % target_num)
        config = Configuration(sent)    # create a parsing transition arc-standard system configuration, see (chen and manning.2014)
        while not config.is_terminal():
            features = transition_system.get_features(config, self.feature_tpl)   # 1-D list
            X = np.array([features])                            # convert 1-D list to ndarray size [1, dim]
            Y = np.array([[0] * target_num])

            # generate greedy prediction of the next_arc_id
            fetches = [model.loss, model.logit]   # fetch out prediction logit
            feed_dict = {}
            feed_dict[model.X] = X
            feed_dict[model.Y] = Y                         # dummy input y: 1D list of shape [, target_num]
            _, logit = session.run(fetches, feed_dict)     # not running eval_op, just for prediction

            pred_next_arc_id = int(np.argmax(logit))       # prediction of next arc_idx of 1 of (2*Nl +1)
            pred_next_arc = self.rev_arc_labels[pred_next_arc_id]    # 5-> L(2)   idx -> L(label_id)
            config.step(pred_next_arc)                     # Configuration Take One Step
            if (debug):
                print ("DEBUG: next arc idx is %d and next arc is %s" % (pred_next_arc_id, pred_next_arc))
        # When config is terminal, return the final dependence trees object
        dep_tree = config.tree
        return dep_tree
Beispiel #2
0
def run_beam_search(session, model, eval_op, ):
    #initialize a new empty beam with size K
    bs = BeamSearch(K)

    #Get the features of one sent and its gold_sequence
    features = []    # list of size T
    gold_sequence = []  # list of size T, int, Action Sequence

    #Compare with golden sequence
    config = Configuration(sent)
    step = 0
    max_step = 100

    while step < max_step:
        step += 1
        all_hpys = []
        # top K hyps in current step in Beam
        for h in bs.hyps:
            config = Configuration(sent)  # new empty config
            config.step(h.tokens)         # repeat the path in hyps
            feat = transition_system.get_features(config)
            top_k = bs.search(session, model, feat)   # topK of current hpys
            all_hpys.append(top_k)
        # all_hpys: top_k K*K hyps
        cur_hyps = get_topk(all_hpys)  # current step top K

        # check if golden path is in cur_hyps
        if (contain()):
            continue
        else:
            # run update function
            model.update(X, Y)

    # end of the beam search, update network
    model.update(X, Y)
Beispiel #3
0
def predict(session, model, sent):
    """ Generate a greedy decoding parsing of a sent object with list of [word, tag]
    """
    config = Configuration(sent)
    while not config.is_terminal():
        features = transition_system.get_features(config) # 1-D list
        # generate greedy prediction of the next_arc_id
        fetches = [model.loss, model.logit]   # fetch out prediction logit
        feed_dict = {}
        feed_dict[model.X] = features
        feed_dict[model.Y] = [0] * model.target_num    # dummy input y: 1D list of shape [, target_num]
        _, logit = session.run(fetches, feed_dict)     # not running eval_op, just for prediction
        
        pred_next_arc_id = int(np.argmax(logits))      # prediction of next arc_idx of 1 of (2*Nl +1)
        pred_next_arc = []
        config.step(pred_next_arc)                     # Configuration Take One Step 
        
        print ("next arc idx is %d and next arc is %s" % (pred_next_arc_id, pred_next_arc))
        
        next_arc = get_next_arc(config, tree)  # number
        next_arc_id = arc_labels[next_arc]