Example #1
0
def export_model_to_pb():
    data_dir = FLAGS.data_dir

    # Create vocabularies of the appropriate sizes.
    vocab_path = os.path.join(data_dir, "in_vocab.txt")
    tag_vocab_path = os.path.join(data_dir, "out_vocab.txt")
    label_vocab_path = os.path.join(data_dir, "label.txt")

    vocab, rev_vocab = data_utils.initialize_vocab(vocab_path)
    tag_vocab, rev_tag_vocab = data_utils.initialize_vocab(tag_vocab_path)
    label_vocab, rev_label_vocab = data_utils.initialize_vocab(
        label_vocab_path)

    config = tf.ConfigProto(
        gpu_options=tf.GPUOptions(per_process_gpu_memory_fraction=0.23),
        # device_count = {'gpu': 2}
    )

    with tf.Session(config=config) as sess:
        # Create model.
        print("Creating %d layers of %d units." %
              (FLAGS.num_layers, FLAGS.size))

        model, model_test = create_model(sess, len(vocab), len(tag_vocab),
                                         len(label_vocab))

        print("Creating model with " +
              "source_vocab_size=%d, target_vocab_size=%d, label_vocab_size=%d." \
              % (len(vocab), len(tag_vocab), len(label_vocab)))

        print('transform model with ckpt to pb')

        export_model_pb(sess, model)
Example #2
0
    def __init__(self):
        print('Applying Parameters:')
        for k, v in FLAGS.__dict__['__flags'].items():
            print('%s: %s' % (k, str(v)))
        print("Preparing data in %s" % FLAGS.data_dir)
        vocab_path = ''
        tag_vocab_path = ''
        label_vocab_path = ''
        date_set = data_utils.prepare_multi_task_data(FLAGS.data_dir,
                                                      FLAGS.in_vocab_size,
                                                      FLAGS.out_vocab_size)

        in_seq_train, out_seq_train, label_train = date_set[0]
        in_seq_dev, out_seq_dev, label_dev = date_set[1]
        in_seq_test, out_seq_test, label_test = date_set[2]
        vocab_path, tag_vocab_path, label_vocab_path = date_set[3]

        vocab, rev_vocab = data_utils.initialize_vocab(vocab_path)
        tag_vocab, rev_tag_vocab = data_utils.initialize_vocab(tag_vocab_path)
        label_vocab, rev_label_vocab = data_utils.initialize_vocab(
            label_vocab_path)

        self.sess = tf.Session()
        self.model, self.model_test = create_model(self.sess, len(vocab),
                                                   len(tag_vocab),
                                                   len(label_vocab))
Example #3
0
def decode():
    print("Decoding interactively")
    with tf.Session() as sess:
        # Create model and load parameters.
        model = create_model(sess, True)
        model.batch_size = 1  # We decode one sentence at a time.
        # Load vocabularies.
        vocab_path = "vocab%d" % FLAGS.vocab_size
        vocab, rev_vocab = data_utils.initialize_vocab(vocab_path)

        # Decode from standard input.
        sys.stdout.write("> ")
        sys.stdout.flush()
        sentence = sys.stdin.readline()
        while sentence:
            # Get token-ids for the input sentence.
            token_ids = data_utils.sentence_to_token_ids(
                tf.compat.as_bytes(sentence), vocab)
            # Which bucket does it belong to?
            bucket_id = len(_buckets) - 1
            for i, bucket in enumerate(_buckets):
                if bucket[0] >= len(token_ids):
                    bucket_id = i
                    break
            else:
                logging.warning("Sentence truncated: %s", sentence)

            # Get a 1-element batch to feed the sentence to the model.
            encoder_inputs, decoder_inputs, target_weights = model.get_batch(
                {bucket_id: [(token_ids, [])]}, bucket_id)
            # Get output logits for the sentence.
            _, _, output_logits = model.step(sess, encoder_inputs,
                                             decoder_inputs, target_weights,
                                             bucket_id, True)
            # This is a greedy decoder - outputs are just argmaxes of output_logits.
            outputs = [
                int(np.argmax(logit, axis=1)) for logit in output_logits
            ]
            # If there is an EOS symbol in outputs, cut them at that point.
            if data_utils.EOS_ID in outputs:
                outputs = outputs[:outputs.index(data_utils.EOS_ID)]
            # Print out sentence corresponding to outputs.
            print(" ".join(
                [tf.compat.as_str(rev_vocab[output]) for output in outputs]))
            print("> ", end="")
            sys.stdout.flush()
            sentence = sys.stdin.readline()
