def theano_verion():

    train, test = load("wiki", filter=True)
    alphabet, embeddings = prepare([train, test])
    test_input = getQAIndiceofTest(test, alphabet)
    from model import Model1
    model = Model1(50, 50, 50, len(alphabet.keys()), embeddings)
    #pdb.set_trace()
    #print((model.predict([q_train, a_train])))
    # start training
    for epoch in range(20):

        for x_trainq, x_traina, y_train1 in batch_gen_with_point_wise(
                train, alphabet, FLAGS.batch_size):
            loss, acc = model.train_on_batch([x_trainq, x_traina], y_train1)
            perf = str(loss) + " " + str(acc)
            print("loss is %f with acc %f" % (loss, acc))

        #y_train1 = y_train1.reshape(y_train1.shape[0],1)
        #x = model.predict([x_trainq, x_traina])
        predicted = model.predict_on_batch(test_input)
        print(evaluation.evaluationBypandas(test, predicted))

        evaluation.briany_test_file(test, predicted)
        print("\n\n\n\n\n")
def englishTest():
    train, test, dev = load("wiki", filter=True)
    q_max_sent_length = max(
        map(lambda x: len(x), train['question'].str.split()))
    a_max_sent_length = max(map(lambda x: len(x), train['answer'].str.split()))
    print 'train length', len(train)
    print 'test length', len(test)
    print 'dev length', len(dev)
    # test = test.reindex(np.random.permutation(test.index))
    train = train.reset_index()
    # test = test.reset_index()
    print 'load Data finished'
    columns1 = get_features(train)
    columns2 = get_features(test)
    common = [item for item in columns2 if item in columns1]
    print common
    # common = ['align', 'align_reverse', 'features_similarity', 'features_similarity_reverse']
    print 'save the small idf_dict'
    # pickle.dump(small_idf_dict,open('data/small_idf_dict','w'))
    x = train[common].fillna(0)
    y = train["flag"]
    test_x = test[common].fillna(0)
    # clf = linear_model.LinearRegression()
    clf = linear_model.LogisticRegression()
    # clf = svm.SVR()
    print x.head()
    # clf = GradientBoostingRegressor()
    # clf = tree.DecisionTreeRegressor()
    # clf = svm.SVR()
    clf.fit(x, y)
    print clf.coef_
    # predicted = clf.predict(test_x)
    predicted = clf.predict_proba(test_x)
    predicted = predicted[:, 1]
    print len(predicted)
    print len(test)
    print(evaluation.evaluationBypandas(test, predicted))
Exemple #3
0
conv1_strides = 1
maxpool1_strides = 2

conv2_strides = 1
maxpool2_strides = 2

tensorboard_run_name += "conv_strides=" + str(conv1_strides) + ";"\
                        + "max-pool_strides=" + str(maxpool1_strides)





os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'

images, labels = data_helpers.load(file_path=my_path)

images = np.array(images)
labels = np.array(labels)

np.random.seed(10)
shuffle_indices = np.random.permutation(np.arange(len(images)))
images = images[shuffle_indices]
labels = labels[shuffle_indices]

