예제 #1
0
 def run_on_sents(self, session, sents):
     input_sents = map(lambda x: ' '.join(x), sents)
     self.loader.run_on_sents(input_sents)
     feed = {}
     predictions_batch = {}
     for feat in self.inputs_placeholder_dict.keys():
         feed[self.inputs_placeholder_dict[
             feat]] = self.loader.inputs_test_batch[feat]
     feed[self.keep_prob] = 1.0
     feed[self.hidden_prob] = 1.0
     feed[self.input_keep_prob] = 1.0
     feed[self.mlp_prob] = 1.0
     weight, arc_outputs, rel_scores, predicted_stags, predicted_jk = session.run(
         [
             self.weight, self.arc_outputs, self.rel_scores,
             self.predicted_stags, self.predicted_jk
         ],
         feed_dict=feed)
     weight = weight.astype(bool)
     predicted_stags = predicted_stags[weight]
     predicted_jk = predicted_jk[weight]
     predictions_batch['stags'] = predicted_stags
     predictions_batch['jk'] = predicted_jk
     non_padding = weight.astype(bool)
     non_padding[:, 0] = True  ## take the dummy root nodes
     predicted_arcs, predicted_rels = predict_arcs_rels(
         arc_outputs, rel_scores, non_padding)
     predictions_batch['arcs'] = predicted_arcs
     predictions_batch['rels'] = predicted_rels
     results = self.loader.get_results(predictions_batch)
     return results
예제 #2
0
 def run_on_sents(self, session, sents):
     input_sents = map(lambda x: ' '.join(x), sents)
     self.loader.run_on_sents(input_sents)
     feed = {}
     predictions_batch = {}
     for feat in self.inputs_placeholder_dict.keys():
         feed[self.inputs_placeholder_dict[feat]] = self.loader.inputs_test_batch[feat]
     feed[self.keep_prob] = 1.0
     feed[self.hidden_prob] = 1.0
     feed[self.input_keep_prob] = 1.0
     feed[self.mlp_prob] = 1.0
     weight, arc_outputs, rel_scores, predicted_stags, predicted_jk, arc_probs, rel_probs = session.run([self.weight, self.arc_outputs, self.rel_scores, self.predicted_stags, self.predicted_jk, self.arc_probs, self.rel_probs], feed_dict=feed)
     weight = weight.astype(bool)
     predicted_stags = predicted_stags[weight]
     predicted_jk = predicted_jk[weight]
     predictions_batch['stags'] = predicted_stags
     predictions_batch['jk'] = predicted_jk
     non_padding = weight.astype(bool)
     non_padding[:, 0] = True ## take the dummy root nodes
     predicted_arcs, predicted_rels = predict_arcs_rels(arc_outputs, rel_scores, non_padding)
     predictions_batch['arcs'] = predicted_arcs
     predictions_batch['rels'] = predicted_rels
     results = self.loader.get_results(predictions_batch)
     ## nbest
     true_arc_probs = []
     true_rel_probs = []
     weight_with_root = weight.copy()
     weight_with_root[:,0] = True
     for sent_idx in xrange(arc_probs.shape[0]):
         true_arc_probs.append(arc_probs[sent_idx][weight[sent_idx]][:,weight_with_root[sent_idx]])
         true_rel_probs.append(rel_probs[sent_idx][weight[sent_idx]][:,weight_with_root[sent_idx]])
     return results, true_arc_probs, true_rel_probs
예제 #3
0
    def run_batch(self, session, testmode = False):
        if not testmode:
            feed = {}
            for feat in self.inputs_placeholder_dict.keys():
                feed[self.inputs_placeholder_dict[feat]] = self.loader.inputs_train_batch[feat]
            feed[self.keep_prob] = self.opts.dropout_p
            feed[self.hidden_prob] = self.opts.hidden_p
            feed[self.input_keep_prob] = self.opts.input_dp
            feed[self.mlp_prob] = self.opts.mlp_prob
            feed[self.word_dropout] = self.opts.word_dropout
            if self.opts.word_dropout_alpha > 0:
                feed[self.word_dropout_alpha] = self.loader.word_dropout_alpha_vec
            if self.opts.word_dropout_jw < 1.0:
                feed[self.word_dropout_jw] = self.opts.word_dropout_jw
            train_op = self.train_op
            _, loss, UAS, rel_acc = session.run([train_op, self.loss, self.UAS, self.rel_acc], feed_dict=feed)
            return loss, UAS, rel_acc
        else:
            feed = {}
            predictions_batch = {}
            for feat in self.inputs_placeholder_dict.keys():
                feed[self.inputs_placeholder_dict[feat]] = self.loader.inputs_test_batch[feat]
            feed[self.keep_prob] = 1.0
            feed[self.hidden_prob] = 1.0
            feed[self.input_keep_prob] = 1.0
            feed[self.mlp_prob] = 1.0
            feed[self.word_dropout] = 1.0
            if self.opts.word_dropout_alpha > 0:
                feed[self.word_dropout_alpha] = np.ones(self.loader.word_embeddings.shape[0]) 
            if self.opts.word_dropout_jw < 1.0:
                feed[self.word_dropout_jw] = 1.0
