Esempio n. 1
0
def predict(logit):
    # load data
    print_log('Loading testing data ...', file=log)
    start_time = time.time()
    premise_test, premise_mask_test, hypothesis_test, hypothesis_mask_test, y_test = sentence2Index(
        arg.testset_path, vocab_dict)
    batches = next_batch(premise_test,
                         premise_mask_test,
                         hypothesis_test,
                         hypothesis_mask_test,
                         y_test,
                         shuffle=False)
    time_diff = get_time_diff(start_time)
    print_log('Time usage : ', time_diff, file=log)

    # testing
    print_log('Start testing ...', file=log)
    start_time = time.time()
    y_pred = []
    for batch in batches:
        batch_premise_test, batch_premise_mask_test, batch_hypothesis_test, batch_hypothesis_mask_test, _ = batch
        feed_dict = {
            premise: batch_premise_test,
            # model.premise_mask: batch_premise_mask_test,
            hypothesis: batch_hypothesis_test,
            # model.hypothesis_mask: batch_hypothesis_mask_test,
            dropout_keep_prob: 1.0
        }
        logits = sess.run([logit], feed_dict=feed_dict)
        logits = np.array(logits)
        logits = logits.reshape([-1, logits.shape[-1]])
        y_pred.extend(logits)
    # evaluating
    y_pred = np.argmax(y_pred, 1)
    y_true = np.argmax(y_test, 1)
    f1 = f1_score(y_true, y_pred, average='weighted', labels=np.unique(y_true))
    acc = np.mean(y_true == y_pred)
    for id in range(len(y_true)):
        if y_true[id] != y_pred[id]:
            premise_text = ''.join([
                index2word[idx] + ' ' for idx in premise_test[id]
                if index2word[idx] != '<PAD>'
            ])
            hypothesis_text = ''.join([
                index2word[idx] + ' ' for idx in hypothesis_test[id]
                if index2word[idx] != '<PAD>'
            ])
            print('Left_text: {0}/ Right_text: {1}'.format(
                premise_text, hypothesis_text))
            print('The true label is {0}/ The pred label is {1}'.format(
                y_true[id], y_pred[id]))
    print('The test accuracy: {0:>6.2%}'.format(acc))
    print('The test F1: {0:>6.4}'.format(f1))
    time_diff = get_time_diff(start_time)
    print('Time usage: ', time_diff, '\n')
Esempio n. 2
0
    # read config
    config = Config.ModelConfig()
    arg = config.arg

    vocab_dict = load_vocab(arg.vocab_path)
    arg.vocab_dict_size = len(vocab_dict)
    index2word = {index: word for word, index in vocab_dict.items()}

    if arg.embedding_path:
        embeddings = load_embeddings(arg.embedding_path, vocab_dict)
    else:
        embeddings = init_embeddings(vocab_dict, arg.embedding_size)
    arg.n_vocab, arg.embedding_size = embeddings.shape

    if arg.embedding_normalize:
        embeddings = normalize_embeddings(embeddings)

    arg.n_classes = len(CATEGORIE_ID)

    dt = datetime.now().strftime("%Y_%m_%d_%H_%M_%S")
    arg.log_path = 'config/log.{}'.format(dt)
    log = open(arg.log_path, 'w')
    print_log('CMD : python3 {0}'.format(' '.join(sys.argv)), file=log)
    print_log('Testing with following options :', file=log)
    print_args(arg, log)

    model = ESIM(arg.seq_length, arg.n_vocab, arg.embedding_size, arg.hidden_size, arg.attention_size, arg.n_classes, \
                 arg.batch_size, arg.learning_rate, arg.optimizer, arg.l2, arg.clip_value, arg)
    predict()
    log.close()