Example #4
0
def train():
    print('Applying Parameters:')
    for k, v in FLAGS.__flags.iteritems():
        print('%s: %s' % (k, str(v)))
    print("Preparing data in %s" % FLAGS.data_dir)
    vocab_path = ''
    tag_vocab_path = ''
    label_vocab_path = ''
    date_set = data_utils.prepare_multi_task_data(FLAGS.data_dir,
                                                  FLAGS.in_vocab_size,
                                                  FLAGS.out_vocab_size)
    in_seq_train, out_seq_train, label_train = date_set[0]
    in_seq_dev, out_seq_dev, label_dev = date_set[1]
    in_seq_test, out_seq_test, label_test = date_set[2]
    vocab_path, tag_vocab_path, label_vocab_path = date_set[3]

    result_dir = FLAGS.train_dir + '/test_results'
    if not os.path.isdir(result_dir):
        os.makedirs(result_dir)

    current_taging_valid_out_file = result_dir + '/tagging.valid.hyp.txt'
    current_taging_test_out_file = result_dir + '/tagging.test.hyp.txt'

    vocab, rev_vocab = data_utils.initialize_vocab(vocab_path)
    tag_vocab, rev_tag_vocab = data_utils.initialize_vocab(tag_vocab_path)
    label_vocab, rev_label_vocab = data_utils.initialize_vocab(
        label_vocab_path)

    config = tf.ConfigProto(
        gpu_options=tf.GPUOptions(per_process_gpu_memory_fraction=0.23),
        #device_count = {'gpu': 2}
    )

    with tf.Session(config=config) as sess:
        # Create model.
        print("Max sequence length: %d." % _buckets[0][0])
        print("Creating %d layers of %d units." %
              (FLAGS.num_layers, FLAGS.size))

        model, model_test = create_model(sess, len(vocab), len(tag_vocab),
                                         len(label_vocab))
        print ("Creating model with " +
               "source_vocab_size=%d, target_vocab_size=%d, label_vocab_size=%d." \
               % (len(vocab), len(tag_vocab), len(label_vocab)))

        # Read data into buckets and compute their sizes.
        print("Reading train/valid/test data (training set limit: %d)." %
              FLAGS.max_train_data_size)
        dev_set = read_data(in_seq_dev, out_seq_dev, label_dev)
        test_set = read_data(in_seq_test, out_seq_test, label_test)
        train_set = read_data(in_seq_train, out_seq_train, label_train)
        train_bucket_sizes = [len(train_set[b]) for b in xrange(len(_buckets))]
        train_total_size = float(sum(train_bucket_sizes))

        train_buckets_scale = [
            sum(train_bucket_sizes[:i + 1]) / train_total_size
            for i in xrange(len(train_bucket_sizes))
        ]

        # This is the training loop.
        step_time, loss = 0.0, 0.0
        current_step = 0

        best_valid_score = 0
        best_test_score = 0
        while model.global_step.eval() < FLAGS.max_training_steps:
            random_number_01 = np.random.random_sample()
            bucket_id = min([
                i for i in xrange(len(train_buckets_scale))
                if train_buckets_scale[i] > random_number_01
            ])

            # Get a batch and make a step.
            start_time = time.time()
            batch_data = model.get_batch(train_set, bucket_id)
            encoder_inputs, tags, tag_weights, batch_sequence_length, labels = batch_data
            if task['joint'] == 1:
                step_outputs = model.joint_step(sess, encoder_inputs, tags,
                                                tag_weights, labels,
                                                batch_sequence_length,
                                                bucket_id, False)
                _, step_loss, tagging_logits, class_logits = step_outputs
            elif task['tagging'] == 1:
                step_outputs = model.tagging_step(sess, encoder_inputs, tags,
                                                  tag_weights,
                                                  batch_sequence_length,
                                                  bucket_id, False)
                _, step_loss, tagging_logits = step_outputs
            elif task['intent'] == 1:
                step_outputs = model.classification_step(
                    sess, encoder_inputs, labels, batch_sequence_length,
                    bucket_id, False)
                _, step_loss, class_logits = step_outputs

            step_time += (time.time() -
                          start_time) / FLAGS.steps_per_checkpoint
            loss += step_loss / FLAGS.steps_per_checkpoint
            current_step += 1

            # Once in a while, we save checkpoint, print statistics, and run evals.
            if current_step % FLAGS.steps_per_checkpoint == 0:
                perplexity = math.exp(loss) if loss < 300 else float('inf')
                print(
                    "global step %d step-time %.2f. Training perplexity %.2f" %
                    (model.global_step.eval(), step_time, perplexity))
                sys.stdout.flush()
                # Save checkpoint and zero timer and loss.
                checkpoint_path = os.path.join(FLAGS.train_dir, "model.ckpt")
                model.saver.save(sess,
                                 checkpoint_path,
                                 global_step=model.global_step)
                step_time, loss = 0.0, 0.0

                def run_valid_test(data_set, mode):  # mode: Eval, Test
                    # Run evals on development/test set and print the accuracy.
                    word_list = list()
                    ref_tag_list = list()
                    hyp_tag_list = list()
                    ref_label_list = list()
                    hyp_label_list = list()
                    correct_count = 0
                    accuracy = 0.0
                    tagging_eval_result = dict()
                    for bucket_id in xrange(len(_buckets)):
                        eval_loss = 0.0
                        count = 0
                        for i in xrange(len(data_set[bucket_id])):
                            count += 1
                            sample = model_test.get_one(data_set, bucket_id, i)
                            encoder_inputs, tags, tag_weights, sequence_length, labels = sample
                            tagging_logits = []
                            class_logits = []
                            if task['joint'] == 1:
                                step_outputs = model_test.joint_step(
                                    sess, encoder_inputs, tags, tag_weights,
                                    labels, sequence_length, bucket_id, True)
                                _, step_loss, tagging_logits, class_logits = step_outputs
                            elif task['tagging'] == 1:
                                step_outputs = model_test.tagging_step(
                                    sess, encoder_inputs, tags, tag_weights,
                                    sequence_length, bucket_id, True)
                                _, step_loss, tagging_logits = step_outputs
                            elif task['intent'] == 1:
                                step_outputs = model_test.classification_step(
                                    sess, encoder_inputs, labels,
                                    sequence_length, bucket_id, True)
                                _, step_loss, class_logits = step_outputs
                            eval_loss += step_loss / len(data_set[bucket_id])
                            hyp_label = None
                            if task['intent'] == 1:
                                ref_label_list.append(
                                    rev_label_vocab[labels[0][0]])
                                hyp_label = np.argmax(class_logits[0], 0)
                                hyp_label_list.append(
                                    rev_label_vocab[hyp_label])
                                if labels[0] == hyp_label:
                                    correct_count += 1
                            if task['tagging'] == 1:
                                word_list.append([rev_vocab[x[0]] for x in \
                                                  encoder_inputs[:sequence_length[0]]])
                                ref_tag_list.append([rev_tag_vocab[x[0]] for x in \
                                                     tags[:sequence_length[0]]])
                                hyp_tag_list.append(
                                        [rev_tag_vocab[np.argmax(x)] for x in \
                                                       tagging_logits[:sequence_length[0]]])

                    accuracy = float(correct_count) * 100 / count
                    if task['intent'] == 1:
                        print("  %s accuracy: %.2f %d/%d" \
                              % (mode, accuracy, correct_count, count))
                        sys.stdout.flush()
                    if task['tagging'] == 1:
                        if mode == 'Eval':
                            taging_out_file = current_taging_valid_out_file
                        elif mode == 'Test':
                            taging_out_file = current_taging_test_out_file
                        tagging_eval_result = conlleval(
                            hyp_tag_list, ref_tag_list, word_list,
                            taging_out_file)
                        print("  %s f1-score: %.2f" %
                              (mode, tagging_eval_result['f1']))
                        sys.stdout.flush()
                    return accuracy, tagging_eval_result

                # valid
                valid_accuracy, valid_tagging_result = run_valid_test(
                    dev_set, 'Eval')
                if task['tagging'] == 1 \
                    and valid_tagging_result['f1'] > best_valid_score:
                    best_valid_score = valid_tagging_result['f1']
                    # save the best output file
                    subprocess.call(['mv',
                                     current_taging_valid_out_file,
                                     current_taging_valid_out_file + '.best_f1_%.2f' \
                                     % best_valid_score])
                # test, run test after each validation for development purpose.
                test_accuracy, test_tagging_result = run_valid_test(
                    test_set, 'Test')
                if task['tagging'] == 1 \
                    and test_tagging_result['f1'] > best_test_score:
                    best_test_score = test_tagging_result['f1']
                    # save the best output file
                    subprocess.call(['mv',
                                     current_taging_test_out_file,
                                     current_taging_test_out_file + '.best_f1_%.2f' \
                                     % best_test_score])