# Splitting the data into training and validation sets
train_data_len = min(int(len(images) * train_data_percentage), 1500)
images_test = images[:train_data_len]
labels_test = labels[:train_data_len]
images = images[train_data_len:]
labels = labels[train_data_len:]
def test_pair_wise(dns=FLAGS.dns):
    train, test, dev = load(FLAGS.data, filter=True)
    q_max_sent_length = max(
        map(lambda x: len(x), train['question'].str.split()))
    a_max_sent_length = max(map(lambda x: len(x), train['answer'].str.split()))
    print 'q_question_length:{} a_question_length:{}'.format(
        q_max_sent_length, a_max_sent_length)
    print 'train question unique:{}'.format(len(train['question'].unique()))
    print 'train length', len(train)
    print 'test length', len(test)
    print 'dev length', len(dev)
    alphabet, embeddings = prepare([train, test, dev],
                                   dim=FLAGS.embedding_dim,
                                   is_embedding_needed=True,
                                   fresh=True)
    # alphabet,embeddings = prepare_300([train,test,dev])
    print 'alphabet:', len(alphabet)

    with tf.Graph().as_default(), tf.device("/gpu:0"):
        # with tf.device("/cpu:0"):
        session_conf = tf.ConfigProto(
            allow_soft_placement=FLAGS.allow_soft_placement,
            log_device_placement=FLAGS.log_device_placement)
        sess = tf.Session(config=session_conf)
        with sess.as_default(), open(precision, "w") as log:
            log.write(str(FLAGS.__flags) + '\n')
            # train,test,dev = load("trec",filter=True)
            # alphabet,embeddings = prepare([train,test,dev],is_embedding_needed = True)
            cnn = QA_attentive(max_input_left=q_max_sent_length,
                               max_input_right=a_max_sent_length,
                               batch_size=FLAGS.batch_size,
                               vocab_size=len(alphabet),
                               embedding_size=FLAGS.embedding_dim,
                               filter_sizes=list(
                                   map(int, FLAGS.filter_sizes.split(","))),
                               num_filters=FLAGS.num_filters,
                               dropout_keep_prob=FLAGS.dropout_keep_prob,
                               embeddings=embeddings,
                               l2_reg_lambda=FLAGS.l2_reg_lambda,
                               is_Embedding_Needed=True,
                               trainable=FLAGS.trainable)

            # Define Training procedure
            global_step = tf.Variable(0, name="global_step", trainable=False)
            optimizer = tf.train.AdamOptimizer(FLAGS.learning_rate)
            grads_and_vars = optimizer.compute_gradients(cnn.loss)
            train_op = optimizer.apply_gradients(grads_and_vars,
                                                 global_step=global_step)
            saver = tf.train.Saver(tf.global_variables(), max_to_keep=20)
            # Initialize all variables
            sess.run(tf.global_variables_initializer())
            if dns == True:
                loadfile = "tmp/20170502223124__0.678083232207.ckpt"
                saver.restore(sess, loadfile)
                predicted = predict(sess, cnn, train, alphabet,
                                    FLAGS.batch_size, q_max_sent_length,
                                    a_max_sent_length)
                map_mrr_train = evaluation.evaluationBypandas(train, predicted)
                predicted = predict(sess, cnn, test, alphabet,
                                    FLAGS.batch_size, q_max_sent_length,
                                    a_max_sent_length)
                map_mrr_test = evaluation.evaluationBypandas(test, predicted)
                print map_mrr_train
                print map_mrr_test
            # seq_process(train, alphabet)
            # seq_process(test, alphabet)
            map_max = 0.65
            for i in range(1000):
                if dns == True:
                    samples = dns_sample(train,
                                         alphabet,
                                         q_max_sent_length,
                                         a_max_sent_length,
                                         sess,
                                         cnn,
                                         FLAGS.batch_size,
                                         neg_sample_num=10)
                    datas = batch_gen_with_pair_dns(samples, FLAGS.batch_size)
                else:
                    datas = batch_gen_with_pair(train,
                                                alphabet,
                                                FLAGS.batch_size,
                                                q_len=q_max_sent_length,
                                                a_len=a_max_sent_length)

                for data in datas:
                    feed_dict = {
                        cnn.question: data[0],
                        cnn.answer: data[1],
                        cnn.answer_negative: data[2]
                    }
                    _, step, loss, accuracy, score12, score13 = sess.run([
                        train_op, global_step, cnn.loss, cnn.accuracy,
                        cnn.score12, cnn.score13
                    ], feed_dict)
                    time_str = datetime.datetime.now().isoformat()

                    print(
                        "{}: step {}, loss {:g}, acc {:g} ,positive {:g},negative {:g}"
                        .format(time_str, step, loss, accuracy,
                                np.mean(score12), np.mean(score13)))
                    # print loss
                predicted = predict(sess, cnn, train, alphabet,
                                    FLAGS.batch_size, q_max_sent_length,
                                    a_max_sent_length)
                map_mrr_train = evaluation.evaluationBypandas(train, predicted)
                predicted = predict(sess, cnn, test, alphabet,
                                    FLAGS.batch_size, q_max_sent_length,
                                    a_max_sent_length)
                map_mrr_test = evaluation.evaluationBypandas(test, predicted)

                # # predicted_train = prediction(sess,cnn,train,alphabet,q_max_sent_length,a_max_sent_length)
                # map_mrr_dev = evaluation.evaluationBypandas(dev,predicted_dev[:,-1])
                # map_mrr_test = evaluation.evaluationBypandas(test,predicted[:,-1])
                # map_mrr_train = evaluation.evaluationBypandas(train,predicted_train[:,-1])
                # # print evaluation.evaluationBypandas(train,predicted_train[:,-1])
                print "{}:epoch:train map mrr {}".format(i, map_mrr_train)
                print "{}:epoch:test map mrr {}".format(i, map_mrr_test)
                # print "{}:epoch:map mrr {}".format(i,map_mrr_dev)
                line = " {}:epoch: map_train{}----map_test{}".format(
                    i, map_mrr_train[0], map_mrr_test[0])
                log.write(line + '\n')
                log.flush()
                if map_mrr_test[0] > map_max:
                    timeStamp = time.strftime("%Y%m%d%H%M%S",
                                              time.localtime(int(time.time())))
                    folder = 'runs/' + timeDay
                    out_dir = folder + '/' + timeStamp + '__' + FLAGS.data + str(
                        map_mrr_test[0])
                    if not os.path.exists(folder):
                        os.makedirs(folder)
                    save_path = saver.save(sess, out_dir)
                    print "Model saved in file: ", save_path
                    map_max = map_mrr_test[0]
