Ejemplo n.º 1
0
def eval(w2v_model):
    # Evaluation评估
    checkpoint_file = tf.train.latest_checkpoint(
        FLAGS.checkpoint_dir)  #查找最新保存的checkpoint文件的文件名
    graph = tf.Graph()  #创建图层
    # #定义属于计算图graph的张量和操作
    with graph.as_default():
        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():
            # Load the saved meta graph and restore variables加载保存的元图并恢复变量
            saver = tf.train.import_meta_graph(
                "{}.meta".format(checkpoint_file))
            saver.restore(sess, checkpoint_file)

            # Get the placeholders from the graph by name从图表中按名称获取占位符
            input_x = graph.get_operation_by_name("input_x").outputs[0]

            dropout_keep_prob = graph.get_operation_by_name(
                "dropout_keep_prob").outputs[0]

            # Tensors we want to evaluate我们要计算的张量
            predictions = graph.get_operation_by_name(
                "output/predictions").outputs[0]

            x_test, y_test = load_data(w2v_model, 5)
            # Generate batches for one epoch为一个epoch生成批处理
            batches = data_helpers.batch_iter(list(x_test),
                                              FLAGS.batch_size,
                                              1,
                                              shuffle=False)

            # Collect the predictions here在这里收集预测值
            all_predictions = []

            for x_test_batch in batches:
                batch_predictions = sess.run(predictions, {
                    input_x: x_test_batch,
                    dropout_keep_prob: 1.0
                })
                all_predictions = np.concatenate(
                    [all_predictions, batch_predictions])  #数据拼接操作

    # Print accuracy if y_test is defined如果定义了y_test,则打印精度
    if y_test is not None:
        correct_predictions = float(sum(all_predictions == y_test))
        print("Total number of test examples: {}".format(len(y_test)))  #测试实例总数
        print("Accuracy: {:g}".format(correct_predictions /
                                      float(len(y_test))))  #得出精确值

    # Save the evaluation to a csv将评估保存到csv
    predictions_human_readable = np.column_stack(
        all_predictions)  #column_stack()连接矩阵,以行的方式
    out_path = os.path.join(FLAGS.checkpoint_dir, "..",
                            "prediction.csv")  #保存评估数据
    print("Saving evaluation to {0}".format(out_path))  #输出保存路径
    with open(out_path, 'w') as f:
        csv.writer(f).writerows(predictions_human_readable)
Ejemplo n.º 2
0
 def dev_test():
     batches_dev = data_helpers.batch_iter(list(zip(x_dev, y_dev)),
                                           FLAGS.batch_size, 1)
     for batch_dev in batches_dev:
         x_batch_dev, y_batch_dev = zip(*batch_dev)
         dev_step(x_batch_dev,
                  y_batch_dev,
                  writer=dev_summary_writer)
Ejemplo n.º 3
0
def eval(w2v_model):
    # Evaluation
    # ==================================================
    checkpoint_file = tf.train.latest_checkpoint(FLAGS.checkpoint_dir)
    graph = tf.Graph()
    with graph.as_default():
        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():
            # Load the saved meta graph and restore variables
            saver = tf.train.import_meta_graph(
                "{}.meta".format(checkpoint_file))
            saver.restore(sess, checkpoint_file)

            # Get the placeholders from the graph by name
            input_x = graph.get_operation_by_name("input_x").outputs[0]

            dropout_keep_prob = graph.get_operation_by_name(
                "dropout_keep_prob").outputs[0]

            # Tensors we want to evaluate
            predictions = graph.get_operation_by_name(
                "output/predictions").outputs[0]

            x_test, y_test = load_data(w2v_model, 1290)
            # Generate batches for one epoch
            batches = data_helpers.batch_iter(list(x_test),
                                              FLAGS.batch_size,
                                              1,
                                              shuffle=False)

            # Collect the predictions here
            all_predictions = []

            for x_test_batch in batches:
                batch_predictions = sess.run(predictions, {
                    input_x: x_test_batch,
                    dropout_keep_prob: 1.0
                })
                all_predictions = np.concatenate(
                    [all_predictions, batch_predictions])

    # Print accuracy if y_test is defined
    if y_test is not None:
        correct_predictions = float(sum(all_predictions == y_test))
        print("Total number of test examples: {}".format(len(y_test)))
        print("Accuracy: {:g}".format(correct_predictions /
                                      float(len(y_test))))

    # Save the evaluation to a csv
    predictions_human_readable = np.column_stack(all_predictions)
    out_path = os.path.join(FLAGS.checkpoint_dir, "..", "prediction.csv")
    print("Saving evaluation to {0}".format(out_path))
    with open(out_path, 'w') as f:
        csv.writer(f).writerows(predictions_human_readable)