Esempio n. 3
0
def train():
    # load data
    print_log('Loading training and validation data ...', file=log)
    start_time = time.time()
    premise_train, premise_mask_train, hypothesis_train, hypothesis_mask_train, y_train = sentence2Index(
        arg.trainset_path, vocab_dict)
    premise_dev, premise_mask_dev, hypothesis_dev, hypothesis_mask_dev, y_dev = sentence2Index(
        arg.devset_path, vocab_dict)
    data_nums = len(premise_train)
    time_diff = get_time_diff(start_time)
    print_log('Time usage : ', time_diff, file=log)

    # model saving
    saver = tf.train.Saver(max_to_keep=5)
    save_file_dir, save_file_name = os.path.split(arg.save_path)
    if not os.path.exists(save_file_dir):
        os.makedirs(save_file_dir)

    # for TensorBoard
    print_log('Configuring TensorBoard and Saver ...', file=log)
    if not os.path.exists(arg.tfboard_path):
        os.makedirs(arg.tfboard_path)
    tf.summary.scalar('train_loss', model.loss)
    tf.summary.scalar('train_accuracy', model.acc)
    merged_summary = tf.summary.merge_all()
    writer = tf.summary.FileWriter(arg.tfboard_path)

    # init
    sess = tf.Session()
    sess.run(tf.global_variables_initializer(),
             {model.embed_matrix: embeddings})

    # count trainable parameters
    total_parameters = count_parameters()
    print_log('Total trainable parameters : {}'.format(total_parameters),
              file=log)

    # training
    print_log('Start training and evaluating ...', file=log)
    start_time = time.time()
    total_batch = 0
    best_acc_val = 0.0
    last_improved_batch = 0
    isEarlyStop = False
    for epoch in range(arg.num_epochs):
        print_log('Epoch : ', epoch + 1, file=log)
        batches = next_batch(premise_train,
                             premise_mask_train,
                             hypothesis_train,
                             hypothesis_mask_train,
                             y_train,
                             batchSize=arg.batch_size)
        total_loss, total_acc = 0.0, 0.0
        for batch in batches:
            batch_nums = len(batch[0])
            feed_dict = feed_data(*batch, arg.dropout_keep_prob)
            _, batch_loss, batch_acc = sess.run(
                [model.train, model.loss, model.acc], feed_dict=feed_dict)
            total_loss += batch_loss * batch_nums
            total_acc += batch_acc * batch_nums

            # evaluta on devset
            if total_batch % arg.eval_batch == 0:
                # write tensorboard scalar
                s = sess.run(merged_summary, feed_dict=feed_dict)
                writer.add_summary(s, total_batch)

                feed_dict[model.dropout_keep_prob] = 1.0
                loss_val, acc_val = evaluate(sess, premise_dev,
                                             premise_mask_dev, hypothesis_dev,
                                             hypothesis_mask_dev, y_dev)
                tf.summary.scalar('test_loss', loss_val)
                tf.summary.scalar('test_accuracy', acc_val)
                merged_summary = tf.summary.merge_all()
                writer = tf.summary.FileWriter(arg.tfboard_path)
                # save model
                saver.save(sess=sess,
                           save_path=arg.save_path +
                           '_dev_loss_{:.4f}.ckpt'.format(loss_val))
                # save best model
                if acc_val > best_acc_val:
                    best_acc_val = acc_val
                    last_improved_batch = total_batch
                    saver.save(sess=sess, save_path=arg.best_path)
                    improved_flag = '*'
                else:
                    improved_flag = ''

                # show batch training information
                time_diff = get_time_diff(start_time)
                msg = 'Epoch : {0:>3}, Batch : {1:>8}, Train Batch Loss : {2:>6.2}, Train Batch Acc : {3:>6.2%}, Dev Loss : {4:>6.2}, Dev Acc : {5:>6.2%}, Time : {6} {7}'
                print_log(
                    msg.format(epoch + 1, total_batch, batch_loss, batch_acc,
                               loss_val, acc_val, time_diff, improved_flag))

            total_batch += 1
            # early stop judge
            if total_batch - last_improved_batch > arg.early_stop_step:
                print_log('No optimization for a long time, auto-stopping ...',
                          file=log)
                isEarlyStop = True
                break
        if isEarlyStop:
            break

        time_diff = get_time_diff(start_time)
        total_loss, total_acc = total_loss / data_nums, total_acc / data_nums
        msg = '** Epoch : {0:>2} finished, Train Loss : {1:>6.2}, Train Acc : {2:6.2%}, Time : {3}'
        print_log(msg.format(epoch + 1, total_loss, total_acc, time_diff),
                  file=log)