[自然语言处理]RNN

1 传统RNN模型与LSTM

python 复制代码
import torch
import torch.nn as nn

torch.manual_seed(6)


# todo:基础RNN模型
def dem01():
    '''
    参数1:input_size 每个词的词向量维度(输入层神经元的个数)
    参数2:hidden_size 隐藏层神经元的个数
    参数3:hidden_layer 隐藏层的层数
    '''
    rnn = nn.RNN(5, 6, 1)
    '''
    参数1:sequence_length 每个样本的句子长度
    参数2:batch_size 每个批次的样本数量
    参数3:input_size 每个词的词向量维度(输入层神经元的个数)
    '''
    input = torch.randn(1, 3, 5)
    '''
    参数1:hidden_layer 隐藏层的层数
    参数2:batch_size 每个批次的样本数量
    参数3:hidden_size 隐藏层神经元的个数
    '''
    h0 = torch.randn(1, 3, 6)
    output, hn = rnn(input, h0)
    print(f'output {output}')
    print(f'hn {hn}')
    print(f'RNN模型 {rnn}')


# todo:增加输入的sequence_length
def dem02():
    '''
    参数1:input_size 每个词的词向量维度(输入层神经元的个数)
    参数2:hidden_size 隐藏层神经元的个数
    参数3:hidden_layer 隐藏层的层数
    '''
    rnn = nn.RNN(5, 6, 1)
    '''
    参数1:sequence_length 每个样本的句子长度
    参数2:batch_size 每个批次的样本数量
    参数3:input_size 每个词的词向量维度(输入层神经元的个数)
    '''
    input = torch.randn(4, 3, 5)
    '''
    参数1:hidden_layer 隐藏层的层数
    参数2:batch_size 每个批次的样本数量
    参数3:hidden_size 隐藏层神经元的个数
    '''
    h0 = torch.randn(1, 3, 6)
    output, hn = rnn(input, h0)
    print(f'output {output}')
    print(f'hn {hn}')
    print(f'RNN模型 {rnn}')


# todo:增加隐藏层的个数
def dem03():
    '''
    参数1:input_size 每个词的词向量维度(输入层神经元的个数)
    参数2:hidden_size 隐藏层神经元的个数
    参数3:hidden_layer 隐藏层的层数
    '''
    rnn = nn.RNN(5, 6, 2)
    '''
    参数1:sequence_length 每个样本的句子长度
    参数2:batch_size 每个批次的样本数量
    参数3:input_size 每个词的词向量维度(输入层神经元的个数)
    '''
    input = torch.randn(4, 3, 5)
    '''
    参数1:hidden_layer 隐藏层的层数
    参数2:batch_size 每个批次的样本数量
    参数3:hidden_size 隐藏层神经元的个数
    '''
    h0 = torch.randn(2, 3, 6)
    output, hn = rnn(input, h0)
    print(f'output {output}')
    print(f'hn {hn}')
    print(f'RNN模型 {rnn}')


# todo:一个一个地向模型输入单词-全零初始化
def dem04_1():
    '''
    参数1:input_size 每个词的词向量维度(输入层神经元的个数)
    参数2:hidden_size 隐藏层神经元的个数
    参数3:hidden_layer 隐藏层的层数
    '''
    rnn = nn.RNN(5, 6, 1)
    '''
    参数1:sequence_length 每个样本的句子长度
    参数2:batch_size 每个批次的样本数量
    参数3:input_size 每个词的词向量维度(输入层神经元的个数)
    '''
    input = torch.randn(4, 1, 5)
    print(f'input {input}')
    '''
    参数1:hidden_layer 隐藏层的层数
    参数2:batch_size 每个批次的样本数量
    参数3:hidden_size 隐藏层神经元的个数
    '''
    # 每个样本一次性输入神经网络
    hn = torch.zeros(1, 1, 6)
    print(f'hn1 {hn}')
    output, hn = rnn(input, hn)
    print(f'output1 {output}')
    print(f'hn1 {hn}')
    print(f'RNN模型1 {rnn}')
    print('*' * 80)
    # 每个样本逐词送入神经网络
    hn = torch.zeros(1, 1, 6)
    print(f'hn2 {hn}')
    for i in range(4):
        tmp = input[i][0]
        print(f'tmp.shape {tmp.shape}')
        output, hn = rnn(tmp.unsqueeze(0).unsqueeze(0), hn)
        print(f'{i}-output {output}')
        print(f'{i}-hn {hn}')


