[PyTorch][chapter 44][RNN]

简介

            循环神经网络(Recurrent Neural Network, RNN)是一类以序列(sequence)数据为输入,在序列的演进方向进行递归(recursion)且所有节点(循环单元)按链式连接的递归神经网络(recursive neural network) [1]  。

            对循环神经网络的研究始于二十世纪80-90年代,并在二十一世纪初发展为深度学习(deep learning)算法之一 [2]  ,其中双向循环神经网络(Bidirectional RNN, Bi-RNN)和长短期记忆网络(Long Short-Term Memory networks,LSTM)是常见的循环神经网络 [3]  。


目录:

  1.  模型
  2. Forward
  3. Backward
  4. nn.RNN
  5. nn.RNNCell

一  模型

    [PyTorch][chapter 44][RNN]_第1张图片

                   x_t: t 时刻样本输入\sim R^{n,1}

                   h_t: t 时刻样本隐藏状态\sim R^{m,1}

                   o_t: t时刻输出\sim R^{k,1}

                  \hat{y_t}:  t时刻样本预测类别(只有分类算法才有)\sim R^{k,1}

                  L_t: t 时刻损失函数


二  RNN 前向传播算法 Forward

     2.1   t 时刻隐藏值h_t 更新

             z_t=Ux_t+Wh_{t-1}+b

             h_t=\sigma(z_t)

            其中激活函数\sigma通常用tanh

   2.2   t 时刻输出

           o_t=Vh_t+c

           \hat{y_t}=\sigma(o_t)

           其中激活函数\sigma 为softmax


三 RNN 反向传播算法 BPTT(back-propagation through time)

      3.1 输出层参数v,c梯度

             \frac{\partial L}{\partial v}=\sum_{t=1}^{T}(\hat{y_t}-y_t)h_t^T

             \frac{\partial L}{\partial c}=\sum_{t=1}^{T}(\hat{y_t}-y_t)

      

     3.2  隐藏层参数更新

             定义

               \delta_t=\frac{\partial L}{\partial h_t}

                   =V^T(\hat{y_t}-y_t)+W^Tdiag(1-h_{t+1}^2)\delta_{t+1}

               证明:

                     \delta_{t}=\frac{\partial L_t}{\partial h_t}+(\frac{\partial h_{t+1}}{\partial h_t})^T\frac{\partial L}{\partial h_{t+1}}

                            =(\frac{\partial o_t}{\partial L_t})^T\frac{\partial L_t}{\partial o_t}+(\frac{\partial h_{t+1}}{\partial h_t})^T\delta_{t+1}

                             =V^T(\hat{y_t}-y_t)+(diag(1-h_{t+1}^2)W)^T\delta_{t+1}

                            =V^T(\hat{y_t}-y_t)+W^Tdiag(1-h_{t+1}^2)\delta_{t+1}

                  对于最后一个时刻T

                   \delta_T=V^T(\hat{y_T}-y_T)

          3.3 计算权重系数U,W,b

                   \frac{\partial L}{\partial W}=\sum_t diag(1-h_t^2)\delta_t h_{t-1}^T

                   \frac{\partial L}{\partial U}=\sum_t diag(1-h_t^2)\delta_t x_t^T

                    \frac{\partial L}{\partial b}=\sum_t diag(1-h_t^2)\delta_t


四 nn.RNN 

   这里面介绍PyTorch 使用RNN 类

    [PyTorch][chapter 44][RNN]_第2张图片                   

    4.1 更新规则:

                      h_t= tanh(W_{ih}h_t+W_{ih}b_{ih}+W_{hh}h_{t-1}+b_{hh})

                    [PyTorch][chapter 44][RNN]_第3张图片

                

参数 说明
L 时间序列长度T  or 句子长度为 L
N batch_size 
d 输入特征维度

                  

                     

# -*- coding: utf-8 -*-
"""
Created on Wed Jul 19 15:30:01 2023

@author: chengxf2
"""

import torch
import torch.nn as nn


rnn = nn.RNN(input_size=100, hidden_size=5)
param = rnn._parameters

print("\n 权重系数",param.keys())

print(rnn.weight_ih_l0.shape)

