简单循环神经网络

简单循环神经网络

文章目录

  • 简单循环神经网络
    • RNN的结构
    • 简单循环网络
    • 循环神经网络的通用近似定理
    • pytorch中rnn的使用
      • 参数
      • 输入
      • 输出
      • 示例
    • 参考

在前馈神经网络中,信息的传递是单向的,因此前馈神经网络难以处理时序序列这种会依赖于过去一段时间输出的的任务,如视频、文本、语音等。而且前馈神经网络的输入尺寸是固定的,但时序序列是不固定的,此时循环神经网络应运而生。

循环神经网络(Recurrent Neural Network,RNN)是一类具有短期记忆能力的神经网络.在循环神经网络中,神经元不但可以接受其他神经元的信息,也 可以接受自身的信息,形成具有环路的网络结构。

RNN的结构

下图为循环神经网络的示例,其中“延时器”为一个虚拟单元,记录神经元的最近一次(或几次)活性值, h t h_t ht也称为状态隐状态。循环神经网络的隐藏层的值不仅仅取决于当前这次的输入 x x x,还取决于上一次隐藏层的值。延迟器就是隐藏层上一次的值作为这一次的输入的权重,也就是将前一次的输出结果带入到当前次中。

简单循环神经网络_第1张图片

给定 t t t时刻的输入 x x x,循环神经网络通过公式 h t = f ( x t , h t − 1 ) h_t=f(x_t,h_t-1) ht=f(xt,ht1)更新带反馈边的隐藏层的活性值 h t h_t ht,其中 h 0 = 0 h_0=0 h0=0 f ( ⋅ ) f(\cdot) f()是一个非线性函数,可以是一个前馈网络。

将上图按照时间线展开,在"what time is it"这个例子中,通过下面这张图我们可以看出,循环神经网络前面的结果对后面产生了影响。

简单循环神经网络_第2张图片

由于循环神经网络具有短期记忆能力,相当于存储装置,因此其计算能力十分强大.理论上,循环神经网络可以近似任意的非线性动力系统.前馈神经网络可以模拟任何连续函数,而循环神经网络可以模拟任何程序.

动力系统:

动力系统(Dynamical System)是一个数学上的概念,指系统状态按照一定的规律随时间 变化的系统.具体讲, 动力系统是使用一个函数来描述一个给定 空间(如某个物理系统的状态空间)中所有点随时间的变化情况. 生活中很多现象(比 如钟摆晃动、台球轨迹等)都可以动力系统来描述。

简单循环网络

简单循环网络(Simple Recurrent Network,SRN)是一个非常简单的循环神经网络,只有一个隐藏层的神经网络.在一个两层的前馈神经网络中,连接存在相邻的层与层之间,隐藏层的节点之间是无连接的。而简单循环网络增加了从隐藏层到隐藏层的反馈连接。

t t t时刻的输入向量为 x t x_t xt h t h_t ht表示 t t t时刻的隐状态,则简单神经网络在时刻 t t t的更新公式为
z t = U h t − 1 + W x t + b h t = f ( z t ) z_t=Uh_{t-1}+Wx_t+b \\ h_t=f(z_t) zt=Uht1+Wxt+bht=f(zt)
其中 z t z_t zt为隐藏层的净输入, U U U状态-状态权重矩阵, W W W状态-输入权重矩阵, b b b为偏置, f ( ⋅ ) f(\cdot) f()是非线性激活函数,也可直接写为
h t = f ( U h t − 1 + W x t + b ) h_t=f(Uh_{t-1}+Wx_t+b) ht=f(Uht1+Wxt+b)

循环神经网络的通用近似定理

如果一个完全连接的循环神经网络有足够数量的sigmoid型隐藏神经元,它可以以任意的准确率去近似任何一个非线性动力系统
h t = g ( h t − 1 , x t ) y t = o ( h t ) h_t=g(h_{t-1},x_t) \\ y_t = o(h_t) ht=g(ht1,xt)yt=o(ht)

其中h_t为每个时刻的隐状态,x_t 是外部输入, ( ⋅ ) (⋅) g()是可测的状态转换函数, ( ⋅ ) (⋅) o()是连续输出函数,并且对状态空间的紧致性没有限制.

pytorch中rnn的使用

torch.nn.RNN(*args, **kwargs)

其内部使用tanh或者relu激活函数,使用哪个在参数中体现

参数

  • input_size – 输入 x x x的维度
  • hidden_size – 隐状态 h h h的维度
  • num_layers – RNN层的个数(在竖直方向堆叠的多个相同个数单元的层数,可以看下图的实例,当num_layers为3时,输入到输出的竖直方向会有三个单元),默认为1.
  • nonlinearity – 决定是用tanh还是relu. Can be either 'tanh' or 'relu'. Default: 'tanh'
  • bias – If False, then the layer does not use bias weights b_ih and b_hh. Default: True
  • batch_first – 是否将batchsize作为第一位。 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 dropout. Default: 0
  • bidirectional – If True, becomes a bidirectional RNN. Default: False

简单循环神经网络_第3张图片

输入

input, h_0
  • input: (seq_len, batch_size, input_size),batch_first为true,就是(batch_size, seq_len, input_size)
  • h_0: (num_direction*num_layers, batch_size, hidden_size),如果是双向RNN,num_direction等于2,否则等于1,hidden_size即输出的隐状态 h t h_t ht的维度大小

输出

output, h_n
  • output: (seq_len, batch_size, num_direction*hidden_size),batch_first=True时同理
  • h_n: (num_direction*num_layers, batch_size, hidden_size)

示例

rnn = nn.RNN(10, 20, 2)  #input_size=10 hidden_size=20 num_layers=2
input = torch.randn(5, 3, 10) # seq_len=5 batch_size=3 input_size=20
h0 = torch.randn(2, 3, 20) # (num_direction=1)*(num_layers=2)=2 batch_size=3 hidden_size=20
output, hn = rnn(input, h0)
print(output.shape)
print(hn.shape)

torch.Size([5, 3, 20]) # seq_len=5 batch_size=3 hidden_size=20

torch.Size([2, 3, 20]) # num_layers=2 batch_size=3 hidden_size=20

参考

https://easyai.tech/ai-definition/rnn/

你可能感兴趣的:(rnn,人工智能,深度学习)