【模型学习-RNN】Pytorch、循环神经网络、RNN、参数详解、原理介绍

前言

RNN循环神经网络个人学习心得,如有不对的地方,欢迎指正。

背景介绍

​ 假定一个化工工业生产过程,主体对象是一个化学反应罐。通过传感器记录了流量、温度、压力、液位等13个辅助变量在不同时刻的数值,同时记录了该罐子在不同时刻的的产品指标(关键变量)数值。

​ 现有10000个时刻的上述数据,则数据维度为:辅助变量,[10000, 13];关键变量:[10000, 1]。现要求:

  1. 利用历史时刻的辅助变量数据和关键变量数据,在给定未来辅助变量的前提下,预测关键变量的数值。
  2. 假定生产过程稳定,利用历史时刻的辅助变量数据,预测未来时刻的辅助变量数据。

RNN简要介绍

基础结构

内容参考

structure-1

image-20230412163724947

structure-2

image-20230412163807063

structure-3

image-20230412163834957

上述三种网络结构是等价的,但同时也是RNN中较为特殊的一种,即输入层和隐含层和输出层是一一对应的

变量理解

以背景介绍中的第二问举例:假定要用历史时刻的50条数据,来预测未来时刻的50条数据,即下边描述中的n=50

  1. 上述图中的 x 1 , x 2 , . . . , x n x_1,x_2,...,x_n x1,x2,...,xn表示的就是第 n n n时刻的输入数据(辅助变量数据)
  2. 可直接把上述图中的隐含层变量 h 1 , h 2 , . . . , h n h_1,h_2,...,h_n h1,h2,...,hn作为网络的输出,即代表网络对未来 n n n个时刻内的辅助变量的预测(若并非n个时刻预测n个时刻的情况,后边再介绍变体RNN结构)

网络参数

内容参考

image-20230412165329268

变量介绍

  • 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网络的共享单元,也是网络需要训练学习到的参数

数学关系

  1. t时刻隐含层状态

h t = ϕ ( U x t + W h t − 1 + b ) h_t=\phi \left( Ux_t+Wh_{t-1}+b \right) ht=ϕ(Uxt+Wht1+b)

ϕ \phi ϕ激活函数,一般选择 t a n h tanh tanh b b b是对应偏执向量

(当t=1时, h 0 h_0 h0是没有的,可以人为给定,也可以默认设置,即矩阵元素都为0)

  1. t时刻的网络输出

o t = V h t + c o_t=Vh_t+c ot=Vht+c

  1. 如果模型为分类任务,可在后边接一个softmax函数

y ^ t = s o f t max ⁡ ( o t ) \hat{y}_t=soft\max \left( o_t \right) y^t=softmax(ot)

  1. 如果模型为预测任务,且数据归一化到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的变体结构

内容参考

image-20230412171737539

one to one

最简单的一种,没有时序信息的关联,单一时刻输入,得到单一时刻输出

one to many

  1. 仅在开始时刻输入x

image-20230412172436010

  1. 在每个阶段都输入x

image-20230412172504528

可用于类别内容生成:输入音乐类别x,输出对应类别的音乐句子

many to one

image-20230412172834017

可用于序列内容判断,类别划分

many to many

N to N

image-20230412173142660

N to M

image-20230412173227552

image-20230412173236428

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网络结构

image-20230412191225487

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
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值