Example #5
0
def train():
    
  # See parameters.
  print ('Applying Parameters:')
  for k,v in FLAGS.__dict__['__flags'].items():
    print ('%s: %s' % (k, str(v)))
    
  # 4-3-1. Prepare indexing data and correspondiing labels.  
  print("Preparing data in %s" % FLAGS.data_dir)
  vocab_path = ''
  tag_vocab_path = ''
    
  # 4-3-1-1. String data --) token index / Make word and label dictionary.
  date_set = data_utils.prepare_multi_task_data(
    FLAGS.data_dir, FLAGS.in_vocab_size, FLAGS.out_vocab_size)
    
  # 4-3-1-2. Get path of each result.
  in_seq_train, out_seq_train = date_set[0]
  in_seq_test, out_seq_test = date_set[1]
  vocab_path, tag_vocab_path = date_set[2]
 
  # Where do we save the result?  
  result_dir = FLAGS.train_dir + '/test_results'
  if not os.path.isdir(result_dir):
      os.makedirs(result_dir)
  current_taging_valid_out_file = result_dir + '/tagging.valid.hyp.txt'
  current_taging_test_out_file = result_dir + '/tagging.test.hyp.txt'
    
  # 4-3-2. Get index dictionary and word list.   
  vocab, rev_vocab = data_utils.initialize_vocab(vocab_path)
  tag_vocab, rev_tag_vocab = data_utils.initialize_vocab(tag_vocab_path)
  tag_vocab_inv = dict()
    
    
  for string, i in tag_vocab.items():
        tag_vocab_inv[i] = string
  config = tf.ConfigProto(
      gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.23),
      #device_count = {'gpu': 2}
  )
    
  with tf.Session(config=config) as sess:
    print("Max sequence length: %d." % _buckets[0][0])
    print("Creating %d layers of %d units." % (FLAGS.num_layers, FLAGS.size))
    
    # 4-3-3. Make train/test model.
    model, model_test = create_model(sess, 
                                     len(vocab), 
                                     len(tag_vocab)
                                     )
    print ("Creating model with " + 
           "source_vocab_size=%d, target_vocab_size=%d" \
           % (len(vocab), len(tag_vocab)))

    # Read data into buckets and compute their sizes.
    print ("Reading train/valid/test data (training set limit: %d)."
           % FLAGS.max_train_data_size)
    
    # 4-3-4. Load data using "# 4-1."
    test_set = read_data(in_seq_test, out_seq_test)
    train_set = read_data(in_seq_train, out_seq_train)
    train_bucket_sizes = [len(train_set[b]) for b in xrange(len(_buckets))]
    train_total_size = float(sum(train_bucket_sizes))
    train_buckets_scale = [sum(train_bucket_sizes[:i + 1]) / train_total_size
                           for i in xrange(len(train_bucket_sizes))]
    
    # 4-3-5. Train Loop.
    step_time, loss = 0.0, 0.0
    current_step = 0
    best_valid_score = 0
    best_test_score = 0

    while model.global_step.eval() < FLAGS.max_training_steps:
      random_number_01 = np.random.random_sample()
      bucket_id = min([i for i in xrange(len(train_buckets_scale))
                       if train_buckets_scale[i] > random_number_01])
      start_time = time.time()
        
      # 4-3-5-1. get batch
      batch_data = model.get_batch(train_set, bucket_id)
      encoder_inputs,tags,tag_weights,batch_sequence_length = batch_data
      
      step_outputs = model.tagging_step(sess, 
                                        encoder_inputs,
                                        tags,
                                        tag_weights,
                                        batch_sequence_length, 
                                        bucket_id, 
                                        False)
      _, step_loss, tagging_logits = step_outputs
        
        
      step_time += (time.time() - start_time) / FLAGS.steps_per_checkpoint
      loss += step_loss / FLAGS.steps_per_checkpoint
      current_step += 1

      # Once in a while, we save checkpoint, print statistics, and run evals.
      if current_step % FLAGS.steps_per_checkpoint == 0:
        perplexity = math.exp(loss) if loss < 300 else float('inf')
        print ("global step %d step-time %.2f. Training perplexity %.2f" 
            % (model.global_step.eval(), step_time, perplexity))
        sys.stdout.flush()
        
        # Save checkpoint and zero timer and loss.
        checkpoint_path = os.path.join(FLAGS.train_dir, "model.ckpt")
        model.saver.save(sess, checkpoint_path, global_step=model.global_step)
        step_time, loss = 0.0, 0.0
        
           
    # Test
    count = 0
    word_list = list()
    ref_tag_list = list()
    hyp_tag_list = list()
    for bucket_id in xrange(len(_buckets)):
        for i in xrange(len(test_set[bucket_id])):
            count += 1
            sample = model_test.get_one(test_set, bucket_id, i)
            encoder_inputs, tags, tag_weights, sequence_length= sample
            step_outputs = model_test.tagging_step(sess,
                                                   encoder_inputs,
                                                   tags,
                                                   tag_weights,
                                                   sequence_length,
                                                   bucket_id,
                                                   True)
            _, step_loss, tagging_logits = step_outputs
            
            lst = []
            string = ""
            for num in encoder_inputs:
                num = num[0]
                word = rev_vocab[num]
                if word == "_PAD" or word == "_UNK":
                    continue
                else:
                    lst.append(word)
                    string = string + word + " "
            string = string + " : "
            string2 = string
            
            for word in tagging_logits:
                word = word[0]
                sort_num = np.argsort(word)
                b = sort_num[39999]
                word = rev_tag_vocab[b]
                if word == "_PAD" or word == "_UNK":
                    continue
                else:
                    lst.append(word)
                    string = string + word + " "
            print(string)
            
            for word in tagging_logits:
                word = word[0]
                sort_num = np.argsort(word)
                b = sort_num[39998]
                word = rev_tag_vocab[b]
                if word == "_PAD" or word == "_UNK":
                    continue
                else:
                    lst.append(word)
                    string2 = string2 + word + " "
            print(string2)
            print("\n")
Example #6
0
def test():
    print('Applying Parameters:')
    for k, v in FLAGS.__dict__['__flags'].items():
        print('%s: %s' % (k, str(v)))
    print("\nPreparing data in %s" % FLAGS.data_dir)
    vocab_path = ''
    tag_vocab_path = ''
    label_vocab_path = ''
    date_set = data_utils.prepare_multi_task_data(FLAGS.data_dir,
                                                  FLAGS.in_vocab_size,
                                                  FLAGS.out_vocab_size)
    in_seq_test, out_seq_test, label_test = date_set[2]
    vocab_path, tag_vocab_path, label_vocab_path = date_set[3]

    vocab, rev_vocab = data_utils.initialize_vocab(vocab_path)
    tag_vocab, rev_tag_vocab = data_utils.initialize_vocab(tag_vocab_path)
    label_vocab, rev_label_vocab = data_utils.initialize_vocab(
        label_vocab_path)

    with tf.Session() as sess:
        # Create model.
        print("\nCreating %d layers of %d units." %
              (FLAGS.num_layers, FLAGS.size))

        model, model_test = create_model(sess, len(vocab), len(tag_vocab),
                                         len(label_vocab))
        print ("Created model with " +
               "source_vocab_size=%d, target_vocab_size=%d, label_vocab_size=%d." \
               % (len(vocab), len(tag_vocab), len(label_vocab)))

        # Read data into buckets and compute their sizes.
        print("\nReading test data")
        test_set = read_data(in_seq_test, out_seq_test, label_test)

        def run_valid_test(data_set, mode):  # mode: Eval, Test
            # Run evals on development/test set and print the accuracy.
            word_list = list()
            ref_tag_list = list()
            hyp_tag_list = list()
            ref_label_list = list()
            hyp_label_list = list()
            correct_count = 0
            accuracy = 0.0
            tagging_eval_result = dict()
            for bucket_id in xrange(len(_buckets)):
                eval_loss = 0.0
                count = 0
                for i in xrange(len(data_set[bucket_id])):
                    count += 1
                    sample = model_test.get_one(data_set, bucket_id, i)
                    encoder_inputs, tags, tag_weights, sequence_length, labels = sample
                    tagging_logits = []
                    class_logits = []
                    if task['joint'] == 1:
                        step_outputs = model_test.joint_step(
                            sess, encoder_inputs, tags, tag_weights, labels,
                            sequence_length, bucket_id, True)
                        _, step_loss, tagging_logits, class_logits = step_outputs
                        class_prob = _softmax(class_logits[0])
                    elif task['tagging'] == 1:
                        step_outputs = model_test.tagging_step(
                            sess, encoder_inputs, tags, tag_weights,
                            sequence_length, bucket_id, True)
                        _, step_loss, tagging_logits = step_outputs
                    elif task['intent'] == 1:
                        step_outputs = model_test.classification_step(
                            sess, encoder_inputs, labels, sequence_length,
                            bucket_id, True)
                        _, step_loss, class_logits = step_outputs
                    eval_loss += step_loss / len(data_set[bucket_id])
                    hyp_label = None
                    if task['intent'] == 1:
                        ref_label_list.append(rev_label_vocab[labels[0][0]])
                        hyp_label = np.argmax(class_logits[0], 0)
                        hyp_label_list.append(rev_label_vocab[hyp_label])
                        if labels[0] == hyp_label:
                            correct_count += 1
                    if task['tagging'] == 1:
                        word_list.append([rev_vocab[x[0]] for x in \
                                          encoder_inputs[:sequence_length[0]]])
                        ref_tag = [x[0] for x in tags[:sequence_length[0]]]
                        ref_tag_list.append([rev_tag_vocab[x[0]] for x in \
                                             tags[:sequence_length[0]]])
                        hyp_tag = [
                            np.argmax(x)
                            for x in tagging_logits[:sequence_length[0]]
                        ]
                        hyp_tag_list.append(
                                [rev_tag_vocab[np.argmax(x)] for x in \
                                               tagging_logits[:sequence_length[0]]])

                    if labels[0] != hyp_label or ref_tag != hyp_tag:
                        error_type = []
                        if labels[0] != hyp_label:
                            error_type.append("Intent misclassification")
                        if ref_tag != hyp_tag:
                            error_type.append("Slot error")
                        print("\n" + ", ".join(error_type))
                        print("(intent) input: (%s) %s" %
                                (rev_label_vocab[labels[0][0]],
                                 " ".join([rev_vocab[x[0]] for x in \
                                           encoder_inputs[:sequence_length[0]]])))
                        print("true slots: %s" % " ".join(ref_tag_list[-1]))
                        print("pred slots: %s" % " ".join(hyp_tag_list[-1]))
                        pred_labels = np.argsort(class_prob)[-3:]
                        intent_preds = [
                            rev_label_vocab[l] for l in pred_labels
                        ]
                        print("Top 3 predicted intents:")
                        for idx in reversed(pred_labels):
                            print("%s (%.4f)" %
                                  (rev_label_vocab[idx], class_prob[idx]))

            accuracy = float(correct_count) * 100 / count
            if task['intent'] == 1:
                print("  %s accuracy: %.2f %d/%d" \
                      % (mode, accuracy, correct_count, count))
                sys.stdout.flush()
            '''
        if task['tagging'] == 1:
          tagging_eval_result = conlleval(hyp_tag_list, 
                                          ref_tag_list, 
                                          word_list, 
                                          None)
          print("  %s f1-score: %.2f" % (mode, tagging_eval_result['f1']))
          sys.stdout.flush()
        return accuracy, tagging_eval_result
        '''
            return accuracy, ref_label_list, hyp_label_list

        # test, run test after each validation for development purpose.
        #test_accuracy, test_tagging_result = run_valid_test(test_set, 'Test')
        test_accuracy, ref_label_list, hyp_label_list = run_valid_test(
            test_set, 'Test')

        # Compute confusion matrix
        cnf_matrix = confusion_matrix(ref_label_list,
                                      hyp_label_list,
                                      labels=rev_label_vocab)
        np.set_printoptions(precision=2)

        # Plot non-normalized confusion matrix
        plt.figure(figsize=(12, 10))
        plot_confusion_matrix(cnf_matrix,
                              classes=rev_label_vocab,
                              title='Confusion matrix, without normalization')

        # Plot normalized confusion matrix
        plt.figure(figsize=(12, 10))
        plot_confusion_matrix(cnf_matrix,
                              classes=rev_label_vocab,
                              normalize=True,
                              title='Normalized confusion matrix')

        plt.show()