from __future__ import print_function

import os

import tensorflow as tf
import numpy as np

import data_helpers

os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'

my_path = "C:/Users/Teodor/Documents/NBU/NETB380 Programming Practice/trainind_data_creation/Generated/TimesNewRoman/"

images, labels = data_helpers.load(file_path=my_path, number=-1)

images = np.array(images)
labels = np.array(labels)

np.random.seed(10)
shuffle_indices = np.random.permutation(np.arange(len(images)))
images = images[shuffle_indices]
labels = labels[shuffle_indices]

print(len(labels))

# Network Parameters
n_input = 32 * 32  # MNIST data input (img shape: 28*28)
n_classes = 26 + 26 + 10  # MNIST total classes (0-9 digits)
dropout = 0.75  # Dropout, probability to keep units

# tf Graph input
Exemple #6
0
tf.flags.DEFINE_integer("num_checkpoints", 5,
                        "Number of checkpoints to store (default: 5)")

tf.flags.DEFINE_boolean("allow_soft_placement", True,
                        "Allow device soft device placement")
tf.flags.DEFINE_boolean("log_device_placement", False,
                        "Log placement of ops on devices")
tf.flags.DEFINE_string("summary_dir", "/tmp/czx", "summary dir path")

FLAGS = tf.flags.FLAGS
FLAGS.flag_values_dict()

print("Loading Data...")
#negative_xtexts = data_helpers.load_data("../../../data/sample_train.log")
#positive_xtexts = data_helpers.load_data("../../../data/sender_train.log")
positive_xtexts = data_helpers.load("../../../data/sender_test.res")
negative_xtexts = data_helpers.load("../../../data/sample_test.res")
positive_ylabels = data_helpers.load_labels(positive_xtexts.shape[0], 2,
                                            [0, 1])
negative_ylabels = data_helpers.load_labels(negative_xtexts.shape[0], 2,
                                            [1, 0])

print("pos:", positive_xtexts.shape)
print("neg:", negative_xtexts.shape)
print(type(positive_xtexts))
#xtexts = np.vstack((positive_xtexts, negative_xtexts))
#ylabels = np.vstatck((positive_ylabels, negative_ylabels))
xtexts = np.concatenate((positive_xtexts, negative_xtexts), axis=0)
ylabels = np.concatenate((positive_ylabels, negative_ylabels), axis=0)

