【TensorFlow】RNN的用法

本文深入探讨了RNN在TensorFlow中的实现与应用,包括BasicRNNCell、BasicLSTMCell、GRUCell的详细介绍,以及tf.nn.dynamic_rnn、tf.nn.bidirectional_dynamic_rnn和MultiRNNCell的使用案例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

https://blog.youkuaiyun.com/jiaoyangwm/article/details/80011656
https://blog.youkuaiyun.com/mzpmzk/article/details/80573338
https://blog.youkuaiyun.com/u013230189/article/details/82822668
https://blog.youkuaiyun.com/qq_34326845/article/details/84894037

一、单步的RNN:RnnCell
1、tf.contrib.rnn.BasicRnnCell:RNN的一个单元细胞定义
BasicRNNCell是最基本的RNN cell单元。

1)函数介绍

tf.contrib.rnn.BasicRnnCell(num_units,actication=None,reuse=None,name=None)
输入参数:
num_units:RNN层神经元的个数
activation: 内部状态之间的激活函数
reuse: Python布尔值, 描述是否重用现有作用域中的变量
name:操作的名称
输出:
一个隐层神经元数量为 num_units 的 RNN 基本单元(实例化的 cell)

在这里插入图片描述
2)方法介绍

call(input, state)
(output, next_state) = call(input, state)。
每调用一次RNNCell的call方法,就相当于在时间上“推进了一步”,这就是RNNCell的基本功能。
找到源码中BasicRNNCell的call函数实现:

def call(self, inputs, state):
“”“Most basic RNN: output = new_state = act(W * input + U * state + B).”""
output = self._activation(_linear([inputs, state], self._num_units, True))
return output, output
这句“return output, output”说明在BasicRNNCell中,output其实和隐状态的值是一样的。因此,我们还需要额外对输出定义新的变换,才能得到图中真正的输出y。由于output和隐状态是一回事,所以在BasicRNNCell中,state_size永远等于output_size。

3)属性介绍
我们通常是将一个batch送入模型计算,设输入数据的形状为 [batch_size, input_size],那么计算时得到的隐层状态就是[batch_size, state_size],输出就是[batch_size, output_size]。

state_size:等于隐层神经元数量
output_size:输出的大小
注意:在此函数中,state_size 永远等于 output_size。

4)应用实例:

(注意,以下代码都基于TensorFlow最新的1.2版本):

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)

2、tf.contrib.rnn.BasicLSTMCell():LSTM的一个单元细胞定义
BasicLSTMCell类是最基本的LSTM循环神经网络单元。

tf.contrib.rnn.BasicLSTMCell(num_units, forget_bias=1.0, state_is_tuple=True, activation=None, reuse=None, name=None)

输入参数:

num_units: LSTM cell层中的单元数
forget_bias: forget gates中的偏置
state_is_tuple: 设置为True, 返回 (c_state , m_state)的二元组
activation: 状态之间转移的激活函数
reuse: Python布尔值, 描述是否重用现有作用域中的变量
name:操作的名称
输出:

一个隐层神经元数量为 num_units 的 LSTM 基本单元(实例化的 lstm_cell)
2)方法介绍

call(input, state)

(output, next_state) = call(input, state)。
每调用一次RNNCell的call方法,就相当于在时间上“推进了一步”,这就是RNNCell的基本功能。
再来看一下BasicLSTMCell的call函数定义(函数的最后几行):

new_c = (
c * sigmoid(f + self._forget_bias) + sigmoid(i) * self._activation(j))
new_h = self._activation(new_c) * sigmoid(o)

if self._state_is_tuple:
new_state = LSTMStateTuple(new_c, new_h)
else:
new_state = array_ops.concat([new_c, new_h], 1)
return new_h, new_state
我们只需要关注self._state_is_tuple == True的情况,因为self._state_is_tuple == False的情况将在未来被弃用。返回的隐状态是new_c和new_h的组合,而output就是单独的new_h。如果我们处理的是分类问题,那么我们还需要对new_h添加单独的Softmax层才能得到最后的分类概率输出。

源码实现

3)属性介绍

state_size:等于隐层神经元数量
output_size:输出的大小
注意:在此函数中,state_size 永远等于 output_size。

    对于BasicLSTMCell,情况有些许不同,因为LSTM可以看做有两个隐状态h和c,对应的隐层就是一个Tuple,每个都是(batch_size, state_size)的形状:

4)应用实例:

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,我们使用它的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}。

tf.nn.dynamic_rnn(cell, inputs, initial_state=None, sequence_length=None, dtype=None, parallel_iterations=None, swap_memory=False, time_major=False, scope=None)

输入参数:

cell: 一个 RNNCell 实例对象
inputs: RNN 的输入序列
sequence_length: 形状为 [batch_size], 其中的每一个值为 sequence length(即 time_steps)
initial_state: RNN 的初始状态。如果cell.state_size是整数,这是一个[batch_size,cell.state_size]大小的张量;如果cell.state_size是一个元祖,这里应该是张量的元组,每个的大小为[batch_size,s],共有state_size个。
time_major: 输入和输出的大小。默认为 False,输入和输出张量的形状为 [batch_size, max_time, depth];当取 True 的时,输入和输出张量的形状为 [max_time, batch_size, depth]
scope: VariableScope for the created subgraph; defaults to “rnn”.
输出 :