Example #7
0
def evaluate_line():
    data_dir = FLAGS.data_dir
    decoder_size = encoder_size = FLAGS.max_sequence_length

    # Create vocabularies of the appropriate sizes.
    vocab_path = os.path.join(data_dir, "in_vocab.txt")
    tag_vocab_path = os.path.join(data_dir, "out_vocab.txt")
    label_vocab_path = os.path.join(data_dir, "label.txt")

    vocab, rev_vocab = data_utils.initialize_vocab(vocab_path)
    tag_vocab, rev_tag_vocab = data_utils.initialize_vocab(tag_vocab_path)
    label_vocab, rev_label_vocab = data_utils.initialize_vocab(
        label_vocab_path)

    UNK_ID = data_utils.UNK_ID_dict['with_padding']

    config = tf.ConfigProto(
        gpu_options=tf.GPUOptions(per_process_gpu_memory_fraction=0.23),
        # device_count = {'gpu': 2}
    )

    with tf.Session(config=config) as sess:
        # Create model.
        print("Creating %d layers of %d units." %
              (FLAGS.num_layers, FLAGS.size))

        model, model_test = create_model(sess, len(vocab), len(tag_vocab),
                                         len(label_vocab))

        print("Creating model with " +
              "source_vocab_size=%d, target_vocab_size=%d, label_vocab_size=%d." \
              % (len(vocab), len(tag_vocab), len(label_vocab)))

        while True:

            string = input("请输入测试句子: ").strip()

            line = ' '.join(list(string))

            encoder_input = data_utils.sentence_to_token_ids(
                line, vocab, UNK_ID, data_utils.naive_tokenizer, False)

            # Encoder inputs are padded and then reversed.
            encoder_pad = [data_utils.PAD_ID
                           ] * (encoder_size - len(encoder_input))
            # encoder_inputs.append(list(reversed(encoder_input + encoder_pad)))
            encoder_inputs = [
                np.array(input) for input in encoder_input + encoder_pad
            ]

            tag_weights = []
            # Batch decoder inputs are re-indexed decoder_inputs, we create weights.
            for length_idx in xrange(decoder_size):
                # Create target_weights to be 0 for targets that are padding.
                tag_weight = np.ones(1, dtype=np.float32)
                if encoder_inputs[length_idx] == data_utils.PAD_ID:
                    tag_weight = np.zeros(1, dtype=np.float32)
                tag_weights.append(tag_weight)

            input_feed = {}
            input_feed[model_test.sequence_length] = np.array(
                [len(encoder_input)], dtype=np.int32)
            for l in xrange(encoder_size):
                input_feed[model_test.encoder_inputs[l].name] = [
                    encoder_inputs[l]
                ]
                input_feed[model_test.tag_weights[l].name] = tag_weights[l]

            output_feed = []
            for i in range(len(encoder_input)):
                output_feed.append(model_test.tagging_output[i])
            output_feed.append(model_test.classification_output)

            outputs = sess.run(output_feed, input_feed)

            tagging_logits, class_logits = outputs[:len(encoder_input
                                                        )], outputs[-1]

            tags = [rev_tag_vocab[np.argmax(x)] for x in tagging_logits]

            label = rev_label_vocab[np.argmax(class_logits)]

            result = result_to_json(string, tags, label)

            print(result)
Example #8
0
def load_variable_pb():
    encoder_size = FLAGS.max_sequence_length
    session = tf.Session(graph=tf.Graph())
    model_file_path = "pb_model/model"
    meta_graph = tf.saved_model.loader.load(
        session, [tf.saved_model.tag_constants.SERVING], model_file_path)

    model_graph_signature = list(meta_graph.signature_def.items())[0][1]
    output_feed = []
    output_op_names = []
    output_tensor_dict = {}
    for i in range(encoder_size):
        output_op_names.append('tag:%d' % i)
    output_op_names.append('label')

    for output_item in model_graph_signature.outputs.items():
        output_op_name = output_item[0]
        output_tensor_name = output_item[1].name
        output_tensor_dict[output_op_name] = output_tensor_name

    for name in output_op_names:
        output_feed.append(output_tensor_dict[name])
        print(output_tensor_dict[name])
    print("load model finish!")

    data_dir = FLAGS.data_dir
    decoder_size = encoder_size = FLAGS.max_sequence_length

    # Create vocabularies of the appropriate sizes.
    vocab_path = os.path.join(data_dir, "in_vocab.txt")
    tag_vocab_path = os.path.join(data_dir, "out_vocab.txt")
    label_vocab_path = os.path.join(data_dir, "label.txt")

    vocab, rev_vocab = data_utils.initialize_vocab(vocab_path)
    tag_vocab, rev_tag_vocab = data_utils.initialize_vocab(tag_vocab_path)
    label_vocab, rev_label_vocab = data_utils.initialize_vocab(
        label_vocab_path)

    UNK_ID = data_utils.UNK_ID_dict['with_padding']

    while True:

        string = input("请输入测试句子: ").strip()

        line = ' '.join(list(string))

        encoder_input = data_utils.sentence_to_token_ids(
            line, vocab, UNK_ID, data_utils.naive_tokenizer, False)
        # Encoder inputs are padded and then reversed.
        encoder_pad = [data_utils.PAD_ID] * (encoder_size - len(encoder_input))
        # encoder_inputs.append(list(reversed(encoder_input + encoder_pad)))
        encoder_inputs = [
            np.array(input) for input in encoder_input + encoder_pad
        ]

        tag_weights = []
        # Batch decoder inputs are re-indexed decoder_inputs, we create weights.
        for length_idx in xrange(decoder_size):
            # Create target_weights to be 0 for targets that are padding.
            tag_weight = np.ones(1, dtype=np.float32)
            if encoder_inputs[length_idx] == data_utils.PAD_ID:
                tag_weight = np.zeros(1, dtype=np.float32)
            tag_weights.append(tag_weight)

        inputs = {}
        inputs['sequence_length'] = np.array([len(encoder_input)],
                                             dtype=np.int32)
        for l in range(encoder_size):
            print(encoder_inputs[l])
            inputs['encoder_input:%d' % l] = [encoder_inputs[l]]
            inputs['tag_weight:%d' % l] = tag_weights[l]

        feed_dict = {}
        for input_item in model_graph_signature.inputs.items():
            input_op_name = input_item[0]
            input_tensor_name = input_item[1].name
            feed_dict[input_tensor_name] = inputs[input_op_name]

        outputs = session.run(output_feed, feed_dict=feed_dict)

        tagging_logits, class_logits = outputs[:len(encoder_input
                                                    )], outputs[-1]

        tags = [rev_tag_vocab[np.argmax(x)] for x in tagging_logits]

        label = rev_label_vocab[np.argmax(class_logits)]

        result = result_to_json(string, tags, label)

        print(result)
