• 《动手学深度学习 Pytorch版》 9.3 深度循环神经网络


    将多层循环神经网络堆叠在一起,通过对几个简单层的组合,产生一个灵活的机制。其中的数据可能与不同层的堆叠有关。

    在这里插入图片描述

    9.3.1 函数依赖关系

    将深度架构中的函数依赖关系形式化,第 l l l 个隐藏层的隐状态表达式为:

    H t ( l ) = ϕ l ( H t ( l − 1 ) W x h ( l ) + H t − 1 ( l ) W h h ( l ) + b h ( l ) ) \boldsymbol{H}^{(l)}_t=\phi_l(\boldsymbol{H}^{(l-1)}_t\boldsymbol{W}^{(l)}_{xh}+\boldsymbol{H}^{(l)}_{t-1}\boldsymbol{W}^{(l)}_{hh}+\boldsymbol{b}^{(l)}_h) Ht(l)=ϕl(Ht(l1)Wxh(l)+Ht1(l)Whh(l)+bh(l))

    参数字典:

    • ϕ l \phi_l ϕl 表示第 l l l 个隐藏层的激活函数

    • X t ∈ R n × d \boldsymbol{X}_t\in\R^{n\times d} XtRn×d 表示小批量输入

      • n n n 表示样本个数

      • d d d 表示输入个数

    • H t ( l ) ∈ R n × h \boldsymbol{H}^{(l)}_{t}\in\R^{n\times h} Ht(l)Rn×h 表示 l t h l^{th} lth 隐藏层 ( l = 1 , … , L ) (l=1,\dots,L) (l=1,,L) 的隐状态

      • h h h 表示隐藏单元个数

      • 设置 H t ( 0 ) = X t \boldsymbol{H}^{(0)}_{t}=\boldsymbol{X}_{t} Ht(0)=Xt

    • O t ∈ R n × q \boldsymbol{O}_{t}\in\R^{n\times q} OtRn×q 表示输出层变量

      • q q q 表示输出数
    • W x h ( l ) , W h h ( l ) ∈ R h × h \boldsymbol{W}^{(l)}_{xh},\boldsymbol{W}^{(l)}_{hh}\in\R^{h\times h} Wxh(l),Whh(l)Rh×h 表示第 l l l 个隐藏层的权重参数

    • b h ( l ) ∈ R 1 × h \boldsymbol{b}^{(l)}_h\in\R^{1\times h} bh(l)R1×h 表示第 l l l 个隐藏层的偏重参数

    最后,输出层的计算仅基于第 l l l 个隐藏层最终的隐状态:

    O t = H t L W h q + b q \boldsymbol{O}_t=\boldsymbol{H}^{L}_t\boldsymbol{W}_{hq}+\boldsymbol{b}_q Ot=HtLWhq+bq

    其中 W h q ∈ R h × q \boldsymbol{W}_{hq}\in\R^{h\times q} WhqRh×q b q ∈ R 1 × q \boldsymbol{b}_q\in\R^{1\times q} bqR1×q 表示输出层的模型参数

    9.3.2 简洁实现

    手撸多层循环神经网络有点过于麻烦了,在此仅简单实现。

    import torch
    from torch import nn
    from d2l import torch as d2l
    
    • 1
    • 2
    • 3
    batch_size, num_steps = 32, 35
    train_iter, vocab = d2l.load_data_time_machine(batch_size, num_steps)
    
    • 1
    • 2
    vocab_size, num_hiddens, num_layers = len(vocab), 256, 2  # 用 num_layers 来设定隐藏层数
    num_inputs = vocab_size
    device = d2l.try_gpu()
    lstm_layer = nn.LSTM(num_inputs, num_hiddens, num_layers)
    model = d2l.RNNModel(lstm_layer, len(vocab))
    model = model.to(device)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    9.3.3 训练与预测

    num_epochs, lr = 500, 2
    d2l.train_ch8(model, train_iter, vocab, lr*1.0, num_epochs, device)  # 多了一层后训练速度大幅下降
    
    • 1
    • 2
    perplexity 1.0, 116173.5 tokens/sec on cuda:0
    time travelleryou can show black is white by argument said filby
    travelleryou can show black is white by argument said filby
    
    • 1
    • 2
    • 3

    在这里插入图片描述

    练习

    (1)基于我们在 8.5 节中讨论的单层实现,尝试从零开始实现两层循环神经网络。

    batch_size, num_steps = 32, 35
    train_iter, vocab = d2l.load_data_time_machine(batch_size, num_steps)
    
    def get_params_bilayer(vocab_size, num_hiddens, device):
        num_inputs = num_outputs = vocab_size
    
        def normal(shape):
            return torch.randn(size=shape, device=device) * 0.01
    
        # 隐藏层1参数
        W_xh1 = normal((num_inputs, num_hiddens))
        W_hh1 = normal((num_hiddens, num_hiddens))
        b_h1 = torch.zeros(num_hiddens, device=device)
        # 新增隐藏层2参数
        W_hh2 = normal((num_hiddens, num_hiddens))
        b_h2 = torch.zeros(num_hiddens, device=device)
        # 输出层参数
        W_hq = normal((num_hiddens, num_outputs))
        b_q = torch.zeros(num_outputs, device=device)
        # 附加梯度
        params = [W_xh1, W_hh1, b_h1, W_hh2, b_h2, W_hq, b_q]
        for param in params:
            param.requires_grad_(True)
        return params
    
    def init_rnn_state_bilayer(batch_size, num_hiddens, device):
        return (torch.zeros((batch_size, num_hiddens), device=device),
                torch.zeros((batch_size, num_hiddens), device=device))  # 新增第二个隐状态初始化张量
    
    def rnn_bilayer(inputs, state, params):  # inputs的形状:(时间步数量,批量大小,词表大小)
        W_xh1, W_hh1, b_h1, W_hh2, b_h2, W_hq, b_q = params  # 新增第二层参数
        H1, H2 = state
        outputs = []
        for X in inputs:  # X的形状:(批量大小,词表大小) 前面转置是为了这里遍历
            H1 = torch.tanh(torch.mm(X, W_xh1) + torch.mm(H1, W_hh1) + b_h1)  # 计算隐状态1
            H2 = torch.tanh(torch.mm(H1, W_hh2) + b_h2)  # 计算隐状态2
            Y = torch.mm(H2, W_hq) + b_q  # 计算输出
            outputs.append(Y)
        return torch.cat(outputs, dim=0), (H1, H2)  # 沿时间步拼接
    
    num_hiddens = 512
    net_rnn_bilayer = d2l.RNNModelScratch(len(vocab), num_hiddens, d2l.try_gpu(), get_params_bilayer,
                          init_rnn_state_bilayer, rnn_bilayer)
    num_epochs, lr = 500, 1
    d2l.train_ch8(net_rnn_bilayer, train_iter, vocab, lr, num_epochs, d2l.try_gpu())
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    perplexity 1.0, 63514.3 tokens/sec on cuda:0
    time travelleryou can show black is white by argument said filby
    travelleryou can show black is white by argument said filby
    
    • 1
    • 2
    • 3

    在这里插入图片描述


    (2)在本节训练模型中,比较使用门控循环单元替换长短期记忆网络后模型的精确度和训练速度。

    vocab_size, num_hiddens, num_layers = len(vocab), 256, 2  # 用 num_layers 来设定隐藏层数
    num_inputs = vocab_size
    device = d2l.try_gpu()
    # lstm_layer = nn.LSTM(num_inputs, num_hiddens, num_layers)
    # model = d2l.RNNModel(lstm_layer, len(vocab))
    gru_layer = nn.GRU(num_inputs, num_hiddens)
    model_gru = d2l.RNNModel(gru_layer, len(vocab))
    model_gru = model_gru.to(device)
    
    num_epochs, lr = 500, 2
    d2l.train_ch8(model_gru, train_iter, vocab, lr*1.0, num_epochs, device)  # 换 gru 后更快了
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    perplexity 1.0, 230590.6 tokens/sec on cuda:0
    time traveller for so it will be convenient to speak of himwas e
    travelleryou can show black is white by argument said filby
    
    • 1
    • 2
    • 3

    在这里插入图片描述


    (3)如果增加训练数据,能够将困惑度降到多低?

    已经是 1 了,没得降了。


    (4)在为文本建模时,是否可以将不同作者的源数据合并?有何优劣呢?

    不同作者的数据源之间可能没有什么关系,拼在一起可能效果反而下降。

  • 相关阅读:
    Windows安装cygwin + swoole,并配置crontab定时任务
    Mac电脑搭建前端项目环境,并适配老项目
    网络编程与HTTP协议
    advanced installer 14.9重新封包
    操作系统备份
    【深度学习】单隐层神经网络
    使用Docker部署Gitlab的记录
    PyCharm 常用快捷键
    Java中方法的使用
    [ansible] playbook运用
  • 原文地址:https://blog.csdn.net/qq_43941037/article/details/133931633