max_sentence_length = max([len(x.split(" ")) for x in xtexts])
def test_pair_wise(dns=FLAGS.dns):
    train, test, dev = load(FLAGS.data, filter=False)
    train = train.fillna('')
    test = test.fillna('')
    dev = dev.fillna('')
    # train = train[:1000]
    # test = test[:1000]
    # dev = dev[:1000]
    # submit = submit[:1000]
    q_max_sent_length = max(
        map(lambda x: len(x), train['question'].str.split()))
    a_max_sent_length = max(map(lambda x: len(x), train['answer'].str.split()))
    print 'q_question_length:{} a_question_length:{}'.format(
        q_max_sent_length, a_max_sent_length)
    print 'train question unique:{}'.format(len(train['question'].unique()))
    print 'train length', len(train)
    print 'test length', len(test)
    print 'dev length', len(dev)
    alphabet, embeddings = prepare([train, test, dev],
                                   dim=FLAGS.embedding_dim,
                                   is_embedding_needed=True,
                                   fresh=FLAGS.fresh)
    # alphabet,embeddings = prepare_300([train,test,dev])
    print 'alphabet:', len(alphabet)
    with tf.Graph().as_default(), tf.device("/gpu:0"):
        # with tf.device("/cpu:0"):
        session_conf = tf.ConfigProto()
        session_conf.allow_soft_placement = FLAGS.allow_soft_placement
        session_conf.log_device_placement = FLAGS.log_device_placement
        session_conf.gpu_options.allow_growth = True
        sess = tf.Session(config=session_conf)
        with sess.as_default(), open(precision, "w") as log:
            log.write(str(FLAGS.__flags) + '\n')
            # train,test,dev = load("trec",filter=True)
            # alphabet,embeddings = prepare([train,test,dev],is_embedding_needed = True)
            print "start build model"
            cnn = QA_CNN_extend(max_input_left=q_max_sent_length,
                                max_input_right=a_max_sent_length,
                                batch_size=FLAGS.batch_size,
                                vocab_size=len(alphabet),
                                embedding_size=FLAGS.embedding_dim,
                                filter_sizes=list(
                                    map(int, FLAGS.filter_sizes.split(","))),
                                num_filters=FLAGS.num_filters,
                                dropout_keep_prob=FLAGS.dropout_keep_prob,
                                embeddings=embeddings,
                                l2_reg_lambda=FLAGS.l2_reg_lambda,
                                overlap_needed=FLAGS.overlap_needed,
                                learning_rate=FLAGS.learning_rate,
                                trainable=FLAGS.trainable,
                                extend_feature_dim=FLAGS.extend_feature_dim,
                                model_type=FLAGS.CNN_type)

            # Define Training procedure
            global_step = tf.Variable(0, name="global_step", trainable=False)
            optimizer = tf.train.AdamOptimizer(cnn.learning_rate)
            grads_and_vars = optimizer.compute_gradients(cnn.loss)
            train_op = optimizer.apply_gradients(grads_and_vars,
                                                 global_step=global_step)
            saver = tf.train.Saver(tf.global_variables(), max_to_keep=20)
            # Initialize all variables
            print "build over"
            sess.run(tf.global_variables_initializer())
            print "variables_initializer"
            if dns == True:
                loadfile = "runs/20170604/20170604183633__nlpcc0.833940715393"
                saver.restore(sess, loadfile)
                predicted = predict(sess, cnn, train, alphabet,
                                    FLAGS.batch_size, q_max_sent_length,
                                    a_max_sent_length)
                map_mrr_train = evaluation.evaluationBypandas(train, predicted)
                predicted = predict(sess, cnn, test, alphabet,
                                    FLAGS.batch_size, q_max_sent_length,
                                    a_max_sent_length)
                map_mrr_test = evaluation.evaluationBypandas(test, predicted)
                print map_mrr_train
                print map_mrr_test
            # seq_process(train, alphabet)
            # seq_process(test, alphabet)
            '''
            print 'get my submit result'
            loadfile="runs/20170604/20170604183633__nlpcc0.833940715393"
            saver.restore(sess, loadfile)
            predicted = predict(sess,cnn,train,alphabet,FLAGS.batch_size,q_max_sent_length,a_max_sent_length)
            train['predicted'] = predicted
            train['predicted'].to_csv('train.QApair.TJU_IR_QA2017_train.score',index = False,sep = '\t')
            map_mrr_train = evaluation.evaluationBypandas(train,predicted)
            predicted_test = predict(sess,cnn,test,alphabet,FLAGS.batch_size,q_max_sent_length,a_max_sent_length)
            test['predicted'] = predicted_test
            test['predicted'].to_csv('train.QApair.TJU_IR_QA2017.score',index = False,sep = '\t')
            map_mrr_test = evaluation.evaluationBypandas(test,predicted_test)
            print 'map_mrr train',map_mrr_train
            print 'map_prr dev',map_mrr_test

            predict_submit = predict(sess,cnn,submit,alphabet,FLAGS.batch_size,q_max_sent_length,a_max_sent_length)
            submit['predicted'] = predict_submit
            submit['predicted'].to_csv('train.QApair.TJU_IR_QA2017_submit.score',index = False,sep = '\t')
            print 'predict over'

            '''
            map_max = 0.65
            for i in range(1000):
                if dns == True:
                    samples = dns_sample(train,
                                         alphabet,
                                         q_max_sent_length,
                                         a_max_sent_length,
                                         sess,
                                         cnn,
                                         FLAGS.batch_size,
                                         neg_sample_num=10)
                    datas = batch_gen_with_pair_dns(samples, FLAGS.batch_size)
                else:
                    d = get_overlap_dict(train,
                                         alphabet,
                                         q_len=q_max_sent_length,
                                         a_len=a_max_sent_length)
                    datas = batch_gen_with_pair_overlap(
                        train,
                        alphabet,
                        FLAGS.batch_size,
                        q_len=q_max_sent_length,
                        a_len=a_max_sent_length,
                        fresh=FLAGS.fresh,
                        overlap_dict=d)
                print "load data"
                for data in datas:
                    feed_dict = {
                        cnn.question: data[0],
                        cnn.answer: data[1],
                        cnn.answer_negative: data[2],
                        cnn.q_pos_overlap: data[3],
                        cnn.q_neg_overlap: data[4],
                        cnn.a_pos_overlap: data[5],
                        cnn.a_neg_overlap: data[6]
                    }
                    _, step, loss, accuracy, score12, score13 = sess.run([
                        train_op, global_step, cnn.loss, cnn.accuracy,
                        cnn.score12, cnn.score13
                    ], feed_dict)
                    time_str = datetime.datetime.now().isoformat()

                    print(
                        "{}: step {}, loss {:g}, acc {:g} ,positive {:g},negative {:g}"
                        .format(time_str, step, loss, accuracy,
                                np.mean(score12), np.mean(score13)))
                    line = "{}: step {}, loss {:g}, acc {:g} ,positive {:g},negative {:g}".format(
                        time_str, step, loss, accuracy, np.mean(score12),
                        np.mean(score13))
                    # print loss
                if i % 1 == 0:
                    predicted = predict(sess, cnn, test, alphabet,
                                        FLAGS.batch_size, q_max_sent_length,
                                        a_max_sent_length)
                    map_mrr_test = evaluation.evaluationBypandas(
                        test, predicted)
                    print "{}:epoch:test map mrr {}".format(i, map_mrr_test)
                    line = " {}:epoch: map_test{}".format(i, map_mrr_test[0])
                    if map_mrr_test[0] > map_max:
                        map_max = map_mrr_test[0]
                        timeStamp = time.strftime(
                            "%Y%m%d%H%M%S", time.localtime(int(time.time())))
                        folder = 'runs/' + timeDay
                        out_dir = folder + '/' + timeStamp + '__' + FLAGS.data + str(
                            map_mrr_test[0])
                        if not os.path.exists(folder):
                            os.makedirs(folder)
                        save_path = saver.save(sess, out_dir)
                        print "Model saved in file: ", save_path
                '''
                predicted = predict(sess,cnn,train,alphabet,FLAGS.batch_size,q_max_sent_length,a_max_sent_length)
                map_mrr_train = evaluation.evaluationBypandas(train,predicted)
                predicted = predict(sess,cnn,dev,alphabet,FLAGS.batch_size,q_max_sent_length,a_max_sent_length)
                map_mrr_dev = evaluation.evaluationBypandas(dev,predicted)
                print "{}:epoch:train map mrr {}".format(i,map_mrr_train)
                # print "{}:epoch:test map mrr {}".format(i,map_mrr_test)
                print "{}:epoch:dev map mrr {}".format(i,map_mrr_dev)
                if map_mrr_dev[0] > map_max:
                    map_max = map_mrr_dev[0]
                    predicted = predict(sess,cnn,test,alphabet,FLAGS.batch_size,q_max_sent_length,a_max_sent_length)
                    map_mrr_test = evaluation.evaluationBypandas(test,predicted)
                    print "{}:epoch:test map mrr {}".format(i,map_mrr_test)
                    line = " {}:epoch: map_test{}".format(i,map_mrr_test[0])
                    if map_mrr_test[0] > map_max:
                        timeStamp = time.strftime("%Y%m%d%H%M%S", time.localtime(int(time.time())))
                        folder = 'runs/' + timeDay
                        out_dir = folder +'/'+timeStamp+'__'+FLAGS.data+str(map_mrr_test[0])
                        if not os.path.exists(folder):
                            os.makedirs(folder)
                        save_path = saver.save(sess, out_dir)
                        print "Model saved in file: ", save_path
                        
                '''
                # # predicted_train = prediction(sess,cnn,train,alphabet,q_max_sent_length,a_max_sent_length)
                # map_mrr_dev = evaluation.evaluationBypandas(dev,predicted_dev[:,-1])
                # map_mrr_test = evaluation.evaluationBypandas(test,predicted[:,-1])
                # map_mrr_train = evaluation.evaluationBypandas(train,predicted_train[:,-1])
                # # print evaluation.evaluationBypandas(train,predicted_train[:,-1])

                # line = " {}:epoch: map_train{}----map_test{}----map_dev{}".format(i,map_mrr_train[0],map_mrr_test[0],map_mrr_dev[0])
                # line = " {}:epoch: map_train{}----map_dev{}".format(i,map_mrr_train[0],map_mrr_dev[0])
                log.write(line + '\n')
                log.flush()
