Exemple #1
0
def main():
    train_data, valid_data, test_data, _ = reader.ptb_raw_data(DATA_PATH)

    # 计算一个epoch需要训练的次数
    train_data_len = len(train_data)
    train_batch_len = train_data_len // TRAIN_BATCH_SIZE
    train_epoch_size = (train_batch_len - 1) // TRAIN_NUM_STEP

    valid_data_len = len(valid_data)
    valid_batch_len = valid_data_len // EVAL_BATCH_SIZE
    valid_epoch_size = (valid_batch_len - 1) // EVAL_NUM_STEP

    test_data_len = len(test_data)
    test_batch_len = test_data_len // EVAL_BATCH_SIZE
    test_epoch_size = (test_batch_len - 1) // EVAL_NUM_STEP

    initializer = tf.random_uniform_initializer(-0.05, 0.05)
    with tf.variable_scope("language_model",
                           reuse=None,
                           initializer=initializer):
        train_model = PTBModel(True, TRAIN_BATCH_SIZE, TRAIN_NUM_STEP)

    with tf.variable_scope("language_model",
                           reuse=True,
                           initializer=initializer):
        eval_model = PTBModel(False, EVAL_BATCH_SIZE, EVAL_NUM_STEP)

    # 训练模型。
    with tf.Session() as session:
        tf.global_variables_initializer().run()

        train_queue = reader.ptb_producer(train_data, train_model.batch_size,
                                          train_model.num_steps)
        eval_queue = reader.ptb_producer(valid_data, eval_model.batch_size,
                                         eval_model.num_steps)
        test_queue = reader.ptb_producer(test_data, eval_model.batch_size,
                                         eval_model.num_steps)

        coord = tf.train.Coordinator()
        threads = tf.train.start_queue_runners(sess=session, coord=coord)

        for i in range(NUM_EPOCH):
            print("In iteration: %d" % (i + 1))
            run_epoch(session, train_model, train_queue, train_model.train_op,
                      True, train_epoch_size)

            valid_perplexity = run_epoch(session, eval_model, eval_queue,
                                         tf.no_op(), False, valid_epoch_size)
            print("Epoch: %d Validation Perplexity: %.3f" %
                  (i + 1, valid_perplexity))

        test_perplexity = run_epoch(session, eval_model, test_queue,
                                    tf.no_op(), False, test_epoch_size)
        print("Test Perplexity: %.3f" % test_perplexity)

        coord.request_stop()
        coord.join(threads)
