Tensorflow- 循环神经网络(LSTM)

tensorflow中文社区对官方文档进行了完整翻译。鉴于官方更新不少内容,而现有的翻译基本上都已过时。故本人对更新后文档进行翻译工作,纰漏之处请大家指正。(如需了解其他方面知识,可参阅以下Tensorflow系列文章)。

Tensorflow系列文章

介绍

可以在 this great article 查看循环神经网络(RNN)以及 LSTM 的介绍。

语言模型

此教程将展示如何在高难度的语言模型中训练循环神经网络。该问题的目标是获得一个能确定语句概率的概率模型。为了做到这一点,通过之前已经给出的词语来预测后面的词语。我们将使用 PTB(Penn Tree Bank) 数据集,这是一种常用来衡量模型的基准,同时它比较小而且训练起来相对快速。

语言模型是很多有趣难题的关键所在,比如语音识别,机器翻译,图像字幕等。它很有意思–可以参看 here

本教程的目的是重现 Zaremba et al., 2014 的成果,他们在 PTB 数据集上得到了很棒的结果。

教程文件

本教程使用的文件在 models/tutorials/rnn/ptb 目录下,请点击 TensorFlow models repo 进行下载。

FilePurpose
ptb_word_lm.pyThe code to train a language model on the PTB dataset.
reader.pyThe code to read the dataset.

下载及准备数据

点击下载后,进行解压,本教程所需要的数据在里面的 data/ 目录下。

该数据集已经预先处理过并且包含了全部的 10000 个不同的词语,其中包括语句结束标记符,以及标记稀有词语的特殊符号 (<unk>) 。我们在 reader.py 中转换所有的词语,让他们各自有唯一的整型标识符,便于神经网络处理。

模型

LSTM

模型的核心由一个 LSTM 单元组成,其可以在某时刻处理一个词语,以及计算语句中下个所有可能的词语的概率 。网络的存储状态由一个零矢量初始化并在读取每一个词语后更新。而且,由于计算上的原因,我们将以 batch_size 为最小批量来处理数据。在这个例子中,current_batch_of_words 并不是对应着“一个句子”。

在每个batch中的单词应该对应时间 t ,Tensorflow会在每个batch中自动对梯度(gradient,此处不知翻译是否准确)进行求和。

举例说明:

 t=0  t=1    t=2  t=3     t=4
[The, brown, fox, is,     quick]
[The, red,   fox, jumped, high]

words_in_dataset[0] = [The, The]
words_in_dataset[1] = [fox, fox]
words_in_dataset[2] = [is, jumped]
words_in_dataset[3] = [quick, high]
num_batches = 4, batch_size = 2, time_steps = 5

基本的伪代码如下:

words_in_dataset = tf.placeholder(tf.float32, [num_batches, batch_size, num_features])
lstm = tf.contrib.rnn.BasicLSTMCell(lstm_size)
# Initial state of the LSTM memory.
hidden_state = tf.zeros([batch_size, lstm.state_size])
current_state = tf.zeros([batch_size, lstm.state_size])
state = hidden_state, current_state
probabilities = []
loss = 0.0
for current_batch_of_words in words_in_dataset:
    # The value of state is updated after processing each batch of words.
    output, state = lstm(current_batch_of_words, state)

    # The LSTM output can be used to make next word predictions
    logits = tf.matmul(output, softmax_w) + softmax_b
    probabilities.append(tf.nn.softmax(logits))
    loss += loss_function(probabilities, target_words)
截断反向传播

通过设计,递归神经网络(RNN)的输出依赖于任意遥远的输入(作者理解:比如很久之前的单词对现在的输出有影响)。不幸的是,这使得反向传播计算很困难。为了使学习过程易于处理,创建一个 “unroll” 版本的网络是很常见的做法,该版本包含了LSTM固定数目(num_steps)的输入和输出。然后将模型训练成RNN的有限近似。这可以通过每次输入长度num_steps的输入来实现,并在每个输入块后执行反向传播。

一个简化版的用于计算图创建的截断反向传播代码:

# Placeholder for the inputs in a given iteration.
words = tf.placeholder(tf.int32, [batch_size, num_steps])

lstm = tf.contrib.rnn.BasicLSTMCell(lstm_size)
# Initial state of the LSTM memory.
initial_state = state = tf.zeros([batch_size, lstm.state_size])

for i in range(num_steps):
    # The value of state is updated after processing each batch of words.
    output, state = lstm(words[:, i], state)

    # The rest of the code.
    # ...

final_state = state

下面展现如何实现迭代整个数据集:

# A numpy array holding the state of LSTM after each batch of words.
numpy_state = initial_state.eval()
total_loss = 0.0
for current_batch_of_words in words_in_dataset:
    numpy_state, current_loss = session.run([final_state, loss],
        # Initialize the LSTM state from the previous iteration.
        feed_dict={initial_state: numpy_state, words: current_batch_of_words})
    total_loss += current_loss
输入

在输入 LSTM 前,词语 ID 被嵌入到了一个密集的表示中(查看 Vector Representations of Words)。这种方式允许模型高效地表示词语,也便于写代码:

# embedding_matrix is a tensor of shape [vocabulary_size, embedding size]
word_embeddings = tf.nn.embedding_lookup(embedding_matrix, word_ids)

嵌入的矩阵会被随机地初始化,模型会学会通过数据分辨不同词语的意思。

损失函数

我们想使目标词语的平均负对数概率最小
《Tensorflow- 循环神经网络(LSTM)》

实现起来并非很难,而且函数 sequence_loss_by_example 已经有了,可以直接使用。

论文中的典型衡量标准是每个词语的平均困惑度(perplexity),计算式为
《Tensorflow- 循环神经网络(LSTM)》

同时我们会观察训练过程中的困惑度值(perplexity)。

多个 LSTM 层堆叠

要想给模型更强的表达能力,可以添加多层 LSTM 来处理数据。第一层的输出作为第二层的输入,以此类推。

类 MultiRNNCell 可以无缝的将其实现:

def lstm_cell():
  return tf.contrib.rnn.BasicLSTMCell(lstm_size)
stacked_lstm = tf.contrib.rnn.MultiRNNCell(
    [lstm_cell() for _ in range(number_of_layers)])

initial_state = state = stacked_lstm.zero_state(batch_size, tf.float32)
for i in range(num_steps):
    # The value of state is updated after processing each batch of words.
    output, state = stacked_lstm(words[:, i], state)

    # The rest of the code.
    # ...

final_state = state

运行代码

在运行代码之前,下载PTB数据集(见本教程开头)。然后,将PTB数据集提取到你的 home directory

tar xvfz simple-examples.tgz -C $HOME

(注意:如果你是windows系统,你可能需要用 other tools.)

现在,从github上clone the TensorFlow models repo ,通过下面的命令行运行:

cd models/tutorials/rnn/ptbpython ptb_word_lm.py --data_path=$HOME/simple-examples/data/ --model=small

教程代码中有 3 个支持的模型配置参数:”small”, “medium” 和 “large”。它们指的是 LSTM 的大小,以及用于训练的超参数集。

模型越大,得到的结果应该更好。在测试集中 small 模型应该可以达到低于 120 的困惑度(perplexity),large 模型则是低于 80,但它可能花费数小时来训练。

除此之外?

还有几个优化模型的技巧没有提到,包括:

  • 随时间降低学习率
  • LSTM 层间 dropout.

继续学习和更改代码以进一步改善模型吧。

原文:Recurrent Neural Networks 翻译:周乘

    原文作者:周乘
    原文地址: https://www.jianshu.com/p/262da1cd2eb2
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