def test():
    train, test, dev = load("trec", filter=False)
    q_max_sent_length = max(
        map(lambda x: len(x), train['question'].str.split()))
    a_max_sent_length = max(map(lambda x: len(x), train['answer'].str.split()))

    print 'train length', len(train)
    print 'test length', len(test)
    print 'dev length', len(dev)
    alphabet, embeddings = prepare([train, test, dev],
                                   is_embedding_needed=True)
    print 'alphabet:', len(alphabet)
    with tf.Graph().as_default():
        # with tf.device("/cpu:0"):
        session_conf = tf.ConfigProto(
            allow_soft_placement=FLAGS.allow_soft_placement,
            log_device_placement=FLAGS.log_device_placement)
        sess = tf.Session(config=session_conf)
        with sess.as_default(), open("precision.txt", "w") as log:

            # train,test,dev = load("trec",filter=True)
            # alphabet,embeddings = prepare([train,test,dev],is_embedding_needed = True)
            cnn = QA_overlap(
                max_len_left=q_max_sent_length,
                max_len_right=a_max_sent_length,
                vocab_size=len(alphabet),
                embedding_size=FLAGS.embedding_dim,
                embeddings=embeddings,
                # filter_sizes = list(map(int, FLAGS.filter_sizes.split(","))),
                filter_sizes=[5],
                num_filters=FLAGS.num_filters,
                num_hidden=10,
                l2_reg_lambda=FLAGS.l2_reg_lambda,
                is_Embedding_Needed=True)

            # Define Training procedure
            global_step = tf.Variable(0, name="global_step", trainable=False)
            optimizer = tf.train.AdamOptimizer(1e-3)
            grads_and_vars = optimizer.compute_gradients(cnn.loss)
            train_op = optimizer.apply_gradients(grads_and_vars,
                                                 global_step=global_step)
            saver = tf.train.Saver(tf.all_variables(), max_to_keep=20)
            # Initialize all variables
            sess.run(tf.global_variables_initializer())

            #summary
            # Keep track of gradient values and sparsity (optional)
            grad_summaries = []
            for g, v in grads_and_vars:
                if g is not None:
                    grad_hist_summary = tf.histogram_summary(
                        "{}/grad/hist".format(v.name), g)
                    sparsity_summary = tf.scalar_summary(
                        "{}/grad/sparsity".format(v.name),
                        tf.nn.zero_fraction(g))
                    grad_summaries.append(grad_hist_summary)
                    grad_summaries.append(sparsity_summary)
            grad_summaries_merged = tf.merge_summary(grad_summaries)
            # Output directory for models and summaries

            out_dir = os.path.abspath(
                os.path.join(os.path.curdir, "runs", timestamp))
            print("Writing to {}\n".format(out_dir))

            # Summaries for loss and accuracy
            loss_summary = tf.scalar_summary("loss", cnn.loss)
            acc_summary = tf.scalar_summary("accuracy", cnn.accuracy)

            # Train Summaries
            train_summary_op = tf.merge_summary(
                [loss_summary, acc_summary, grad_summaries_merged])
            train_summary_dir = os.path.join(out_dir, "summaries", "train")
            train_summary_writer = tf.train.SummaryWriter(
                train_summary_dir, sess.graph)

            # Dev summaries
            dev_summary_op = tf.merge_summary([loss_summary, acc_summary])
            dev_summary_dir = os.path.join(out_dir, "summaries", "dev")
            dev_summary_writer = tf.train.SummaryWriter(
                dev_summary_dir, sess.graph)

            # Checkpoint directory. Tensorflow assumes this directory already exists so we need to create it
            checkpoint_dir = os.path.abspath(
                os.path.join(out_dir, "checkpoints"))
            checkpoint_prefix = os.path.join(checkpoint_dir, "model")
            if not os.path.exists(checkpoint_dir):
                os.makedirs(checkpoint_dir)
            # saver = tf.train.Saver(tf.global_variables(), max_to_keep=FLAGS.num_checkpoints)

            # seq_process(train, alphabet)
            # seq_process(test, alphabet)
            for i in range(25):
                for x_left_batch, x_right_batch, y_batch, overlap in batch_gen_with_point_wise(
                        train,
                        alphabet,
                        FLAGS.batch_size,
                        overlap=True,
                        q_len=q_max_sent_length,
                        a_len=a_max_sent_length):
                    feed_dict = {
                        cnn.input_left: x_left_batch,
                        cnn.input_right: x_right_batch,
                        cnn.input_y: y_batch,
                        cnn.overlap: overlap,
                        cnn.dropout_keep_prob: FLAGS.dropout_keep_prob
                    }

                    _, step, summaries, loss, accuracy, pred, scores = sess.run(
                        [
                            train_op, global_step, train_summary_op, cnn.loss,
                            cnn.accuracy, cnn.predictions, cnn.scores
                        ], feed_dict)
                    time_str = datetime.datetime.now().isoformat()

                    print("{}: step {}, loss {:g}, acc {:g}  ".format(
                        time_str, step, loss, accuracy))
                    train_summary_writer.add_summary(summaries, step)
                    # print loss

                predicted = prediction(sess, cnn, test, alphabet,
                                       q_max_sent_length, a_max_sent_length)
                predicted_dev = prediction(sess, cnn, dev, alphabet,
                                           q_max_sent_length,
                                           a_max_sent_length)
                # predicted_train = prediction(sess,cnn,train,alphabet)
                print np.array(predicted).shape
                print len(predicted)
                print len(test)
                map_mrr_dev = evaluation.evaluationBypandas(
                    dev, predicted_dev[:, -1])
                map_mrr_test = evaluation.evaluationBypandas(
                    test, predicted[:, -1])
                # print evaluation.evaluationBypandas(train,predicted_train[:,-1])
                print map_mrr_dev
                print map_mrr_test
                line = " {}: epoch: precision {}".format(i, map_mrr_test)
                log.write(line + '\n')