Exemple #2
0
 def __init__(self, config, data, name=None):
     self.batch_size = batch_size = config.batch_size
     self.num_steps = num_steps = config.num_steps
     self.epoch_size = ((len(data) // batch_size) - 1) // num_steps
     self.input_data,self.targets=reader.ptb_producer(data,batch_size,\
                                                 num_steps,name=name)
     '''
Exemple #3
0
 def __init__(self, config, data, name=None):
     self.batch_size = batch_size = config.batch_size  # 读取config中的batch_size,num_steps到本地变量
     self.num_steps = num_steps = config.num_steps
     self.epoch_size = (
         (len(data) // batch_size) - 1) // num_steps  # 全部样本被训练的次数
     self.input_data, self.targets = reader.ptb_producer(data,
                                                         batch_size,
                                                         num_steps,
                                                         name=name)
Exemple #4
0
def main(_):
    # 原始数据
    train_data, valid_data, test_data, _ = reader.ptb_raw_data(DATA_PATH)

    # 计算一个epoch需要训练的次数
    train_data_len = len(train_data)  # 数据集的大小
    train_batch_len = train_data_len // TRAIN_BATCH_SIZE  # batch的个数
    train_epoch_size = (train_batch_len - 1) // TRAIN_NUM_STEP  # 该epoch的训练次数

    valid_data_len = len(valid_data)
    valid_batch_len = valid_data_len // EVAL_BATCH_SIZE
    valid_epoch_size = (valid_batch_len - 1) // EVAL_NUM_STEP

    test_data_len = len(test_data)
    test_batch_len = test_data_len // EVAL_BATCH_SIZE
    test_epoch_size = (test_batch_len - 1) // EVAL_NUM_STEP

    # 定义初始化函数
    initializer = tf.random_uniform_initializer(-0.05, 0.05)

    # 定义训练用的模型
    with tf.variable_scope('language_model',
                           reuse=None,
                           initializer=initializer):
        train_model = PTBModel(True, TRAIN_BATCH_SIZE, TRAIN_NUM_STEP)

    # 定义评估用的模型
    with tf.variable_scope('language_model',
                           reuse=True,
                           initializer=initializer):
        eval_model = PTBModel(False, EVAL_BATCH_SIZE, EVAL_NUM_STEP)

    # 生成数据队列,必须放在开启多线程之前
    train_queue = reader.ptb_producer(train_data, train_model.batch_size,
                                      train_model.num_steps)
    valid_queue = reader.ptb_producer(valid_data, eval_model.batch_size,
                                      eval_model.num_steps)
    test_queue = reader.ptb_producer(test_data, eval_model.batch_size,
                                     eval_model.num_steps)

    with tf.Session() as sess:
        tf.global_variables_initializer().run()

        # 开启多线程从而支持ptb_producer()使用tf.train.range_input_producer()
        coord = tf.train.Coordinator()
        threads = tf.train.start_queue_runners(sess=sess, coord=coord)

        # 使用训练数据训练模型
        for i in range(NUM_EPOCH):
            print('In iteration: %d' % (i + 1))
            run_epoch(sess, train_model, train_queue, train_model.train_op,
                      True, train_epoch_size)  # 训练模型
            valid_perplexity = run_epoch(sess, eval_model, valid_queue,
                                         tf.no_op(), False,
                                         valid_epoch_size)  # 使用验证数据评估模型
            print('Epoch: %d Validation Perplexity: %.3f' %
                  (i + 1, valid_perplexity))

        # 使用测试数据测试模型
        test_perplexity = run_epoch(sess, eval_model, test_queue, tf.no_op(),
                                    False, test_epoch_size)
        print('Test Perplexity: %.3f' % test_perplexity)

        saver = tf.train.Saver()
        saver.save(sess, "save_path/model2")
        # 停止所有线程
        coord.request_stop()
        coord.join(threads)
Exemple #5
0
    # 计算一个epoch需要训练的次数
    train_data_len = len(train_data)  # 数据集的大小
    train_batch_len = train_data_len // config.batch_size  # batch的个数
    train_epoch_size = (train_batch_len - 1) // config.num_steps  # 该epoch的训练次数

    valid_data_len = len(valid_data)
    valid_batch_len = valid_data_len // eval_config.batch_size
    valid_epoch_size = (valid_batch_len - 1) // eval_config.num_steps

    test_data_len = len(test_data)
    test_batch_len = test_data_len // eval_config.batch_size
    test_epoch_size = (test_batch_len - 1) // eval_config.num_steps

    # 生成数据队列,必须放在开启多线程之前
    train_queue = reader.ptb_producer(train_data, config.batch_size, config.num_steps)
    valid_queue = reader.ptb_producer(valid_data, eval_config.batch_size, eval_config.num_steps)
    test_queue = reader.ptb_producer(test_data, eval_config.batch_size, eval_config.num_steps)

    # train_data = datagen4lstm.BatchGenerator(datagen4lstm.train_text, config.batch_size, config.num_steps)
    # valid_data = datagen4lstm.BatchGenerator(datagen4lstm.valid_text, eval_config.batch_size, eval_config.num_steps)

    with tf.Graph().as_default(), tf.Session() as session:
        initializer = tf.random_uniform_initializer(-config.init_scale,  # 定义如何对参数变量初始化
                                                    config.init_scale)
        with tf.variable_scope("model", reuse=None, initializer=initializer):
            m = PTBModel(is_training=True, config=config)  # 训练模型, is_trainable=True
        with tf.variable_scope("model", reuse=True, initializer=initializer):
            mvalid = PTBModel(is_training=False, config=config)  # 交叉检验和测试模型,is_trainable=False
            mtest = PTBModel(is_training=False, config=eval_config)