(TensorFlow)使用循环神经网络实现语言模型

零:训练模型从上下文判断下一个单词的选择范围(perplextity),选择范围越小,模型越好

一:训练,验证,测试数据下载
从Tomas Mikolov 网站上的PTB数据下载

二:此次所用的文件在simple-examples.tgz解压后的data文件夹里:ptb.test.text,train.txt,valid三个文件

如图

三:代码

# -*- coding: utf-8 -*-
import numpy as np
import tensorflow as tf
import reader

DATA_PATH = 'F:/PycharmProjects/tmp/model/data'
HIDDEN_SIZE = 200 #隐藏层规模
NUM_LAYERS = 2
VOCAB_SIZE = 10000  #词典规模

LEARNING_RATE = 1.0
TRAIN_BATCH_SIZE = 20
TRAIN_NUM_STEP = 35 #训练数据截断长度

EVAL_BATCH_SIZE = 1
EVAL_NUM_STEP = 1
NUM_EPOCH = 2   #使用训练数据的轮数
KEEP_PROB = 0.5 #节点不被dropout的概率
MAX_GARD_NORM = 5   #用于控制梯度膨胀的参数


class PTBModel(object):
    def __init__(self, is_training, batch_size, num_steps):
        self.batch_size = batch_size
        self.num_steps = num_steps

        self.input_data = tf.placeholder(tf.int32, [batch_size, num_steps])
        self.targets =  tf.placeholder(tf.int32, [batch_size, num_steps])

        #定义使用LSTM结构为循环体结构且使用dropout的深层循环神经网络
        lstm_cell = tf.nn.rnn_cell.BasicLSTMCell(HIDDEN_SIZE)
        if is_training:
            lstm_cell = tf.nn.rnn_cell.DropoutWrapper(
                lstm_cell, output_keep_prob=KEEP_PROB
            )
        cell = tf.nn.rnn_cell.MultiRNNCell([lstm_cell] * NUM_LAYERS)

        self.initial_state = cell.zero_state(batch_size, tf.float32)
        embedding = tf.get_variable('embedding', [VOCAB_SIZE, HIDDEN_SIZE])
        inputs = tf.nn.embedding_lookup(embedding, self.input_data)

        if is_training:
            inputs = tf.nn.dropout(inputs, KEEP_PROB)

        outputs = []
        state = self.initial_state
        with tf.variable_scope('RNN'):
            for time_step in range(num_steps):
                if time_step > 0: tf.get_variable_scope().reuse_variables()
                cell_output, state = cell(inputs[:, time_step, :], state)
                outputs.append(cell_output)

        output = tf.reshape(tf.concat(outputs, 1), [-1, HIDDEN_SIZE])
        weight = tf.get_variable('weight', [HIDDEN_SIZE, VOCAB_SIZE])
        bias = tf.get_variable('bias', [VOCAB_SIZE])
        logits = tf.matmul(output, weight) + bias

        loss = tf.contrib.legacy_seq2seq.sequence_loss_by_example(
            [logits],
            [tf.reshape(self.targets, [-1])],
            [tf.ones([batch_size * num_steps], dtype=tf.float32)]
        )
        self.cost = tf.reduce_sum(loss) / batch_size
        self.final_state = state

        if not is_training: return #只在训练模型时进行反向传播操作

        trainable_variables = tf.trainable_variables()
        grads, _ = tf.clip_by_global_norm(tf.gradients(self.cost, trainable_variables), MAX_GARD_NORM)

        optimizer = tf.train.GradientDescentOptimizer(LEARNING_RATE)
        self.train_op = optimizer.apply_gradients(zip(grads, trainable_variables))


def run_epoch(session, model, data, train_op, output_log):
    total_costs = 0.0
    iters = 0
    state = session.run(model.initial_state)
    for step, (x, y) in enumerate(reader.ptb_iterator(data, model.batch_size, model.num_steps)):
        cost, state, _ = session.run(
            [model.cost, model.final_state, train_op],
            {model.input_data: x, model.targets: y,
             model.initial_state: state}
        )
        total_costs += cost
        iters += model.num_steps

        if output_log and step % 100 == 0:
            print('After %d steps, perplexity is %.3f' % (step, np.exp(total_costs / iters)))
    return np.exp(total_costs / iters)

def main(_):
    train_data, valid_data, test_data, _ = reader.ptb_raw_data(DATA_PATH)
    initializer = tf.random_uniform_initializer(-0.05, 0.05)
    with tf.variable_scope('language_model', reuse=tf.AUTO_REUSE, initializer=initializer):
        train_model = PTBModel(True, TRAIN_BATCH_SIZE, TRAIN_NUM_STEP)

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

    with tf.Session() as sess:
        tf.initialize_all_variables().run()
        for i in range(NUM_EPOCH):
            print('In iteration: %d' % (i+1))
            run_epoch(sess, train_model, train_data, train_model.train_op, True)

            valid_perplexity = run_epoch(sess, eval_model, valid_data, tf.no_op(), False)
            print('Epoch: %d Validation Perplexity: %.3f' % (i+1, valid_perplexity))

        test_perplexity = run_epoch(sess, eval_model, test_data, tf.no_op(), False)
        print('Test:  Test Perplexity: %.3f' % test_perplexity)

if __name__ == '__main__':
    tf.app.run()

导入的reader文件来自于:reader.把reader.py文件于主Python文件置于同一目录下。

四:训练结果
perplexity 最开始值为9982.331,这基本相当于从9982多个单词随机选择下一个单词。训练结束后选择的单词降到了236个。

In iteration: 1
After 0 steps, perplexity is 9982.331
After 100 steps, perplexity is 1373.757
After 200 steps, perplexity is 1014.514
After 300 steps, perplexity is 866.278
After 400 steps, perplexity is 765.193
After 500 steps, perplexity is 693.194
After 600 steps, perplexity is 640.376
After 700 steps, perplexity is 595.449
After 800 steps, perplexity is 555.005
After 900 steps, perplexity is 523.647
After 1000 steps, perplexity is 499.572
After 1100 steps, perplexity is 476.513
After 1200 steps, perplexity is 457.129
After 1300 steps, perplexity is 439.515
Epoch: 1 Validation Perplexity: 293.073
In iteration: 2
After 0 steps, perplexity is 378.070
After 100 steps, perplexity is 265.294
After 200 steps, perplexity is 270.458
After 300 steps, perplexity is 271.947
After 400 steps, perplexity is 268.950
After 500 steps, perplexity is 266.594
After 600 steps, perplexity is 266.106
After 700 steps, perplexity is 263.804
After 800 steps, perplexity is 259.322
After 900 steps, perplexity is 256.499
After 1000 steps, perplexity is 254.953
After 1100 steps, perplexity is 251.484
After 1200 steps, perplexity is 248.905
After 1300 steps, perplexity is 246.169
Epoch: 2 Validation Perplexity: 243.809
Test:  Test Perplexity: 236.948
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 213,928评论 6 493
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 91,192评论 3 387
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 159,468评论 0 349
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 57,186评论 1 286
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 66,295评论 6 386
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 50,374评论 1 292
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,403评论 3 412
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,186评论 0 269
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,610评论 1 306
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,906评论 2 328
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,075评论 1 341
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,755评论 4 337
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,393评论 3 320
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,079评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,313评论 1 267
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 46,934评论 2 365
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 43,963评论 2 351

推荐阅读更多精彩内容