Ejemplo n.º 4
0
 def dev_test():
     batches_dev = batch_iter(list(zip(x_dev, y_dev)),
                              FLAGS.batch_size, 1)
     prediction_all = []
     y_true_all = []
     for batch_dev in batches_dev:
         x_batch_dev, y_batch_dev = zip(*batch_dev)
         predictions = dev_step(x_batch_dev,
                                y_batch_dev,
                                writer=dev_summary_writer)
         prediction_all.extend(predictions)
         y_true_all.extend(np.argmax(y_batch_dev, axis=1).tolist())
     p, r, micro_p, micro_r, micro_f1, macro_f1, accuracy1 = fastF1(
         y_true_all, prediction_all, FLAGS.num_class)
     print(
         "test P: {:.2f}%, R: {:.2f}%, micro_p: {:.2f}%, micro_r: {:.2f}%,Micro_f1: {:.2f}%, Macro_f1: {:.2f}%, Accuracy: {:.2f}%"
         .format(p, r, micro_p, micro_r, micro_f1, macro_f1,
                 accuracy1))
Ejemplo n.º 5
0
def train(w2v_model):
    # Training
    # ==================================================
    x_train, x_dev, y_train, y_dev, vocab_size = load_data(w2v_model)
    with tf.Graph().as_default():
        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():
            cnn = TextCNN(w2v_model,
                          sequence_length=x_train.shape[1],
                          num_classes=y_train.shape[1],
                          vocab_size=vocab_size,
                          embedding_size=FLAGS.embedding_dim,
                          filter_sizes=list(
                              map(int, FLAGS.filter_sizes.split(","))),
                          num_filters=FLAGS.num_filters,
                          l2_reg_lambda=FLAGS.l2_reg_lambda)

            # 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)

            # 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.summary.histogram(
                        "{}/grad/hist".format(v.name), g)
                    sparsity_summary = tf.summary.scalar(
                        "{}/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.summary.merge(grad_summaries)

            # Output directory for models and summaries
            timestamp = str(int(time.time()))
            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.summary.scalar("loss", cnn.loss)
            acc_summary = tf.summary.scalar("accuracy", cnn.accuracy)

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

            # Dev summaries
            dev_summary_op = tf.summary.merge([loss_summary, acc_summary])
            dev_summary_dir = os.path.join(out_dir, "summaries", "dev")
            dev_summary_writer = tf.summary.FileWriter(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)

            # Write vocabulary
            # vocab_processor.save(os.path.join(out_dir, "vocab"))

            # Initialize all variables
            sess.run(tf.global_variables_initializer())

            def train_step(x_batch, y_batch):
                """
                A single training step
                """
                feed_dict = {
                    cnn.input_x: x_batch,
                    cnn.input_y: y_batch,
                    cnn.dropout_keep_prob: FLAGS.dropout_keep_prob
                }
                # _, step, summaries, loss, accuracy,(w,idx) = sess.run(
                #     [train_op, global_step, train_summary_op, cnn.loss, cnn.accuracy,cnn.get_w2v_W()],
                #     feed_dict)
                _, step, summaries, loss, accuracy = sess.run([
                    train_op, global_step, train_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))
                # print w[:2],idx[:2]
                train_summary_writer.add_summary(summaries, step)

            def dev_step(x_batch, y_batch, writer=None):
                """
                Evaluates model on a dev set
                """
                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 = data_helpers.batch_iter(list(zip(x_train, y_train)),
                                              FLAGS.batch_size,
                                              FLAGS.num_epochs)

            def dev_test():
                batches_dev = data_helpers.batch_iter(list(zip(x_dev, y_dev)),
                                                      FLAGS.batch_size, 1)
                for batch_dev in batches_dev:
                    x_batch_dev, y_batch_dev = zip(*batch_dev)
                    dev_step(x_batch_dev,
                             y_batch_dev,
                             writer=dev_summary_writer)

            # 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)
                # Training loop. For each batch...
                if current_step % FLAGS.evaluate_every == 0:
                    print("\nEvaluation:")
                    dev_test()

                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))