#            loss, accuracy, predictions, weight = session.run([self.loss, self.accuracy, self.predictions, self.weight], feed_dict=feed)
            loss, predicted_arcs, predicted_rels, UAS, weight, arc_outputs, rel_scores = session.run([self.loss, self.predicted_arcs, self.predicted_rels, self.UAS, self.weight, self.arc_outputs, self.rel_scores], feed_dict=feed)
            if self.test_opts is not None:
                if self.opts.embedding_dim > 0:
                    word_embeddings = session.run(self.embeddings)
                    with open(os.path.join(self.opts.model_dir, 'word_embeddings.pkl'), 'wb') as fout:
                        pickle.dump(word_embeddings, fout)
                pos_embeddings = session.run(self.pos_embeddings)
                with open(os.path.join(self.opts.model_dir, 'pos_embeddings.pkl'), 'wb') as fout:
                    pickle.dump(pos_embeddings, fout)
                weights = session.run(self.input_weights, feed_dict=feed)
                with open(os.path.join(self.opts.model_dir, 'pos_weight.pkl'), 'wb') as fout:
                    pickle.dump(weights, fout)
            weight = weight.astype(bool)
            predicted_arcs_greedy = predicted_arcs[weight]
            predicted_rels_greedy = predicted_rels[weight]
            predictions_batch['arcs_greedy'] = predicted_arcs_greedy
            predictions_batch['rels_greedy'] = predicted_rels_greedy
            non_padding = weight.astype(bool)
            non_padding[:, 0] = True ## take the dummy root nodes
            predicted_arcs, predicted_rels = predict_arcs_rels(arc_outputs, rel_scores, non_padding)
            predictions_batch['arcs'] = predicted_arcs
            predictions_batch['rels'] = predicted_rels
#            print(predicted_greedy_arcs.shape)
#            print(predicted_arcs.shape)
            #print(arc_outputs.shape)
            return loss, predictions_batch, UAS
 def run_batch(self, session, testmode=False):
     if not testmode:
         feed = {}
         for feat in self.inputs_placeholder_dict.keys():
             feed[self.inputs_placeholder_dict[
                 feat]] = self.loader.inputs_train_batch[feat]
         feed[self.keep_prob] = self.opts.dropout_p
         feed[self.hidden_prob] = self.opts.hidden_p
         feed[self.input_keep_prob] = self.opts.input_dp
         feed[self.mlp_prob] = self.opts.mlp_prob
         train_op = self.train_op
         _, loss, UAS, rel_acc, stag_acc = session.run(
             [train_op, self.loss, self.UAS, self.rel_acc, self.stag_acc],
             feed_dict=feed)
         return loss, UAS, rel_acc, stag_acc
     else:
         feed = {}
         predictions_batch = {}
         for feat in self.inputs_placeholder_dict.keys():
             feed[self.inputs_placeholder_dict[
                 feat]] = self.loader.inputs_test_batch[feat]
         feed[self.keep_prob] = 1.0
         feed[self.hidden_prob] = 1.0
         feed[self.input_keep_prob] = 1.0
         feed[self.mlp_prob] = 1.0
         #            loss, accuracy, predictions, weight = session.run([self.loss, self.accuracy, self.predictions, self.weight], feed_dict=feed)
         loss, predicted_arcs, predicted_rels, UAS, weight, arc_outputs, rel_scores, stag_acc, predicted_stags, predicted_jk, probs = session.run(
             [
                 self.loss, self.predicted_arcs, self.predicted_rels,
                 self.UAS, self.weight, self.arc_outputs, self.rel_scores,
                 self.stag_acc, self.predicted_stags, self.predicted_jk,
                 self.probs
             ],
             feed_dict=feed)
         weight = weight.astype(bool)
         predicted_arcs_greedy = predicted_arcs[weight]
         predicted_rels_greedy = predicted_rels[weight]
         predicted_stags = predicted_stags[weight]
         predicted_jk = predicted_jk[weight]
         predictions_batch['arcs_greedy'] = predicted_arcs_greedy
         predictions_batch['rels_greedy'] = predicted_rels_greedy
         predictions_batch['stags'] = predicted_stags
         predictions_batch['jk'] = predicted_jk
         non_padding = weight.astype(bool)
         non_padding[:, 0] = True  ## take the dummy root nodes
         predicted_arcs, predicted_rels = predict_arcs_rels(
             arc_outputs, rel_scores, non_padding)
         predictions_batch['arcs'] = predicted_arcs
         predictions_batch['rels'] = predicted_rels
         probs = probs[weight]
         #            print(predicted_greedy_arcs.shape)
         #            print(predicted_arcs.shape)
         #print(arc_outputs.shape)
         return loss, predictions_batch, UAS, probs