Example #9
0
def train():
    tf.logging.info('Applying Parameters:')
    tf.logging.info("Preparing data in %s" % FLAGS.data_dir)
    nowTime = datetime.datetime.now().strftime('%Y_%m_%d_%H_%M_%S')
    tf.logging.set_verbosity(tf.logging.INFO)
    handlers = [
        logging.FileHandler(os.path.join(FLAGS.log, nowTime + '.log')),
        logging.StreamHandler(sys.stdout)
    ]
    logging.getLogger('tensorflow').handlers = handlers

    date_set = data_utils.prepare_multi_task_data(FLAGS.data_dir)
    in_seq_train, out_seq_train, label_train = date_set[0]
    in_seq_dev, out_seq_dev, label_dev = date_set[1]
    in_seq_test, out_seq_test, label_test = date_set[2]
    vocab_path, tag_vocab_path, label_vocab_path = date_set[3]

    result_dir = FLAGS.train_dir + '/test_results'
    if not tf.gfile.IsDirectory(result_dir):
        tf.gfile.MakeDirs(result_dir)

    current_taging_valid_out_file = result_dir + '/tagging.valid.hyp.txt'
    current_taging_test_out_file = result_dir + '/tagging.test.hyp.txt'

    if not tf.gfile.Exists('data_bak/vocab.json') or not tf.gfile.Exists(
            'data_bak/rev_vocab.json'):
        vocab, rev_vocab = data_utils.initialize_vocab(vocab_path)
        with tf.gfile.GFile('data_bak/vocab.json',
                            'w') as vocab_file, tf.gfile.GFile(
                                'data_bak/rev_vocab.json',
                                'w') as rev_vocab_file:
            vocab_file.write(json.dumps(vocab, ensure_ascii=False, indent=4))
            rev_vocab_file.write(
                json.dumps(rev_vocab, ensure_ascii=False, indent=4))
    else:
        with tf.gfile.GFile('data_bak/vocab.json',
                            'r') as vocab_file, tf.gfile.GFile(
                                'data_bak/rev_vocab.json',
                                'r') as rev_vocab_file:
            vocab = json.load(vocab_file)
            rev_vocab = seq.json(rev_vocab_file).map(
                lambda x: (int(x[0]), x[1])).to_dict()

    if not tf.gfile.Exists('data_bak/tag_vocab.json') or not tf.gfile.Exists(
            'data_bak/rev_tag_vocab.json'):
        tag_vocab, rev_tag_vocab = data_utils.initialize_vocab(tag_vocab_path)
        with tf.gfile.GFile('data_bak/tag_vocab.json', 'w') as tag_vocab_file, \
                tf.gfile.GFile('data_bak/rev_tag_vocab.json', 'w') as rev_tag_vocab_file:
            tag_vocab_file.write(
                json.dumps(tag_vocab, ensure_ascii=False, indent=4))
            rev_tag_vocab_file.write(
                json.dumps(rev_tag_vocab, ensure_ascii=False, indent=4))
    else:
        with tf.gfile.GFile('data_bak/tag_vocab.json',
                            'r') as tag_vocab_file, tf.gfile.GFile(
                                'data_bak/rev_tag_vocab.json',
                                'r') as rev_tag_vocab_file:
            tag_vocab = json.load(tag_vocab_file)
            rev_tag_vocab = seq.json(rev_tag_vocab_file).map(
                lambda x: (int(x[0]), x[1])).to_dict()

    if not tf.gfile.Exists('data_bak/label_vocab.json') or not tf.gfile.Exists(
            'data_bak/rev_label_vocab.json'):
        label_vocab, rev_label_vocab = data_utils.initialize_vocab(
            label_vocab_path)
        with tf.gfile.GFile('data_bak/label_vocab.json', 'w') as label_vocab_file, \
                tf.gfile.GFile('data_bak/rev_label_vocab.json', 'w') as rev_label_vocab_file:
            label_vocab_file.write(
                json.dumps(label_vocab, ensure_ascii=False, indent=4))
            rev_label_vocab_file.write(
                json.dumps(rev_label_vocab, ensure_ascii=False, indent=4))
    else:
        with tf.gfile.GFile('data_bak/label_vocab.json',
                            'r') as label_vocab_file, tf.gfile.GFile(
                                'data_bak/rev_label_vocab.json',
                                'r') as rev_label_vocab_file:
            label_vocab = json.load(label_vocab_file)
            rev_label_vocab = seq.json(rev_label_vocab_file).map(
                lambda x: (int(x[0]), x[1])).to_dict()

    # Read data into buckets and compute their sizes.
    tf.logging.info("Reading train/valid/test data (training set limit: %d)." %
                    FLAGS.max_train_data_size)
    dev_set = read_data(in_seq_dev, out_seq_dev, label_dev)
    test_set = read_data(in_seq_test, out_seq_test, label_test)
    train_set = read_data(in_seq_train, out_seq_train, label_train)
    train_bucket_sizes = [len(train_set[b]) for b in xrange(len(_buckets))]
    train_total_size = float(sum(train_bucket_sizes))

    config = tf.ConfigProto(
        gpu_options=tf.GPUOptions(per_process_gpu_memory_fraction=0.23),
        # device_count = {'gpu': 2}
    )

    with tf.Session(config=config) as sess:
        # Create model.
        tf.logging.info("Max sequence length: %d." % _buckets[0][0])
        tf.logging.info("Creating %d layers of %d units." %
                        (FLAGS.num_layers, FLAGS.size))

        model, model_test = create_model(sess, len(vocab), len(tag_vocab),
                                         len(label_vocab))
        tf.logging.info("Creating model with " +
              "source_vocab_size=%d, target_vocab_size=%d, label_vocab_size=%d." \
              % (len(vocab), len(tag_vocab), len(label_vocab)))

        tf.summary.scalar('loss', model.loss)
        tf.summary.scalar('dev_accuracy', model.best_dev_accuracy)
        tf.summary.scalar('dev_f1', model.best_dev_f1)
        tf.summary.scalar('test_accuracy', model.best_test_accuracy)
        tf.summary.scalar('test_f1', model.best_test_f1)

        model.merged = tf.summary.merge_all()
        model.writer = tf.summary.FileWriter(
            os.path.join(FLAGS.tensorboard, nowTime))
        model.writer.add_graph(graph=sess.graph)

        train_buckets_scale = [
            sum(train_bucket_sizes[:i + 1]) / train_total_size
            for i in xrange(len(train_bucket_sizes))
        ]

        # This is the training loop.
        step_time, loss = 0.0, 0.0
        current_step = 0

        no_improve_step = 0
        while model.global_step.eval() < FLAGS.max_training_steps:
            random_number_01 = np.random.random_sample()
            bucket_id = min([
                i for i in xrange(len(train_buckets_scale))
                if train_buckets_scale[i] > random_number_01
            ])

            # Get a batch and make a step.
            start_time = time.time()
            batch_data = model.get_batch(train_set, bucket_id)
            encoder_inputs, tags, tag_weights, batch_sequence_length, labels = batch_data
            if task['joint'] == 1:
                step_outputs = model.joint_step(sess, encoder_inputs, tags,
                                                tag_weights, labels,
                                                batch_sequence_length,
                                                bucket_id, False)
                _, step_loss, tagging_logits, class_logits = step_outputs
            elif task['tagging'] == 1:
                step_outputs = model.tagging_step(sess, encoder_inputs, tags,
                                                  tag_weights,
                                                  batch_sequence_length,
                                                  bucket_id, False)
                _, step_loss, tagging_logits = step_outputs
            elif task['intent'] == 1:
                step_outputs = model.classification_step(
                    sess, encoder_inputs, labels, batch_sequence_length,
                    bucket_id, False)
                _, step_loss, class_logits = step_outputs

            summary = sess.run(model.merged, model.input_feed)
            model.writer.add_summary(summary, model.global_step.eval())

            step_time += (time.time() -
                          start_time) / FLAGS.steps_per_checkpoint
            loss += step_loss / FLAGS.steps_per_checkpoint
            current_step += 1

            # Once in a while, we save checkpoint, print statistics, and run evals.
            if current_step % FLAGS.steps_per_checkpoint == 0:
                perplexity = math.exp(loss) if loss < 300 else float('inf')
                tf.logging.info(
                    "global step %d step-time %.2f. Training perplexity %.2f" %
                    (model.global_step.eval(), step_time, perplexity))
                sys.stdout.flush()
                # Save checkpoint and zero timer and loss.
                checkpoint_path = os.path.join(FLAGS.train_dir, "model.ckpt")
                step_time, loss = 0.0, 0.0

                def run_valid_test(data_set, mode):  # mode: Eval, Test
                    # Run evals on development/test set and print the accuracy.
                    word_list = list()
                    ref_tag_list = list()
                    hyp_tag_list = list()
                    ref_label_list = list()
                    hyp_label_list = list()
                    correct_count = 0
                    accuracy = 0.0
                    tagging_eval_result = dict()
                    for bucket_id in xrange(len(_buckets)):
                        eval_loss = 0.0
                        count = 0
                        for i in xrange(len(data_set[bucket_id])):
                            count += 1
                            sample = model_test.get_one(data_set, bucket_id, i)
                            encoder_inputs, tags, tag_weights, sequence_length, labels = sample
                            tagging_logits = []
                            class_logits = []
                            if task['joint'] == 1:
                                step_outputs = model_test.joint_step(
                                    sess, encoder_inputs, tags, tag_weights,
                                    labels, sequence_length, bucket_id, True)
                                _, step_loss, tagging_logits, class_logits = step_outputs
                            elif task['tagging'] == 1:
                                step_outputs = model_test.tagging_step(
                                    sess, encoder_inputs, tags, tag_weights,
                                    sequence_length, bucket_id, True)
                                _, step_loss, tagging_logits = step_outputs
                            elif task['intent'] == 1:
                                step_outputs = model_test.classification_step(
                                    sess, encoder_inputs, labels,
                                    sequence_length, bucket_id, True)
                                _, step_loss, class_logits = step_outputs
                            eval_loss += step_loss / len(data_set[bucket_id])
                            hyp_label = None
                            if task['intent'] == 1:
                                ref_label_list.append(
                                    rev_label_vocab[labels[0][0]])
                                hyp_label = np.argmax(class_logits[0], 0)
                                hyp_label_list.append(
                                    rev_label_vocab[hyp_label])
                                if labels[0] == hyp_label:
                                    correct_count += 1
                            if task['tagging'] == 1:
                                word_list.append([rev_vocab[x[0]] for x in \
                                                  encoder_inputs[:sequence_length[0]]])
                                ref_tag_list.append([rev_tag_vocab[x[0]] for x in \
                                                     tags[:sequence_length[0]]])
                                hyp_tag_list.append(
                                    [rev_tag_vocab[np.argmax(x)] for x in \
                                     tagging_logits[:sequence_length[0]]])

                    accuracy = float(correct_count) * 100 / count
                    if task['intent'] == 1:
                        tf.logging.info("\t%s accuracy: %.2f %d/%d" \
                              % (mode, accuracy, correct_count, count))
                        sys.stdout.flush()
                    if task['tagging'] == 1:
                        if mode == 'Eval':
                            taging_out_file = current_taging_valid_out_file
                        elif mode == 'Test':
                            taging_out_file = current_taging_test_out_file
                        tagging_eval_result = conlleval(
                            hyp_tag_list, ref_tag_list, word_list,
                            taging_out_file)
                        tf.logging.info("\t%s f1-score: %.2f" %
                                        (mode, tagging_eval_result['f1']))
                        sys.stdout.flush()
                    return accuracy, tagging_eval_result

                # valid
                valid_accuracy, valid_tagging_result = run_valid_test(
                    dev_set, 'Eval')
                if task['tagging'] == 1 and task['intent'] == 0:
                    best_dev_f1 = model.best_dev_f1.eval()
                    if valid_tagging_result['f1'] > best_dev_f1:
                        tf.assign(model.best_dev_f1,
                                  valid_tagging_result['f1']).eval()
                        # save the best output file
                        subprocess.call(['mv',
                                         current_taging_valid_out_file,
                                         current_taging_valid_out_file + '.best_f1_%.2f' \
                                         % best_dev_f1], shell=True)
                        model.saver.save(sess,
                                         checkpoint_path,
                                         global_step=model.global_step)
                        no_improve_step = 0
                    else:
                        no_improve_step += 1

                if task['tagging'] == 1 and task['intent'] == 1:
                    best_dev_accuracy = model.best_dev_accuracy.eval()
                    best_dev_f1 = model.best_dev_f1.eval()
                    if valid_accuracy > best_dev_accuracy and valid_tagging_result[
                            'f1'] > best_dev_f1:
                        tf.assign(model.best_dev_accuracy,
                                  valid_accuracy).eval()
                        tf.assign(model.best_dev_f1,
                                  valid_tagging_result['f1']).eval()
                        subprocess.call(['mv',
                                         current_taging_valid_out_file,
                                         current_taging_valid_out_file + '.best_f1_%.2f' \
                                         % best_dev_f1], shell=True)
                        model.saver.save(sess,
                                         checkpoint_path,
                                         global_step=model.global_step)
                        no_improve_step = 0
                    else:
                        no_improve_step += 1

                # test, run test after each validation for development purpose.
                test_accuracy, test_tagging_result = run_valid_test(
                    test_set, 'Test')
                if task['tagging'] == 1 and task['intent'] == 0:
                    best_test_f1 = model.best_test_f1.eval()
                    if test_tagging_result['f1'] > best_test_f1:
                        tf.assign(model.best_test_f1,
                                  test_tagging_result['f1']).eval()
                    # save the best output file
                    subprocess.call(['mv',
                                     current_taging_test_out_file,
                                     current_taging_test_out_file + '.best_f1_%.2f' \
                                     % best_test_f1], shell=True)

                if task['tagging'] == 1 and task['intent'] == 1:
                    best_test_accuracy = model.best_test_accuracy.eval()
                    best_test_f1 = model.best_test_f1.eval()
                    if test_accuracy > best_test_accuracy and test_tagging_result[
                            'f1'] > best_test_f1:
                        tf.assign(model.best_test_accuracy,
                                  test_accuracy).eval()
                        tf.assign(model.best_test_f1,
                                  test_tagging_result['f1']).eval()
                        subprocess.call(['mv',
                                         current_taging_test_out_file,
                                         current_taging_test_out_file + '.best_f1_%.2f' \
                                         % best_test_f1], shell=True)

                if no_improve_step > FLAGS.no_improve_per_step:
                    tf.logging.info("continuous no improve per step " +
                                    str(FLAGS.no_improve_per_step) +
                                    ", auto stop...")
                    tf.logging.info("max accuracy is: " +
                                    str(model.best_dev_accuracy.eval()) +
                                    ", max f1 score is: " +
                                    str(model.best_dev_f1.eval()))
                    break
