一、学习单步的RNN:RNNCell
如果要学习TensorFlow中的RNN,第一站应该就是去了解“RNNCell”,它是TensorFlow中实现RNN的基本单元,每个RNNCell都有一个call方法,使用方式是:(output, next_state) = call(input, state)。
也就是说,每调用一次RNNCell的call方法,就相当于在时间上“推进了一步”,这就是RNNCell的基本功能。
在代码实现上,RNNCell只是一个抽象类,我们用的时候都是用的它的两个子类BasicRNNCell和BasicLSTMCell。顾名思义,前者是RNN的基础类,后者是LSTM的基础类。这里推荐大家阅读其源码实现,一开始并不需要全部看一遍,只需要看下RNNCell、BasicRNNCell、BasicLSTMCell这三个类的注释部分,应该就可以理解它们的功能了。
除了call方法外,对于RNNCell,还有两个类属性比较重要:
state_size
output_size
前者是隐层的大小,后者是输出的大小。比如我们通常是将一个batch送入模型计算,设输入数据的形状为(batch_size, input_size),那么计算时得到的隐层状态就是(batch_size, state_size),输出就是(batch_size, output_size)。
import tensorflow as tf import numpy as np cell = tf.nn.rnn_cell.BasicRNNCell(num_units=128) # state_size = 128 print(cell.state_size) # 128 inputs = tf.placeholder(np.float32, shape=(32, 100)) # 32 是 batch_size h0 = cell.zero_state(32, np.float32) # 通过zero_state得到一个全0的初始状态,形状为(batch_size, state_size) output, h1 = cell.call(inputs, h0) #调用call函数 print(h1.shape) # (32, 128)
对于BasicLSTMCell,情况有些许不同,因为LSTM可以看做有两个隐状态h和c,对应的隐层就是一个Tuple,每个都是(batch_size, state_size)的形状:
import tensorflow as tf import numpy as np lstm_cell = tf.nn.rnn_cell.BasicLSTMCell(num_units=128) inputs = tf.placeholder(np.float32, shape=(32, 100)) # 32 是 batch_size h0 = lstm_cell.zero_state(32, np.float32) # 通过zero_state得到一个全0的初始状态 output, h1 = lstm_cell.call(inputs, h0) print(h1.h) # shape=(32, 128) print(h1.c) # shape=(32, 128)
二、学习如何一次执行多步:tf.nn.dynamic_rnn
基础的RNNCell有一个很明显的问题:对于单个的RNNCell,我们使用它的call函数进行运算时,只是在序列时间上前进了一步。比如使用x1、h0得到h1,通过x2、h1得到h2等。这样的h话,如果我们的序列长度为10,就要调用10次call函数,比较麻烦。对此,TensorFlow提供了一个tf.nn.dynamic_rnn函数,使用该函数就相当于调用了n次call函数。即通过{h0,x1, x2, …., xn}直接得{h1,h2…,hn}。
具体来说,设我们输入数据的格式为(batch_size, time_steps, input_size),其中time_steps表示序列本身的长度,如在Char RNN中,长度为10的句子对应的time_steps就等于10。最后的input_size就表示输入数据单个序列单个时间维度上固有的长度。另外我们已经定义好了一个RNNCell,调用该RNNCell的call函数time_steps次,对应的代码就是:
# inputs: shape = (batch_size, time_steps, input_size) # cell: RNNCell # initial_state: shape = (batch_size, cell.state_size)。初始状态。一般可以取零矩阵
# inputs: shape = (batch_size, time_steps, input_size)
# cell: RNNCell
# initial_state: shape = (batch_size, cell.state_size)。初始状态。一般可以取零矩阵
import tensorflow as tf
tf.reset_default_graph()
batch_size = 32 # batch大小
input_size = 100 # 输入向量xt维度
state_size = 128 # 隐藏状态ht维度
time_steps = 10 # 序列长度
inputs = tf.random_normal(shape=[batch_size, time_steps, input_size], dtype=tf.float32)
print("inputs.shape:",inputs.shape)
lstm_cell = tf.nn.rnn_cell.BasicLSTMCell(num_units = state_size)
print(lstm_cell.state_size)
initial_state = lstm_cell.zero_state(batch_size, dtype = tf.float32)
print(initial_state.h, initial_state.c)
outputs, state = tf.nn.dynamic_rnn(lstm_cell, inputs, initial_state = initial_state)
print(outputs)
print(state)
print(state.h, state.c)
三、学习如何堆叠RNNCell:MultiRNNCell
很多时候,单层RNN的能力有限,我们需要多层的RNN。将x输入第一层RNN的后得到隐层状态h,这个隐层状态就相当于第二层RNN的输入,第二层RNN的隐层状态又相当于第三层RNN的输入,以此类推。在TensorFlow中,可以使用tf.nn.rnn_cell.MultiRNNCell函数对RNNCell进行堆叠,相应的示例程序如下:
import tensorflow as tf import numpy as np # 每调用一次这个函数就返回一个BasicRNNCell def get_a_cell(): return tf.nn.rnn_cell.BasicRNNCell(num_units=128) # 用tf.nn.rnn_cell MultiRNNCell创建3层RNN cell = tf.nn.rnn_cell.MultiRNNCell([get_a_cell() for _ in range(3)]) # 3层RNN # 得到的cell实际也是RNNCell的子类 # 它的state_size是(128, 128, 128) # (128, 128, 128)并不是128x128x128的意思 # 而是表示共有3个隐层状态,每个隐层状态的大小为128 print(cell.state_size) # (128, 128, 128) # 使用对应的call函数 inputs = tf.placeholder(np.float32, shape=(32, 100)) # 32 是 batch_size h0 = cell.zero_state(32, np.float32) # 通过zero_state得到一个全0的初始状态 output, h1 = cell.call(inputs, h0) print(h1) # tuple中含有3个32x128的向量