Ejemplo n.º 6
0
        # Get the placeholders from the graph by name
        input_x = graph.get_operation_by_name("input_x").outputs[0]

        dropout_keep_prob = graph.get_operation_by_name(
            "dropout_keep_prob").outputs[0]

        # Tensors we want to evaluate
        predictions = graph.get_operation_by_name(
            "output/predictions").outputs[0]

        scores = graph.get_operation_by_name("output/scores").outputs[0]

        x_test, y_test = load_data(w2v_model, 1290)
        # Generate batches for one epoch
        batches = data_helpers.batch_iter(list(x_test),
                                          FLAGS.batch_size,
                                          1,
                                          shuffle=False)

        # Collect the predictions here
        all_predictions = []
        all_probabilities = None

        for index, x_test_batch in enumerate(batches):
            batch_predictions = sess.run([predictions, scores], {
                input_x: x_test_batch,
                dropout_keep_prob: 1.0
            })
            all_predictions = np.concatenate(
                [all_predictions, batch_predictions[0]])

            probabilities = softmax(batch_predictions[1])
Ejemplo n.º 7
0
 def dev_test():
     batches_dev = data_helpers.batch_iter(list(zip(x_dev, y_dev)),
                                           batch_size, 1)
     for batch_dev in batches_dev:
         x_batch_dev, y_batch_dev = zip(*batch_dev)
         dev_step(x_batch_dev, y_batch_dev, writer=None)