def train():
    print('应用参数:')
    for k, v in FLAGS.__dict__['__flags'].items():
        print('%s: %s' % (k, str(v)))

    print("准备数据 %s" % FLAGS.data_dir)
    vocab_path = ''
    tag_vocab_path = ''
    label_vocab_path = ''
    date_set = data_utils.prepare_multi_task_data(
        FLAGS.data_dir, FLAGS.in_vocab_size, FLAGS.out_vocab_size)
    in_seq_train, out_seq_train, label_train = date_set[0]
    in_seq_dev, out_seq_dev, label_dev = date_set[1]
    in_seq_test, out_seq_test, label_test = date_set[2]
    vocab_path, tag_vocab_path, label_vocab_path = date_set[3]

    result_dir = FLAGS.train_dir + '/test_results'
    if not os.path.isdir(result_dir):
        os.makedirs(result_dir)

    current_tagging_valid_out_file = result_dir + '/tagging.valid.hyp.txt'
    current_tagging_test_out_file = result_dir + '/tagging.test.hyp.txt'

    vocab, rev_vocab = data_utils.initialize_vocab(vocab_path)
    tag_vocab, rev_tag_vocab = data_utils.initialize_vocab(tag_vocab_path)
    label_vocab, rev_label_vocab = data_utils.initialize_vocab(label_vocab_path)

    config = tf.ConfigProto(
        gpu_options=tf.GPUOptions(per_process_gpu_memory_fraction=0.23),
        # device_count = {'gpu': 2}
    )

    with tf.Session(config=config) as sess:
        # 创建模型。
        print("最大序列长度: %d." % _buckets[0][0])
        print("创建%d单元的%d层。" % (FLAGS.num_layers, FLAGS.size))

        model, model_test = create_model(sess,
                                         len(vocab),
                                         len(tag_vocab),
                                         len(label_vocab))
        print("创建模型 " +
              "source_vocab_size=%d, target_vocab_size=%d, label_vocab_size=%d."
              % (len(vocab), len(tag_vocab), len(label_vocab)))

        # 将数据读入桶中并计算桶的大小。
        print("读取 train/valid/test 数据 (训练集范围: %d)."
              % FLAGS.max_train_data_size)
        dev_set = read_data(in_seq_dev, out_seq_dev, label_dev)
        test_set = read_data(in_seq_test, out_seq_test, label_test)
        train_set = read_data(in_seq_train, out_seq_train, label_train)
        train_bucket_sizes = [len(train_set[b]) for b in xrange(len(_buckets))]
        train_total_size = float(sum(train_bucket_sizes))

        train_buckets_scale = [sum(train_bucket_sizes[:i + 1]) / train_total_size
                               for i in xrange(len(train_bucket_sizes))]

        # 这是一个训练循环。
        step_time, loss = 0.0, 0.0
        current_step = 0

        best_valid_score = 0
        best_test_score = 0
        while model.global_step.eval() < FLAGS.max_training_steps:
            random_number_01 = np.random.random_sample()
            bucket_id = min([i for i in xrange(len(train_buckets_scale))
                             if train_buckets_scale[i] > random_number_01])

            # 获取一个分支并执行一步
            start_time = time.time()
            batch_data = model.get_batch(train_set, bucket_id)
            encoder_inputs, tags, tag_weights, batch_sequence_length, labels = batch_data
            if task['joint'] == 1:
                step_outputs = model.joint_step(sess,
                                                encoder_inputs,
                                                tags,
                                                tag_weights,
                                                labels,
                                                batch_sequence_length,
                                                bucket_id,
                                                False)
                _, step_loss, tagging_logits, class_logits = step_outputs
            elif task['tagging'] == 1:
                step_outputs = model.tagging_step(sess,
                                                  encoder_inputs,
                                                  tags,
                                                  tag_weights,
                                                  batch_sequence_length,
                                                  bucket_id,
                                                  False)
                _, step_loss, tagging_logits = step_outputs
            elif task['intent'] == 1:
                step_outputs = model.classification_step(sess,
                                                         encoder_inputs,
                                                         labels,
                                                         batch_sequence_length,
                                                         bucket_id,
                                                         False)
                _, step_loss, class_logits = step_outputs

            step_time += (time.time() - start_time) / FLAGS.steps_per_checkpoint
            loss += step_loss / FLAGS.steps_per_checkpoint
            current_step += 1

            # 有时,我们保存检查点、打印统计数据并运行evals。
            if current_step % FLAGS.steps_per_checkpoint == 0:
                perplexity = math.exp(loss) if loss < 300 else float('inf')
                print("全局步数 %d 每步时间 %.2fs 训练复杂度 %.2f"
                      % (model.global_step.eval(), step_time, perplexity))
                sys.stdout.flush()
                # 保存检查点和零计时器和损失。
                checkpoint_path = os.path.join(FLAGS.train_dir, "model.ckpt")
                model.saver.save(sess, checkpoint_path, global_step=model.global_step)
                step_time, loss = 0.0, 0.0

                # 模式: Eval, Test
                def run_valid_test(data_set, mode):
                    # 在开发/测试集上运行evals并打印准确性。
                    word_list = list()
                    ref_tag_list = list()
                    hyp_tag_list = list()
                    ref_label_list = list()
                    hyp_label_list = list()
                    correct_count = 0
                    accuracy = 0.0
                    tagging_eval_result = dict()
                    eval_loss = 0.0
                    count = 0
                    for bucket_id in xrange(len(_buckets)):
                        for i in xrange(len(data_set[bucket_id])):
                            count += 1
                            sample = model_test.get_one(data_set, bucket_id, i)
                            encoder_inputs, tags, tag_weights, sequence_length, labels = sample
                            tagging_logits = []
                            class_logits = []

                            step_loss = None
                            if task['joint'] == 1:
                                step_outputs = model_test.joint_step(sess,
                                                                     encoder_inputs,
                                                                     tags,
                                                                     tag_weights,
                                                                     labels,
                                                                     sequence_length,
                                                                     bucket_id,
                                                                     True)
                                _, step_loss, tagging_logits, class_logits = step_outputs
                            elif task['tagging'] == 1:
                                step_outputs = model_test.tagging_step(sess,
                                                                       encoder_inputs,
                                                                       tags,
                                                                       tag_weights,
                                                                       sequence_length,
                                                                       bucket_id,
                                                                       True)
                                _, step_loss, tagging_logits = step_outputs
                            elif task['intent'] == 1:
                                step_outputs = model_test.classification_step(sess,
                                                                              encoder_inputs,
                                                                              labels,
                                                                              sequence_length,
                                                                              bucket_id,
                                                                              True)
                                _, step_loss, class_logits = step_outputs
                            eval_loss += step_loss / len(data_set[bucket_id])
                            hyp_label = None
                            if task['intent'] == 1:
                                ref_label_list.append(rev_label_vocab[labels[0][0]])
                                hyp_label = np.argmax(class_logits[0], 0)
                                hyp_label_list.append(rev_label_vocab[hyp_label])
                                if labels[0] == hyp_label:
                                    correct_count += 1
                            if task['tagging'] == 1:
                                word_list.append([rev_vocab[x[0]] for x in
                                                  encoder_inputs[:sequence_length[0]]])
                                ref_tag_list.append([rev_tag_vocab[x[0]] for x in
                                                     tags[:sequence_length[0]]])
                                hyp_tag_list.append(
                                    [rev_tag_vocab[np.argmax(x)] for x in
                                     tagging_logits[:sequence_length[0]]])

                    accuracy = float(correct_count) * 100 / count
                    if task['intent'] == 1:
                        print("  %s 准确性: %.2f%% %d/%d"
                              % (mode, accuracy, correct_count, count))
                        sys.stdout.flush()
                    if task['tagging'] == 1:
                        taging_out_file = None
                        if mode == 'Eval':
                            taging_out_file = current_tagging_valid_out_file
                        elif mode == 'Test':
                            taging_out_file = current_tagging_test_out_file
                        tagging_eval_result = conlleval(hyp_tag_list,
                                                        ref_tag_list,
                                                        word_list,
                                                        taging_out_file)
                        print("  %s f1-score: %.2f%%" % (mode, tagging_eval_result['f1']))
                        sys.stdout.flush()
                    return accuracy, tagging_eval_result

                # valid
                valid_accuracy, valid_tagging_result = run_valid_test(dev_set, 'Eval')
                if task['tagging'] == 1 \
                        and valid_tagging_result['f1'] > best_valid_score:
                    best_valid_score = valid_tagging_result['f1']
                    # 保存最好的输出文件
                    subprocess.call(['mv',
                                     current_tagging_valid_out_file,
                                     current_tagging_valid_out_file + '.best_f1_%.2f'
                                     % best_valid_score])
                # 测试,在每个验证后运行测试,以供开发之用。
                test_accuracy, test_tagging_result = run_valid_test(test_set, 'Test')
                if task['tagging'] == 1 \
                        and test_tagging_result['f1'] > best_test_score:
                    best_test_score = test_tagging_result['f1']
                    # 保存最好的输出文件
                    subprocess.call(['mv',
                                     current_tagging_test_out_file,
                                     current_tagging_test_out_file + '.best_f1_%.2f'
                                     % best_test_score])
