Beispiel #1
0
def evaluate(sess,query,doc):
    data_len = len(query)
    query_len = data_len/config.unit_size
    batch_val = batch_iter(query,doc,config.batch_size,config.unit_size)

    total_loss = 0.0
    total_p_1 = 0.0
    total_mrr = 0.0

    for val_query,val_doc in batch_val:
        loss,p_1,mrr = sess.run([model.loss,model.p_1,model.mrr],feed_dict=feed_data(val_query,val_doc))
        total_loss += loss*64
        total_p_1 += p_1*64
        total_mrr += mrr*64
    return total_loss/query_len,total_p_1/query_len,total_mrr/query_len
Beispiel #2
0
#encoding:utf-8

import load_data
from config import TextConfig as config

#参数
filename = config.filename
vocab_dir = config.vocab_dir
vocab_size = config.vocab_size

#读取,分词
contents, labels = load_data.read_file_seg(filename)
#建立词典
load_data.build_vocab(filename, vocab_dir, vocab_size)
#词ID,类别ID
_, word_to_id = load_data.read_vocab(vocab_dir)
_, cat_to_id = load_data.read_category()
#生成向量
x, y = load_data.process_file(filename, word_to_id, cat_to_id, max_length=600)
a = load_data.batch_iter(x, y, 10)
#load_data函数里面所有包含filename为参数的函数,都应该优化
#实现,运行load_data生成词典,词向量,引用直接加载训练好的模型就可以
Beispiel #3
0
        print(
            "++++++++++++++++++dev++++++++++++++{}: step {}, loss {:g}, acc {:g}"
            .format(time_str, step, cost, accuracy))
        if writer:
            writer.add_summary(summaries, step)

    # for epoch in range(FLAGS.num_epochs):
    #     print('current epoch %s' % (epoch + 1))
    #     for i in range(0, 200000, FLAGS.batch_size):
    #         x = train_x[i:i + FLAGS.batch_size]
    #         y = train_y[i:i + FLAGS.batch_size]
    #         step = train_step(x, y)
    #         if step % FLAGS.evaluate_every == 0:
    #             dev_step(dev_x[0: 100], dev_y[0: 100], dev_summary_writer)

    batches = batch_iter(list(zip(train_x, train_y)), FLAGS.batch_size,
                         FLAGS.num_epochs)

    for batch in batches:
        # print (batch[0])
        x_batch, y_batch = zip(
            *batch)  # x_batch = 64 * 30 * 30  y_batch = 64 * 5
        train_step(x_batch, y_batch)
        current_step = tf.train.global_step(sess, global_step)
        if current_step % FLAGS.evaluate_every == 0:  # 100次
            print("\nEvaluation:")
            dev_step(dev_x[0:2000], dev_y[0:2000], writer=dev_summary_writer)
            print("")
        if current_step % FLAGS.checkpoint_every == 0:  # 100次
            path = saver.save(sess,
                              checkpoint_prefix,
                              global_step=current_step)