输出:

 [PyTorch][chapter 44][RNN]_第4张图片

 RNN参数说明:

参数

说明

input_size =d

 输入维度

hidden_size=h

隐藏层维度

num_layers

RNN默认是 1 层。该参数大于 1 ,会形成 Stacked RNN,又称多层RNN或深度RNN

nonlinearity

非线性激活函数。可以选择 tanh relu

bias

即偏置。默认启用

batch_first

选择让 batch_size=N 作为输入的形状中的第一个参数。默认是 False,L × N × d 形状

batch_first=True 时, N × L × d

dropout

即是否启用 dropout。如要启用,则应设置 dropout 的概率,此时除最后一层外,RNN的每一层后面都会加上一个dropout层。默认是 0,即不启用

bidirectional

即是否启用双向RNN,默认关闭

 4.2 单层例子

import torch.nn as nn
import torch

rnn = nn.RNN(input_size= 100, hidden_size=20, num_layers=1)

X = torch.randn(10,3,100)

h_0 = torch.zeros(1,3,20)

out,h = rnn(X,h_0)


print("\n out.shape",out.shape)

print("\n h.shape",h.shape)
      

          out: 包含每个时刻的 隐藏值h_t

           h :    最后一个时刻的隐藏值h_T

  4.3  多层RNN

    [PyTorch][chapter 44][RNN]_第5张图片

    把当前的隐藏层输出,作为下一层的输入

 第一个隐藏层输出:

               h_t^1= tanh(x_tW_{ih}^1+h_{t-1}^1W_{hh}^1)

第二个隐藏层输出

            h_t^2=tanh(h_t^1W_{ih}^2+h_{t-1}^2W_{hh}^2)

# -*- coding: utf-8 -*-
"""
Created on Mon Jul 24 11:43:30 2023

@author: chengxf2
"""

import torch.nn as nn
import torch
rnn = nn.RNN(input_size=100,  hidden_size=20, num_layers=2)
print(rnn)

x = torch.randn(10,3,100) #默认是[L,N,d]结构

out,h =rnn(x)

print(out.shape, h.shape)

[PyTorch][chapter 44][RNN]_第6张图片


5  nn.RNNCell

     nn.RNN封装了整个RNN实现的过程, PyTorch 还提供了 nn.RNNCell 可以

自己实现RNN 

[PyTorch][chapter 44][RNN]_第7张图片

                x_t\sim [N, dim]

                h_{t-1} \sim [layers, N, dim]

                h_t=rnnCell(x_t,h_{t-1})

               

   5.1  单层RNN

          

# -*- coding: utf-8 -*-
"""
Created on Mon Jul 24 11:43:30 2023

@author: chengxf2
"""
import torch
from torch import nn

def  main():
    model = nn.RNNCell(input_size=10, hidden_size=20)
    
    h1= torch.zeros(3,20)
    
    trainData = torch.randn(8,3,10)
    
    for xt in trainData:
        
         h1= model(xt,h1)
         
    print(h1.shape)


if __name__ == "__main__":
    
    main()
    

[PyTorch][chapter 44][RNN]_第8张图片

 6.2 多层RNN

# -*- coding: utf-8 -*-
"""
Created on Mon Jul 24 11:43:30 2023

@author: chengxf2
"""
import torch
from torch import nn



def  main():
    layer1 = nn.RNNCell(input_size=40, hidden_size=30)
    layer2 = nn.RNNCell(input_size=30, hidden_size=20)
    
    h1= torch.zeros(3,30)
    h2= torch.zeros(3,20)
    
    
    trainData = torch.randn(8,3,40)
    
    for xt in trainData:
        
         h1=  layer1(xt,h1)
         h2 = layer2(h1,h2)
    
    print(h1.shape)
    print(h2.shape)


if __name__ == "__main__":
    
    main()
    

   [PyTorch][chapter 44][RNN]_第9张图片  

参考:

Pytorch 循环神经网络 nn.RNN() nn.RNNCell() nn.Parameter()不同方法实现_老光头_ME2CS的博客-CSDN博客

你可能感兴趣的:(pytorch,人工智能,python)