Example #11
0
    def feed_sentence(self, sentence, raw=True):
        data_set = [[[]]]
        token_ids = data_utils.prepare_one_data(FLAGS.data_dir,
                                                FLAGS.in_vocab_size, sentence)
        slot_ids = [0 for i in range(len(token_ids))]
        data_set[0][0].append(token_ids)
        data_set[0][0].append(slot_ids)
        data_set[0][0].append([0])
        encoder_inputs, tags, tag_weights, sequence_length, labels = self.model_test.get_one(
            data_set, 0, 0)
        if task['joint'] == 1:
            _, _, tagging_logits, classification_logits = self.model_test.joint_step(
                self.sess, encoder_inputs, tags, tag_weights, labels,
                sequence_length, 0, True)

        classification = [
            np.argmax(classification_logit)
            for classification_logit in classification_logits
        ]
        tagging_logit = [
            np.argmax(tagging_logit) for tagging_logit in tagging_logits
        ]

        out_vocab_path = os.path.join(
            FLAGS.data_dir, "out_vocab_%d.txt" % FLAGS.out_vocab_size)
        label_path = os.path.join(FLAGS.data_dir, "label.txt")
        out_vocab, _ = data_utils.initialize_vocab(out_vocab_path)
        label_vocab, _ = data_utils.initialize_vocab(label_path)

        def inverse_lookup(vocab, id):
            for key, value in vocab.items():
                if value == id:
                    return key

        classification_word = [
            inverse_lookup(label_vocab, c) for c in classification
        ]
        tagging_word = [
            inverse_lookup(out_vocab, t)
            for t in tagging_logit[:len(sentence.split())]
        ]
        if raw:
            return classification_word, tagging_word[:len(sentence.split())]
        else:
            sentence = sentence.split()
            singer_name = ''
            singer_flag = False
            song_name = ''
            song_flag = False
            album_name = ''
            album_flag = False
            for i, word in enumerate(tagging_word):
                if word == 'B-song':
                    song_flag = True
                elif word == 'B-singer':
                    singer_flag = True
                elif word == 'B-album':
                    album_flag = True
                if song_flag:
                    song_name = song_name + sentence[i] + ' '
                elif singer_flag:
                    singer_name = singer_name + sentence[i] + ' '
                elif album_flag:
                    album_name = album_name + sentence[i] + ' '
                if word == 'O':
                    song_flag = False
                    singer_flag = False
                    album_flag = False
            return classification_word, [singer_name, song_name, album_name]