def main():
    train, test, dev = load("wiki", filter=True)
    q_max_sent_length = max(
        map(lambda x: len(x), train['question'].str.split()))
    a_max_sent_length = max(map(lambda x: len(x), train['answer'].str.split()))
    q_max_sent_length = 40
    a_max_sent_length = 40
    print 'train length', len(train)
    print 'test length', len(test)
    print 'dev length', len(dev)
    alphabet, embeddings = prepare([train, test, dev],
                                   is_embedding_needed=True)
    print 'alphabet:', len(alphabet)
    with tf.Graph().as_default():
        # with tf.device("/cpu:0"):
        session_conf = tf.ConfigProto(
            allow_soft_placement=FLAGS.allow_soft_placement,
            log_device_placement=FLAGS.log_device_placement)
        sess = tf.Session(config=session_conf)
        with sess.as_default(), open("precision.txt", "w") as log:
            cnn = QA_CNN_Attentive(max_input_left=q_max_sent_length,
                                   max_input_right=a_max_sent_length,
                                   batch_size=FLAGS.batch_size,
                                   vocab_size=len(alphabet),
                                   embeddings=embeddings,
                                   embedding_size=FLAGS.embedding_dim,
                                   num_filters=FLAGS.num_filters,
                                   dropout_keep_prob=1.0,
                                   l2_reg_lambda=FLAGS.l2_reg_lambda,
                                   is_Embedding_Needed=True,
                                   trainable=True)

            # Define Training procedure
            global_step = tf.Variable(0, name="global_step", trainable=False)
            optimizer = tf.train.AdamOptimizer(FLAGS.learning_rate)
            grads_and_vars = optimizer.compute_gradients(cnn.loss)
            train_op = optimizer.apply_gradients(grads_and_vars,
                                                 global_step=global_step)
            saver = tf.train.Saver(tf.all_variables(), max_to_keep=20)
            # Initialize all variables
            sess.run(tf.global_variables_initializer())
            for i in range(1000):
                for x_batch_1, x_batch_2, x_batch_3 in batch_gen_with_pair_whole(
                        train, alphabet, FLAGS.batch_size):

                    feed_dict = {
                        cnn.question: x_batch_1,
                        cnn.answer: x_batch_2,
                        cnn.answer_negative: x_batch_3,
                    }

                    _, step, loss, accuracy, scores1, scores2, a1, a2, U = sess.run(
                        [
                            train_op, global_step, cnn.loss, cnn.accuracy,
                            cnn.score12, cnn.score13, cnn.attention_q,
                            cnn.attention_a, cnn.U
                        ], feed_dict)
                    time_str = datetime.datetime.now().isoformat()
                    print(
                        "{}: step {}, loss {:g}, acc {:g} positive {:g} negative {:g} mean_pooling {:g}"
                        .format(time_str, step, loss,
                                accuracy, np.mean(scores1), np.mean(scores2),
                                np.mean(a1)))
                    # print a1
                predicted = predict(sess, cnn, train, alphabet,
                                    FLAGS.batch_size, q_max_sent_length,
                                    a_max_sent_length)
                print(evaluation.evaluationBypandas(train, predicted))
                predicted = predict(sess, cnn, test, alphabet,
                                    FLAGS.batch_size, q_max_sent_length,
                                    a_max_sent_length)
                print(evaluation.evaluationBypandas(test, predicted))