Beispiel #4
0
def train():

    word_2_id = read_vocab(vocab_dir)
    query_train,doc_train = process_file(train_query_dir,train_docs_dir,word_2_id)
    query_val,doc_val = process_file(val_query_dir,val_docs_dir,word_2_id)

    # 配置Tensorboard,重新训练时需要将文件夹删除,不然会覆盖
    tb_dir = './tensorboard/mvlstm-'+config.interaction
    if not os.path.exists(tb_dir):
        os.makedirs(tb_dir)

    tf.summary.scalar('loss',model.loss)
    tf.summary.scalar('p@1',model.p_1)
    tf.summary.scalar('MRR',model.mrr)

    merged = tf.summary.merge_all()


    # 配置 Saver
    saver = tf.train.Saver()
    if not os.path.exists(save_dir):
        os.makedirs(save_dir)

    sess = tf.Session()
    train_writer = tf.summary.FileWriter(tb_dir + '/train', sess.graph)
    val_writer = tf.summary.FileWriter(tb_dir + '/test',sess.graph)
    sess.run(tf.global_variables_initializer())
    #writer.add_graph(sess.graph)

    best_val = 0.0
    total_batch = 0
    print('Training and Evaluation...')
    for epoch in tqdm(range(config.num_epochs)):
        print('Epoch:', epoch + 1)
        batch_train = batch_iter(query_train,doc_train,config.batch_size,config.unit_size)

        for query_batch,doc_batch in batch_train:

            feed_dict =feed_data(query_batch, doc_batch)
            val_feed = feed_data(query_val,doc_val)

            if total_batch%config.save_per_batch==0:
                s = sess.run(merged,feed_dict=feed_dict)
                train_writer.add_summary(s,total_batch)

            if total_batch%100==0:
                train_loss, train_p_1, train_mrr,y_pred= sess.run([model.loss, model.p_1, model.mrr,model.y_pred],feed_dict=feed_dict)
                val_loss, val_p_1, val_mrr = evaluate(sess, query_val, doc_val)
                s = sess.run(merged,feed_dict=val_feed)
                val_writer.add_summary(s,total_batch)
                #print(y_pred)
                #print(type(y_pred))
                #print(y_pred.shape)

                if val_p_1 >best_val:
                    best_val = val_p_1
                    saver.save(sess,save_path)
                    improved_str='*'
                else:
                    improved_str=''

                msg = 'Iter: {0:>6}, Train Loss: {1:>6.2}, Train p@1: {2:>7.2%}, Train MRR: {3:>7.2%} , Val Loss: {4:>6.2}, Val p@1: {5:>7.2%}, Val MRR: {6:>4.2%} {7}'
                print(msg.format(total_batch, train_loss, train_p_1, train_mrr,val_loss, val_p_1, val_mrr,improved_str))

            sess.run(model.optmz,feed_dict=feed_dict)
            total_batch+=1
            Evaluates model on a dev set
            """
            feed_dict = {cnn.input_u: u_batch, cnn.input_i: i_batch, cnn.input_y: y_batch, cnn.dropout_keep_prob: 1.0}
            step, summaries, loss, accuracy, mae, rmse = sess.run(
                [global_step, dev_summary_op, cnn.loss, cnn.accuracy, cnn.mae, cnn.rmse], feed_dict
            )
            time_str = datetime.datetime.now().isoformat()
            print(
                "{}: step {}, loss {:g}, acc {:g}, mae {:g}, rmse {:g}".format(
                    time_str, step, loss, accuracy, mae, rmse
                )
            )
            if writer:
                writer.add_summary(summaries, step)

        # Generate batches
        batches = batch_iter(zip(u_train, i_train, y_train), FLAGS.batch_size, FLAGS.num_epochs)
        # Training loop. For each batch...
        for batch in batches:
            u_batch, i_batch, y_batch = zip(*batch)
            # print u_batch, i_batch, y_batch
            train_step(u_batch, i_batch, y_batch)
            current_step = tf.train.global_step(sess, global_step)
            if current_step % FLAGS.evaluate_every == 0:
                print("\nEvaluation:")
                dev_step(u_dev, i_dev, y_dev, writer=dev_summary_writer)
                print("")
            if current_step % FLAGS.checkpoint_every == 0:
                path = saver.save(sess, checkpoint_prefix, global_step=current_step)
                print("Saved model checkpoint to {}\n".format(path))
Beispiel #6
0
                cnn.input_y: y_batch,
                cnn.dropout_keep_prob: 1.0
            }
            step, summaries, loss, accuracy, mae, rmse = sess.run([
                global_step, dev_summary_op, cnn.loss, cnn.accuracy, cnn.mae,
                cnn.rmse
            ], feed_dict)
            time_str = datetime.datetime.now().isoformat()
            print(
                "{}: step {}, loss {:g}, acc {:g}, mae {:g}, rmse {:g}".format(
                    time_str, step, loss, accuracy, mae, rmse))
            if writer:
                writer.add_summary(summaries, step)

        # Generate batches
        batches = batch_iter(zip(u_train, i_train, y_train), FLAGS.batch_size,
                             FLAGS.num_epochs)
        # Training loop. For each batch...
        for batch in batches:
            u_batch, i_batch, y_batch = zip(*batch)
            # print u_batch, i_batch, y_batch
            train_step(u_batch, i_batch, y_batch)
            current_step = tf.train.global_step(sess, global_step)
            if current_step % FLAGS.evaluate_every == 0:
                print("\nEvaluation:")
                dev_step(u_dev, i_dev, y_dev, writer=dev_summary_writer)
                print("")
            if current_step % FLAGS.checkpoint_every == 0:
                path = saver.save(sess,
                                  checkpoint_prefix,
                                  global_step=current_step)
                print("Saved model checkpoint to {}\n".format(path))
Beispiel #7
0
            feed_dict = {
                cnn.input_x: x_batch,
                cnn.input_y: y_batch,
                cnn.dropout_keep_prob: 1.0
            }
            step, summaries, loss, accuracy = sess.run(
                [global_step, dev_summary_op, cnn.loss, cnn.accuracy],
                feed_dict)
            time_str = datetime.datetime.now().isoformat()
            print("{}: step {}, loss {:g}, acc {:g}".format(
                time_str, step, loss, accuracy))
            if writer:
                writer.add_summary(summaries, step)

        # Generate batches
        batches = load_data.batch_iter(list(zip(x_train, y_train)),
                                       FLAGS.batch_size, FLAGS.num_epochs)

        # Training loop. For each batch...
        for batch in batches:
            x_batch, y_batch = zip(*batch)
            train_step(x_batch, y_batch)
            current_step = tf.train.global_step(sess, global_step)
            if current_step % FLAGS.evaluate_every == 0:
                print("\nEvaluation:")
                dev_step(x_dev, y_dev, writer=dev_summary_writer)
                print("")
            if current_step % FLAGS.checkpoint_every == 0:
                path = saver.save(sess,
                                  checkpoint_prefix,
                                  global_step=current_step)
                print("Saved model checkpoint to {}\n".format(path))
Beispiel #8
0
def train():
    # load data
    data, labels = load_data.extract_data('linearly_separable_data.csv')
    # creating testing and training set
    X_train, X_test, Y_train, Y_test = train_test_split(data,
                                                        labels,
                                                        test_size=0.25)

    train_data_node = tf.placeholder(tf.float32, shape=(None, 2))
    train_label_node = tf.placeholder(tf.float32, shape=(None, 1))

    # weight
    W = tf.Variable(tf.random_uniform([2, 1], -1.0, 1.0), name="W")
    b = tf.Variable(tf.zeros([1]))
    # y_value = [batch_size,1]
    y_value = tf.matmul(train_data_node, W) + b
    weight_loss = 0.5 * tf.reduce_sum(tf.square(W))
    hinge_loss = tf.reduce_sum(
        tf.maximum(tf.zeros([BATCH_SIZE, 1]), 1 - train_label_node * y_value))
    svm_loss = weight_loss + svmC * hinge_loss
    # for test
    hinge_loss_test = tf.reduce_sum(
        tf.maximum(tf.zeros([Test_Size, 1]), 1 - train_label_node * y_value))
    svm_loss_test = weight_loss + svmC * hinge_loss_test
    # train
    global_step = tf.Variable(0, name="global_step", trainable=False)
    optimizer = tf.train.AdamOptimizer(1e-3)
    grads_and_vars = optimizer.compute_gradients(svm_loss)
    train_op = optimizer.apply_gradients(grads_and_vars,
                                         global_step=global_step)

    # evaluation
    predicted_class = tf.sign(y_value)
    correct_prediction = tf.equal(train_label_node, predicted_class)
    accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))

    # runing the training
    with tf.Session() as sess:
        tf.initialize_all_variables().run()
        print('Initialized!')
        # generate batches
        batches = load_data.batch_iter(list(zip(X_train, Y_train)), BATCH_SIZE,
                                       NUM_EPOCHS)
        # batch count
        batch_count = 0
        epoch = 1
        print("Epoch " + str(epoch) + ":")
        for batch in batches:
            batch_count += 1
            # train process
            x_batch, y_batch = zip(*batch)
            feed_dict = {train_data_node: x_batch, train_label_node: y_batch}
            _, step, losses = sess.run([train_op, global_step, svm_loss],
                                       feed_dict=feed_dict)
            # test process
            if (batch_count * BATCH_SIZE) % Train_Size == 0:
                epoch += 1
                print("Epoch " + str(epoch) + ":")
            if batch_count % EVAL_FREQUENCY == 0:
                feed_dict = {train_data_node: X_test, train_label_node: Y_test}
                step, losses, acc = sess.run(
                    [global_step, svm_loss_test, accuracy],
                    feed_dict=feed_dict)
                time_str = datetime.datetime.now().isoformat()
                print("{}: step {}, loss {:g}, acc {:g}".format(
                    time_str, step, losses, acc))