Ejemplo n.º 8
0
def train():
    # Training
    # ==================================================
    # x_train, x_dev, y_train, y_dev ,vocab_size= load_data(w2v_model)
    with tf.Graph().as_default():
        session_conf = tf.ConfigProto(
            allow_soft_placement=allow_soft_placement,
            log_device_placement=log_device_placement)
        sess = tf.Session(config=session_conf)
        with sess.as_default():
            if (nn_type == "text_cnn"):
                nn = TextCNN(model_type=model_type,
                             sequence_length=x_train.shape[1],
                             num_classes=y_train.shape[1],
                             vocab_size=vocab_size,
                             embedding_size=embedding_dim,
                             filter_sizes=list(
                                 map(int, filter_sizes.split(","))),
                             num_filters=num_filters,
                             l2_reg_lambda=l2_reg_lambda)
            elif nn_type == "text_birnn":
                nn = TextBiRNN(model_type=model_type,
                               sequence_length=x_train.shape[1],
                               num_classes=y_train.shape[1],
                               vocab_size=vocab_size,
                               embedding_size=embedding_dim,
                               rnn_size=128,
                               num_layers=3,
                               l2_reg_lambda=l2_reg_lambda)
            elif nn_type == "text_rnn":
                nn = TextRNN(model_type=model_type,
                             sequence_length=x_train.shape[1],
                             num_classes=y_train.shape[1],
                             vocab_size=vocab_size,
                             embedding_size=embedding_dim,
                             rnn_size=128,
                             num_layers=3,
                             l2_reg_lambda=l2_reg_lambda)
            elif nn_type == "text_rcnn":
                nn = TextBiRNN(model_type=model_type,
                               sequence_length=x_train.shape[1],
                               num_classes=y_train.shape[1],
                               vocab_size=vocab_size,
                               embedding_size=embedding_dim,
                               rnn_size=128,
                               num_layers=3,
                               l2_reg_lambda=l2_reg_lambda)
            elif nn_type == "text_dnn":
                nn = TextDNN(model_type=model_type,
                             sequence_length=x_train.shape[1],
                             num_classes=y_train.shape[1],
                             vocab_size=vocab_size,
                             embedding_size=embedding_dim,
                             hidden_layes=2,
                             hidden_size=128,
                             l2_reg_lambda=l2_reg_lambda)
            elif nn_type == "text_fasttext":
                nn = TextFast(model_type=model_type,
                              sequence_length=x_train.shape[1],
                              num_classes=y_train.shape[1],
                              vocab_size=vocab_size,
                              embedding_size=embedding_dim,
                              l2_reg_lambda=l2_reg_lambda)

            # 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(nn.loss)
            train_op = optimizer.apply_gradients(grads_and_vars,
                                                 global_step=global_step)

            # Initialize all variables
            sess.run(tf.global_variables_initializer())

            def train_step(x_batch, y_batch):
                """
                A single training step
                """
                feed_dict = {
                    nn.input_x: x_batch,
                    nn.input_y: y_batch,
                    nn.dropout_keep_prob: dropout_keep_prob
                }
                _, step, loss, accuracy = sess.run(
                    [train_op, global_step, nn.loss, nn.accuracy], feed_dict)

                time_str = datetime.datetime.now().isoformat()
                print("{}: step {}, loss {:g}, acc {:g}".format(
                    time_str, step, loss, accuracy))
                # print w[:2],idx[:2]
                # train_summary_writer.add_summary(summaries, step)

            def dev_step(x_batch, y_batch, writer=None):
                """
                Evaluates model on a dev set
                """
                feed_dict = {
                    nn.input_x: x_batch,
                    nn.input_y: y_batch,
                    nn.dropout_keep_prob: 1.0
                }

                step, loss, accuracy = sess.run(
                    [global_step, nn.loss, nn.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 = data_helpers.batch_iter(list(zip(x_train, y_train)),
                                              batch_size, num_epochs)

            def dev_test():
                batches_dev = data_helpers.batch_iter(list(zip(x_dev, y_dev)),
                                                      batch_size, 1)
                for batch_dev in batches_dev:
                    x_batch_dev, y_batch_dev = zip(*batch_dev)
                    dev_step(x_batch_dev, y_batch_dev, writer=None)

            # Training loop. For each batch...
            for batch in batches:
                x_batch, y_batch = zip(*batch)
                train_step(x_batch, y_batch)
Ejemplo n.º 9
0
def train(w2v_model,
          epsilon=8 / 255,
          alpha=10 / 255,
          K=5,
          is_free=False,
          mode=None):
    # Training
    # ==================================================
    x_train, x_dev, y_train, y_dev, vocab_size = load_data(w2v_model)
    # fgsm = FGSM()
    with tf.Graph().as_default():
        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():
            cnn = TextCNN(w2v_model,
                          sequence_length=x_train.shape[1],
                          num_classes=y_train.shape[1],
                          vocab_size=vocab_size,
                          embedding_size=FLAGS.embedding_dim,
                          filter_sizes=list(
                              map(int, FLAGS.filter_sizes.split(","))),
                          num_filters=FLAGS.num_filters,
                          l2_reg_lambda=FLAGS.l2_reg_lambda,
                          sess=sess,
                          mode=mode)

            # 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)

            # 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.summary.histogram(
                        "{}/grad/hist".format(v.name), g)
                    sparsity_summary = tf.summary.scalar(
                        "{}/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.summary.merge(grad_summaries)

            # Output directory for models and summaries
            timestamp = str(int(time.time()))
            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.summary.scalar("loss", cnn.loss)
            acc_summary = tf.summary.scalar("accuracy", cnn.accuracy)

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

            # Dev summaries
            dev_summary_op = tf.summary.merge([loss_summary, acc_summary])
            dev_summary_dir = os.path.join(out_dir, "summaries", "dev")
            dev_summary_writer = tf.summary.FileWriter(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)

            # Write vocabulary
            # vocab_processor.save(os.path.join(out_dir, "vocab"))

            # Initialize all variables
            sess.run(tf.global_variables_initializer())

            def train_step(x_batch, y_batch):
                """
                A single training step
                """
                feed_dict = {
                    cnn.input_x: x_batch,
                    cnn.input_y: y_batch,
                    cnn.dropout_keep_prob: FLAGS.dropout_keep_prob
                }
                _, step, summaries, loss, accuracy, scores, l2_loss, predictions = sess.run(
                    [
                        train_op, global_step, train_summary_op, cnn.loss,
                        cnn.accuracy, cnn.scores, cnn.l2_loss, cnn.predictions
                    ], feed_dict)
                time_str = datetime.datetime.now().isoformat()
                p, r, micro_p, micro_r, micro_f1, macro_f1, accuracy1 = fastF1(
                    np.argmax(y_batch, axis=1), predictions, FLAGS.num_class)
                print(
                    "train {}: step {}, loss {:g}, P: {:.2f}%, R: {:.2f}%, micro_p: {:.2f}%, micro_r: {:.2f}%,Micro_f1: {:.2f}%, Macro_f1: {:.2f}%, Accuracy: {:.2f}%"
                    .format(time_str, step, loss, p, r, micro_p, micro_r,
                            micro_f1, macro_f1, accuracy1))
                train_summary_writer.add_summary(summaries, step)

            def dev_step(x_batch, y_batch, writer=None):
                """
                Evaluates model on a dev set
                """
                feed_dict = {
                    cnn.input_x: x_batch,
                    cnn.input_y: y_batch,
                    cnn.dropout_keep_prob: 1.0
                }
                step, summaries, loss, accuracy, predictions = sess.run([
                    global_step, dev_summary_op, cnn.loss, cnn.accuracy,
                    cnn.predictions
                ], feed_dict)
                time_str = datetime.datetime.now().isoformat()

                # p, r, micro_p, micro_r, micro_f1, macro_f1, accuracy1 = fastF1(np.argmax(y_batch, axis=1), predictions,
                #                                                                2)
                print("test {}: step {}, loss {:g}".format(
                    time_str, step, loss))
                if writer:
                    writer.add_summary(summaries, step)
                return predictions

            # Generate batches
            batches = batch_iter(list(zip(x_train, y_train)), FLAGS.batch_size,
                                 FLAGS.num_epochs)
            num_batches_per_epoch = int(
                (len(list(zip(x_train, y_train))) - 1) / FLAGS.batch_size) + 1

            def dev_test():
                batches_dev = batch_iter(list(zip(x_dev, y_dev)),
                                         FLAGS.batch_size, 1)
                prediction_all = []
                y_true_all = []
                for batch_dev in batches_dev:
                    x_batch_dev, y_batch_dev = zip(*batch_dev)
                    predictions = dev_step(x_batch_dev,
                                           y_batch_dev,
                                           writer=dev_summary_writer)
                    prediction_all.extend(predictions)
                    y_true_all.extend(np.argmax(y_batch_dev, axis=1).tolist())
                p, r, micro_p, micro_r, micro_f1, macro_f1, accuracy1 = fastF1(
                    y_true_all, prediction_all, FLAGS.num_class)
                print(
                    "test P: {:.2f}%, R: {:.2f}%, micro_p: {:.2f}%, micro_r: {:.2f}%,Micro_f1: {:.2f}%, Macro_f1: {:.2f}%, Accuracy: {:.2f}%"
                    .format(p, r, micro_p, micro_r, micro_f1, macro_f1,
                            accuracy1))
                # dev_step(x_dev, y_dev, writer=dev_summary_writer)

            # Training loop. For each batch...
            num_batches_per_epoch_ = num_batches_per_epoch
            if is_free:
                num_batches_per_epoch_ = int(num_batches_per_epoch / K)
            count_num = 0
            for batch in batches:
                x_batch, y_batch = zip(*batch)
                if count_num == num_batches_per_epoch_:
                    break
                if is_free:
                    for i in range(K):
                        train_step(x_batch, y_batch)
                else:
                    train_step(x_batch, y_batch)
                current_step = tf.train.global_step(sess, global_step)
                # Training loop. For each batch...
                # 每50步一次
                if current_step % FLAGS.evaluate_every == 0 and current_step > 0:
                    print("\nEvaluation:")
                    dev_test()

                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))
                if is_free:
                    count_num += 1