# todo:一个一个地向模型输入单词-全一初始化
def dem04_2():
    '''
    参数1:input_size 每个词的词向量维度(输入层神经元的个数)
    参数2:hidden_size 隐藏层神经元的个数
    参数3:hidden_layer 隐藏层的层数
    '''
    rnn = nn.RNN(5, 6, 1)
    '''
    参数1:sequence_length 每个样本的句子长度
    参数2:batch_size 每个批次的样本数量
    参数3:input_size 每个词的词向量维度(输入层神经元的个数)
    '''
    input = torch.randn(4, 1, 5)
    print(f'input {input}')
    '''
    参数1:hidden_layer 隐藏层的层数
    参数2:batch_size 每个批次的样本数量
    参数3:hidden_size 隐藏层神经元的个数
    '''
    # 每个样本一次性输入神经网络
    hn = torch.ones(1, 1, 6)
    print(f'hn1 {hn}')
    output, hn = rnn(input, hn)
    print(f'output1 {output}')
    print(f'hn1 {hn}')
    print(f'RNN模型1 {rnn}')
    print('*' * 80)
    # 每个样本逐词送入神经网络
    hn = torch.ones(1, 1, 6)
    print(f'hn2 {hn}')
    for i in range(4):
        tmp = input[i][0]
        print(f'tmp.shape {tmp.shape}')
        output, hn = rnn(tmp.unsqueeze(0).unsqueeze(0), hn)
        print(f'{i}-output {output}')
        print(f'{i}-hn {hn}')


# todo:一个一个地向模型输入单词-随机初始化
def dem04_3():
    '''
    参数1:input_size 每个词的词向量维度(输入层神经元的个数)
    参数2:hidden_size 隐藏层神经元的个数
    参数3:hidden_layer 隐藏层的层数
    '''
    rnn = nn.RNN(5, 6, 1)
    '''
    参数1:sequence_length 每个样本的句子长度
    参数2:batch_size 每个批次的样本数量
    参数3:input_size 每个词的词向量维度(输入层神经元的个数)
    '''
    input = torch.randn(4, 1, 5)
    print(f'input {input}')
    '''
    参数1:hidden_layer 隐藏层的层数
    参数2:batch_size 每个批次的样本数量
    参数3:hidden_size 隐藏层神经元的个数
    '''
    # 每个样本一次性输入神经网络
    hn = torch.randn(1, 1, 6)
    print(f'hn1 {hn}')
    output, hn = rnn(input, hn)
    print(f'output1 {output}')
    print(f'hn1 {hn}')
    print(f'RNN模型1 {rnn}')
    print('*' * 80)
    # 每个样本逐词送入神经网络
    hn = torch.randn(1, 1, 6)
    print(f'hn2 {hn}')
    for i in range(4):
        tmp = input[i][0]
        print(f'tmp.shape {tmp.shape}')
        output, hn = rnn(tmp.unsqueeze(0).unsqueeze(0), hn)
        print(f'{i}-output {output}')
        print(f'{i}-hn {hn}')


# todo:设置batch_first=True
def dem05():
    '''
    参数1:input_size 每个词的词向量维度(输入层神经元的个数)
    参数2:hidden_size 隐藏层神经元的个数
    参数3:hidden_layer 隐藏层的层数
    '''
    rnn = nn.RNN(5, 6, 1, batch_first=True)
    '''
    参数1:batch_size 每个批次的样本数量
    参数2:sequence_length 每个样本的句子长度
    参数3:input_size 每个词的词向量维度(输入层神经元的个数)
    '''
    input = torch.randn(3, 4, 5)
    '''
    参数1:hidden_layer 隐藏层的层数
    参数2:batch_size 每个批次的样本数量
    参数3:hidden_size 隐藏层神经元的个数
    '''
    h0 = torch.randn(1, 3, 6)
    output, hn = rnn(input, h0)
    print(f'output {output}')
    print(f'hn {hn}')
    print(f'RNN模型 {rnn}')


# todo:基础LSTM模型
def dem06_1():
    '''
    参数1:input_size 每个词的词向量维度(输入层神经元的个数)
    参数2:hidden_size 隐藏层神经元的个数
    参数3:hidden_layer 隐藏层的层数
    '''
    rnn = nn.LSTM(5, 6, 2)
    '''
    参数1:batch_size 每个批次的样本数量
    参数2:sequence_length 每个样本的句子长度
    参数3:input_size 每个词的词向量维度(输入层神经元的个数)
    '''
    input = torch.randn(1, 3, 5)
    '''
    参数1:hidden_layer 隐藏层的层数
    参数2:batch_size 每个批次的样本数量
    参数3:hidden_size 隐藏层神经元的个数
    '''
    h0 = torch.randn(2, 3, 6)
    '''
    参数1:hidden_layer 隐藏层的层数
    参数2:batch_size 每个批次的样本数量
    参数3:hidden_size 隐藏层神经元的个数
    '''
    c0 = torch.randn(2, 3, 6)
    output, (hn, cn) = rnn(input, (h0, c0))
    print(f'output {output}')
    print(f'hn {hn}')
    print(f'cn {cn}')