Example #12
0
def chatbot():

    print('Applying Parameters:')
    for k, v in FLAGS.__dict__['__flags'].items():
        print('%s: %s' % (k, str(v)))
    print('\n')

    vocab_path = os.path.join(FLAGS.data_dir,
                              "in_vocab_%d.txt" % FLAGS.in_vocab_size)
    tag_vocab_path = os.path.join(FLAGS.data_dir,
                                  "out_vocab_%d.txt" % FLAGS.out_vocab_size)
    label_vocab_path = os.path.join(FLAGS.data_dir, "label.txt")

    vocab, rev_vocab = data_utils.initialize_vocab(vocab_path)
    tag_vocab, rev_tag_vocab = data_utils.initialize_vocab(tag_vocab_path)
    label_vocab, rev_label_vocab = data_utils.initialize_vocab(
        label_vocab_path)

    with tf.Session() as sess:

        # Create model.
        print("\nLoading a model...")
        _, model_pred = create_model(sess, len(vocab), len(tag_vocab),
                                     len(label_vocab))
        print ("Created model with " +
               "source_vocab_size=%d, target_vocab_size=%d, label_vocab_size=%d." \
               % (len(vocab), len(tag_vocab), len(label_vocab)))
        print("\n")

        # Begin chatbot for airline company client service
        print("Welcome to Alma airline customer service!!")
        print(
            "I can only understand up to 50 words... Yes, I'm still learning :)\n"
        )

        # Create vocabularies of the appropriate sizes.
        '''
    user_input = "i want to fly from baltimore to dallas round trip"
    ref_tag = "O O O O O B-fromloc.city_name O B-toloc.city_name B-round_trip I-round_trip"
    ref_class = "atis_flight"
    print("\noriginal input: %s" % user_input)
    print("tagging: ", list(zip(user_input.split(" "),ref_tag.split(" "))))
    print("intent: %s\n" % ref_class)
    '''

        while True:
            user_input = _get_user_input()
            if len(user_input) > 0 and user_input[-1] == '\n':
                user_input = user_input[:-1]
            if user_input == '':
                break

            _, user_input_to_model, input_token_ids = \
              data_utils.user_input_to_token_ids(user_input, vocab_path)
            print("input to the model: %s" % user_input_to_model)
            #print("tokenized input:", input_token_ids)

            if (len(input_token_ids) > FLAGS.max_sequence_length):
                print('Max length I can handle is:', FLAGS.max_sequence_length)
                user_input = _get_user_input()
                continue

            for bucket_id, (source_size, target_size) in enumerate(_buckets):
                if len(input_token_ids) < source_size:
                    input_bucket_id = bucket_id
                    break
            #print("input bucket id: %d\n" % input_bucket_id)

            sample = model_pred.get_batch_format(input_token_ids,
                                                 input_bucket_id)
            encoder_inputs, sequence_length = sample
            #print("encoder_inputs: ", encoder_inputs)
            #print("sequence_length: ", sequence_length)

            if task['joint'] == 1:
                pred_outputs = model_pred.joint_pred(sess, encoder_inputs,
                                                     sequence_length,
                                                     input_bucket_id)
                tagging_logits, class_logits = pred_outputs
                class_prob = _softmax(class_logits[0])
                #print(class_logits[0])
                #print(class_prob)
            else:
                sys.exit("Current only support joint model")

            #print("tagging_logits: ", tagging_logits)
            #print("class_logits: ", class_logits)

            if task['intent'] == 1:
                hyp_label = np.argmax(class_logits[0], 0)
                intent_pred = rev_label_vocab[hyp_label]
                print("Predicted intents: %s" % (intent_pred))
                pred_labels = np.argsort(class_prob)[-3:]
                intent_preds = [rev_label_vocab[l] for l in pred_labels]
                print("Top 3 predicted intents:")
                for idx in reversed(pred_labels):
                    print("%s (%.4f)" %
                          (rev_label_vocab[idx], class_prob[idx]))

            if task['tagging'] == 1:
                tag_pred = [rev_tag_vocab[np.argmax(x)] for x in \
                              tagging_logits[:sequence_length[0]]]
                print("predicted tag:", tag_pred)
                entity_dict = _extract_entity(tag_pred,
                                              user_input_to_model.split(" "))
                print("extraged entity:")
                pprint(entity_dict)
                print("\n")