Ejemplo n.º 10
0
def train(w2v_model):
    # Training
    x_train, x_dev, y_train, y_dev, vocab_size = load_data(w2v_model)
    with tf.Graph().as_default():  #返回值:返回一个上下文管理器,这个上下管理器使用这个图作为默认的图
        #tf.ConfigProto()配置tf.Session的运算方式,比如gpu运算或者cpu运算
        #allow_soft_placement允许动态分配GPU内存
        #log_device_placement打印出设备信息
        session_conf = tf.ConfigProto(
            allow_soft_placement=FLAGS.allow_soft_placement,
            log_device_placement=FLAGS.log_device_placement)
        sess = tf.Session(config=session_conf)

        #分类算法TextCNN,主要思想是将不同长度的短文作为矩阵输入,
        #使用多个不同size的filter去提取句子中的关键信息,并用于最终的分类
        with sess.as_default():
            cnn = TextCNN(w2v_model,
                          sequence_length=x_train.shape[1],
                          num_classes=y_train.shape[1],
                          vocab_size=vocab_size,
                          embedding_size=FLAGS.embedding_dim,
                          filter_sizes=list(
                              map(int, FLAGS.filter_sizes.split(", "))),
                          num_filters=FLAGS.num_filters,
                          l2_reg_lambda=FLAGS.l2_reg_lambda)

            # 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)

            # 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.summary.histogram(
                        "{}/grad/hist".format(v.name), g)
                    sparsity_summary = tf.summary.scalar(
                        "{}/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.summary.merge(grad_summaries)

            # Output directory for models and summaries
            #模型和摘要的输出目录
            timestamp = str(int(time.time()))
            out_dir = os.path.abspath(os.path.join(os.path.curdir, "runs"))
            print("Writing to {}\n".format(out_dir))

            # Summaries for loss and accuracy
            #生成准确率和损失率标量图

            loss_summary = tf.summary.scalar(
                "loss", cnn.loss)  #tf.summary.scalar()用来显示标量信息,
            acc_summary = tf.summary.scalar("accuracy", cnn.accuracy)

            # Train Summaries训练总结
            train_summary_op = tf.summary.merge(
                [loss_summary, acc_summary, grad_summaries_merged])  #保存信息
            train_summary_dir = os.path.join(out_dir, "summaries",
                                             "train")  #文件路径
            train_summary_writer = tf.summary.FileWriter(
                train_summary_dir, sess.graph)  #指定一个文件用来保存图。
            #下面同理
            # Dev summaries开发总结
            dev_summary_op = tf.summary.merge([loss_summary, acc_summary])
            dev_summary_dir = os.path.join(out_dir, "summaries", "dev")
            dev_summary_writer = tf.summary.FileWriter(dev_summary_dir,
                                                       sess.graph)

            # Checkpoint directory. Tensorflow assumes this directory already exists so we need to create it
            ##检查Checkpoint目录。Tensorflow假设这个目录已经存在,所以我们需要创建它
            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)  #tf.train.Saver()保存和加载模型

            # Write vocabulary
            # vocab_processor.save(os.path.join(out_dir, "vocab"))

            # Initialize all variables初始化所有变量
            #含有tf.Variable的环境下,因为tf中建立的变量是没有初始化的,
            #也就是在debug时还不是一个tensor量,而是一个Variable变量类型
            sess.run(tf.global_variables_initializer())

            #训练步骤
            def train_step(x_batch, y_batch):
                """
                A single training step
                """
                feed_dict = {
                    cnn.input_x: x_batch,
                    cnn.input_y: y_batch,
                    cnn.dropout_keep_prob: FLAGS.dropout_keep_prob
                }
                # _, step, summaries, loss, accuracy, (w, idx) = sess.run(
                #     [train_op, global_step, train_summary_op, cnn.loss, cnn.accuracy, cnn.get_w2v_W()],
                #     feed_dict)
                _, step, summaries, loss, accuracy = sess.run([
                    train_op, global_step, train_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))
                # print w[:2], idx[:2]
                train_summary_writer.add_summary(summaries, step)

            #开发步骤
            def dev_step(x_batch, y_batch, writer=None):
                """
                Evaluates model on a dev set
                评估开发集上的模型
                """
                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 = data_helpers.batch_iter(list(zip(x_train, y_train)),
                                              FLAGS.batch_size,
                                              FLAGS.num_epochs)

            #测试步骤
            def dev_test():
                batches_dev = data_helpers.batch_iter(list(zip(x_dev, y_dev)),
                                                      FLAGS.batch_size, 1)
                for batch_dev in batches_dev:
                    x_batch_dev, y_batch_dev = zip(*batch_dev)
                    dev_step(x_batch_dev,
                             y_batch_dev,
                             writer=dev_summary_writer)

            # 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)  # #获得global_step
                # Training loop. For each batch...
                if current_step % FLAGS.evaluate_every == 0:
                    print("\nEvaluation:")
                    dev_test()

                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))