前言
RNN循环神经网络个人学习心得,如有不对的地方,欢迎指正。
背景介绍
假定一个化工工业生产过程,主体对象是一个化学反应罐。通过传感器记录了流量、温度、压力、液位等13个辅助变量在不同时刻的数值,同时记录了该罐子在不同时刻的的产品指标(关键变量)数值。
现有10000个时刻的上述数据,则数据维度为:辅助变量,[10000, 13];关键变量:[10000, 1]。现要求:
- 利用历史时刻的辅助变量数据和关键变量数据,在给定未来辅助变量的前提下,预测关键变量的数值。
- 假定生产过程稳定,利用历史时刻的辅助变量数据,预测未来时刻的辅助变量数据。
RNN简要介绍
基础结构
structure-1
structure-2
structure-3
上述三种网络结构是等价的,但同时也是RNN中较为特殊的一种,即输入层和隐含层和输出层是一一对应的
变量理解
以背景介绍中的第二问举例:假定要用历史时刻的50条数据,来预测未来时刻的50条数据,即下边描述中的n=50
- 上述图中的 x 1 , x 2 , . . . , x n x_1,x_2,...,x_n x1,x2,...,xn表示的就是第 n n n时刻的输入数据(辅助变量数据)
- 可直接把上述图中的隐含层变量 h 1 , h 2 , . . . , h n h_1,h_2,...,h_n h1,h2,...,hn作为网络的输出,即代表网络对未来 n n n个时刻内的辅助变量的预测(若并非n个时刻预测n个时刻的情况,后边再介绍变体RNN结构)
网络参数
变量介绍
- x t x_t xt: t t t时刻的输入变量
- h t h_t ht: t t t时刻的隐藏单元状态
- o t o_t ot: t t t时刻的网络输入
- y t y_t yt: t t t时刻的标签(待预测的数据)
- L t L_t Lt: t t t时刻的损失函数
参数介绍
U , W , V U, W, V U,W,V:均为网络权值,是整个RNN网络的共享单元,也是网络需要训练学习到的参数
数学关系
- t时刻隐含层状态
h t = ϕ ( U x t + W h t − 1 + b ) h_t=\phi \left( Ux_t+Wh_{t-1}+b \right) ht=ϕ(Uxt+Wht−1+b)
ϕ \phi ϕ激活函数,一般选择 t a n h tanh tanh; b b b是对应偏执向量
(当t=1时, h 0 h_0 h0是没有的,可以人为给定,也可以默认设置,即矩阵元素都为0)
- t时刻的网络输出
o t = V h t + c o_t=Vh_t+c ot=Vht+c
- 如果模型为分类任务,可在后边接一个softmax函数
y ^ t = s o f t max ( o t ) \hat{y}_t=soft\max \left( o_t \right) y^t=softmax(ot)
- 如果模型为预测任务,且数据归一化到0-1,可以接一个sigmoid函数
y ^ t = s i g m o i d ( o t ) \hat{y}_t=sigmoid\left( o_t \right) y^t=sigmoid(ot)
RNN的变体结构
one to one
最简单的一种,没有时序信息的关联,单一时刻输入,得到单一时刻输出
one to many
- 仅在开始时刻输入x
- 在每个阶段都输入x
可用于类别内容生成:输入音乐类别x,输出对应类别的音乐句子
many to one
可用于序列内容判断,类别划分
many to many
N to N
N to M
N to M又被称为Encoder-Decoder结构
Pytorch建立RNN网络
torch.nn.RNN()
参数介绍
input_size: The number of expected features in the input `x`
- 输入变量x的维度,例如北京介绍中的数据,维度就是13
hidden_size: The number of features in the hidden state `h`
- 隐含层特征的维度,要么参考别人的结构设置,要么自行设置
num_layers: Number of recurrent layers. E.g., setting ``num_layers=2`` would mean stacking two RNNs together to form a `stacked RNN`, with the second RNN taking in outputs of the first RNN and computing the final results. Default: 1
- RNN网络堆叠的层数
nonlinearity: The non-linearity to use. Can be either ``'tanh'`` or ``'relu'``. Default: ``'tanh'``
- RNN cell 单元之间相互连接的的激活函数类型
bias: If ``False``, then the layer does not use bias weights `b_ih` and `b_hh`. Default: ``True``
- 是否需要偏执向量
batch_first: If ``True``, then the input and output tensors are provided as `(batch, seq, feature)` instead of `(seq, batch, feature)`. Note that this does not apply to hidden or cell states. See the Inputs/Outputs sections below for details. Default: ``False``
- 不同的设置影响输入数据的维度结构
dropout: If non-zero, introduces a `Dropout` layer on the outputs of each RNN layer except the last layer, with dropout probability equal to :attr:`dropout`. Default: 0
bidirectional: If ``True``, becomes a bidirectional RNN. Default: ``False``
- 是否设置为双向RNN网络
num_layers = 2 的RNN网络结构
RNN模型输入参数
Input: input, h_0
当batch_first=False
- input:[sequence_length, batch_size, input_size]
# 此时的batch_size与dataloader的batch_size不是一回事,若sequence_length是x1,...,x50,batch_size=2表示有两组x1, ..., x50的时序数据
# 可以理解为拿昨天和今天同时序时刻的数据,叠加在一起,训练网络了
当batch_first=True
- input:[batch_size, sequence_length, input_size]
h_0:[D*num_layers, batch_size, hidden_size]
- 当bidirectional=True,D=2
- 当bidirectional=False,D=1
RNN模型输出参数
Outputs:output,h_n
当batch_first=False
- output:[sequence_length, batch_size, D*output_size]
当batch_first=True
- output:[batch_size, sequence_length, D*output_size]
h_n:[D*num_layers, batch_size, hidden_size]
- 当bidirectional=True,D=2
- 当bidirectional=False,D=1