outputs:是 time_steps 步里所有的输出,形状为 [batch_size, max_time, cell.output_size]
state:是最后一步的隐状态,形状为[batch_size, cell.state_size]
2)应用实例:

设我们输入数据的格式为 [ batch_size, time_steps, input_size ],其中time_steps表示序列本身的长度。最后的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)。初始状态。一般可以取零矩阵
outputs, state = tf.nn.dynamic_rnn(cell, inputs, initial_state=initial_state)
此时,得到的outputs就是time_steps步里所有的输出。它的形状为 [ batch_size, time_steps, cell.output_size ]。state是最后一步的隐状态,它的形状为 [batch_size, cell.state_size]。

2、 BLSTM
tf.nn.bidirectional_dynamic_rnn(cell_fw, cell_bw, inputs, initial_state_fw=None, initial_state_bw=None, sequence_length=None, dtype=None, parallel_iterations=None, swap_memory=False, time_major=False, scope=None)

输入参数:

只比上面 1 中多了一个反向的 LSTMCell 实例对象和反向的初始状态;输入 inputs 相同,只是信息是双向传递的
输出 (outputs, output_states) :

outputs:
输出是 time_steps 步里所有的输出, 它是一个元组 (output_fw, output_bw) 包含了前向和后向的输出结果,每一个结果的形状为 [batch_size, max_time, cell_fw.output_size]
It returns a tuple instead of a single concatenated Tensor. If the concatenated one is preferred, the forward and backward outputs can be concatenated as tf.concat(outputs, 2)
output_states:: 是一个元组 (output_state_fw, output_state_bw) ,包含前向和后向的最后一步的状态

三、堆叠RNNCell:MultiRNNCell
问题:单层RNN的能力有限,我们需要多层的RNN。

在TensorFlow中,可以使用tf.nn.rnn_cell.MultiRNNCell函数对RNNCell进行堆叠。

tf.nn.rnn_cell.MultiRNNCell(cells,state_is_tuple=True)

输入参数:

cells: RNNCells的列表,RNN网络将会按照这个顺序搭建.
state_is_tuple: 如果为True, 接受和返回的状态将为一个n元组,其中n = len(cells)。如果为False,这n个元素将会被concat到一起组成states.为False的情况将会被废弃。
输出:

‘outputs’ is a tensor of shape [batch_size, max_time, cell.output_size]
‘state’ is a N-tuple where N is the number of LSTMCells containing a tf.contrib.rnn.LSTMStateTuple for each cell
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的向量
通过MultiRNNCell得到的cell并不是什么新鲜事物,它实际也是RNNCell的子类,因此也有call方法、state_size和output_size属性。同样可以通过tf.nn.dynamic_rnn来一次运行多步。

参考链接:静态和动态RNN如何堆叠多层RNN

注意:

我们一般用到堆叠RNN时,使用的代码是:

def get_a_cell():
return tf.nn.rnn_cell.BasicRNNCell(num_units=128)
cell = tf.nn.rnn_cell.MultiRNNCell([get_a_cell() for _ in range(3)])
这个代码在TensorFlow 1.2中是可以正确使用的。但在之前的版本中(以及网上很多相关教程),实现方式是这样的:

one_cell = tf.nn.rnn_cell.BasicRNNCell(num_units=128)
cell = tf.nn.rnn_cell.MultiRNNCell([one_cell] * 3) # 3层RNN

四、GRU

tf.contrib.rnn.GRUCell
Aliases:

Class tf.contrib.rnn.GRUCell
Class tf.nn.rnn_cell.GRUCell
门控循环单元cell

init(

num_units,

activation=None,

reuse=None,

kernel_initializer=None,

bias_initializer=None,

name=None,

dtype=None

)
参数说明:

num_unit:GRU cell中神经元数量。即隐藏的神经元数量。

activation:使用的激活函数

resue:布尔型,表示是否在现有的scope中重复使用变量。如果不为True,并且现有的scope中已经存在给定的变量,则会产生错误。

Kernel_initializer:可选参数,权重和投影矩阵使用的初始化器。

Bias_initializer:可选参数,偏置使用的初始化器。

name:该层的名称。拥有同样名称的层共享权重,但为了避免错误,一般会使用reuse=True

dtype:该层默认的数据类型。

代码示例:

import tensorflow as tf

batch_size=10

depth=128

output_dim=100

inputs=tf.Variable(tf.random_normal([batch_size,depth]))

previous_state=tf.Variable(tf.random_normal([batch_size,output_dim])) #前一个状态的输出

gruCell=tf.nn.rnn_cell.GRUCell(output_dim)

output,state=gruCell(inputs,previous_state)

print(output)

print(state)
输出:

Tensor(“gru_cell/add:0”, shape=(10, 100), dtype=float32)

Tensor(“gru_cell/add:0”, shape=(10, 100), dtype=float32)

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值