# todo:双向LSTM模型
def dem06_2():
    '''
    参数1:input_size 每个词的词向量维度(输入层神经元的个数)
    参数2:hidden_size 隐藏层神经元的个数
    参数3:hidden_layer 隐藏层的层数
    '''
    rnn = nn.LSTM(5, 6, 2,bidirectional=True)
    '''
    参数1:batch_size 每个批次的样本数量
    参数2:sequence_length 每个样本的句子长度
    参数3:input_size 每个词的词向量维度(输入层神经元的个数)
    '''
    input = torch.randn(1, 3, 5)
    '''
    参数1:hidden_layer 隐藏层的层数
    参数2:batch_size 每个批次的样本数量
    参数3:hidden_size 隐藏层神经元的个数
    '''
    h0 = torch.randn(4, 3, 6)
    '''
    参数1:hidden_layer 隐藏层的层数
    参数2:batch_size 每个批次的样本数量
    参数3:hidden_size 隐藏层神经元的个数
    '''
    c0 = torch.randn(4, 3, 6)
    output, (hn, cn) = rnn(input, (h0, c0))
    print(f'output {output}')
    print(f'hn {hn}')
    print(f'cn {cn}')


if __name__ == '__main__':
    # dem01()
    # dem02()
    # dem03()
    # dem04_1()
    # dem04_2()
    # dem04_3()
    # dem05()
    # dem06_1()
    dem06_2()
bash 复制代码
D:\nlplearning\nlpbase\python.exe D:\nlpcoding\rnncode.py 
output tensor([[[ 0.0207, -0.1121, -0.0706,  0.1167, -0.3322, -0.0686],
         [ 0.1256,  0.1328,  0.2361,  0.2237, -0.0203, -0.2709],
         [-0.2668, -0.2721, -0.2168,  0.4734,  0.2420,  0.0349]]],
       grad_fn=<MkldnnRnnLayerBackward0>)
hn tensor([[[ 0.1501, -0.2106,  0.0213,  0.1309,  0.3074, -0.2038],
         [ 0.3639, -0.0394, -0.1912,  0.1282,  0.0369, -0.1094],
         [ 0.1217, -0.0517,  0.1884, -0.1100, -0.5018, -0.4512]],

        [[ 0.0207, -0.1121, -0.0706,  0.1167, -0.3322, -0.0686],
         [ 0.1256,  0.1328,  0.2361,  0.2237, -0.0203, -0.2709],
         [-0.2668, -0.2721, -0.2168,  0.4734,  0.2420,  0.0349]]],
       grad_fn=<StackBackward0>)
cn tensor([[[ 0.2791, -0.7362,  0.0501,  0.2612,  0.4655, -0.2338],
         [ 0.7902, -0.0920, -0.4955,  0.3865,  0.0868, -0.1612],
         [ 0.2312, -0.3736,  0.4033, -0.1386, -1.0151, -0.5971]],

        [[ 0.0441, -0.2279, -0.1483,  0.3397, -0.5597, -0.4339],
         [ 0.2154,  0.4119,  0.4723,  0.4731, -0.0284, -1.1095],
         [-0.5016, -0.5146, -0.4286,  1.5299,  0.5992,  0.1224]]],
       grad_fn=<StackBackward0>)

Process finished with exit code 0

2 GRU

python 复制代码
import torch
import torch.nn as nn


# todo:基础GRU
def dem01():
    gru = nn.GRU(5, 6, 1)
    input = torch.randn(4, 3, 5)
    h0 = torch.randn(1, 3, 6)
    output, hn = gru(input, h0)
    print(f'output {output}')
    print(f'hn {hn}')


if __name__ == '__main__':
    dem01()
相关推荐
憨波个1 小时前
【语音识别】Conformer: Convolution-augmented Transformer for Speech Recognition
人工智能·深度学习·transformer·语音识别
动物园猫1 小时前
睡岗检测数据集分享(适用于YOLO系列深度学习分类检测任务)
深度学习·yolo·分类
z小猫不吃鱼1 小时前
10 GPT-3 论文精读:Few-shot Learning 为什么会出现?
人工智能·语言模型·自然语言处理·gpt-3
大模型最新论文速读2 小时前
05-29 · LLM 最新论文速览
论文阅读·人工智能·深度学习·机器学习·自然语言处理
杀生丸学AI2 小时前
【三维重建】RT-Splatting:基于3DGS的联合反射-透射建模(CVPR 2026)
人工智能·深度学习·3d·三维重建·高斯泼溅·动态重建·镜面反射
AI人工智能+2 小时前
赋能智慧监管:基于深度学习的特种行业许可证全要素精准识别系统
计算机视觉·自然语言处理·ocr·特种行业许可证识别
weixin_468466852 小时前
图像分类技术落地应用与实战指南
人工智能·深度学习·ai·分类·数据挖掘·图像分类·模型部署
MediaTea2 小时前
人工智能通识课:深度学习
人工智能·深度学习
wabs6662 小时前
本科毕业设计项目——基于RAG与大语言模型的408问答系统设计与实现【用户端的提问逻辑是怎么实现的?】
人工智能·语言模型·自然语言处理
探物 AI3 小时前
Mamba 会替代 Transformer 成为 AI 的下一代底层模型吗?
人工智能·深度学习·transformer