[自然语言处置惩罚]RNN

[复制链接]
发表于 2026-1-12 18:56:58 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×
1 传统RNN模子与LSTM

  1. import torch
  2. import torch.nn as nn
  3. torch.manual_seed(6)
  4. # todo:基础RNN模型
  5. def dem01():
  6.     '''
  7.     参数1:input_size 每个词的词向量维度(输入层神经元的个数)
  8.     参数2:hidden_size 隐藏层神经元的个数
  9.     参数3:hidden_layer 隐藏层的层数
  10.     '''
  11.     rnn = nn.RNN(5, 6, 1)
  12.     '''
  13.     参数1:sequence_length 每个样本的句子长度
  14.     参数2:batch_size 每个批次的样本数量
  15.     参数3:input_size 每个词的词向量维度(输入层神经元的个数)
  16.     '''
  17.     input = torch.randn(1, 3, 5)
  18.     '''
  19.     参数1:hidden_layer 隐藏层的层数
  20.     参数2:batch_size 每个批次的样本数量
  21.     参数3:hidden_size 隐藏层神经元的个数
  22.     '''
  23.     h0 = torch.randn(1, 3, 6)
  24.     output, hn = rnn(input, h0)
  25.     print(f'output {output}')
  26.     print(f'hn {hn}')
  27.     print(f'RNN模型 {rnn}')
  28. # todo:增加输入的sequence_length
  29. def dem02():
  30.     '''
  31.     参数1:input_size 每个词的词向量维度(输入层神经元的个数)
  32.     参数2:hidden_size 隐藏层神经元的个数
  33.     参数3:hidden_layer 隐藏层的层数
  34.     '''
  35.     rnn = nn.RNN(5, 6, 1)
  36.     '''
  37.     参数1:sequence_length 每个样本的句子长度
  38.     参数2:batch_size 每个批次的样本数量
  39.     参数3:input_size 每个词的词向量维度(输入层神经元的个数)
  40.     '''
  41.     input = torch.randn(4, 3, 5)
  42.     '''
  43.     参数1:hidden_layer 隐藏层的层数
  44.     参数2:batch_size 每个批次的样本数量
  45.     参数3:hidden_size 隐藏层神经元的个数
  46.     '''
  47.     h0 = torch.randn(1, 3, 6)
  48.     output, hn = rnn(input, h0)
  49.     print(f'output {output}')
  50.     print(f'hn {hn}')
  51.     print(f'RNN模型 {rnn}')
  52. # todo:增加隐藏层的个数
  53. def dem03():
  54.     '''
  55.     参数1:input_size 每个词的词向量维度(输入层神经元的个数)
  56.     参数2:hidden_size 隐藏层神经元的个数
  57.     参数3:hidden_layer 隐藏层的层数
  58.     '''
  59.     rnn = nn.RNN(5, 6, 2)
  60.     '''
  61.     参数1:sequence_length 每个样本的句子长度
  62.     参数2:batch_size 每个批次的样本数量
  63.     参数3:input_size 每个词的词向量维度(输入层神经元的个数)
  64.     '''
  65.     input = torch.randn(4, 3, 5)
  66.     '''
  67.     参数1:hidden_layer 隐藏层的层数
  68.     参数2:batch_size 每个批次的样本数量
  69.     参数3:hidden_size 隐藏层神经元的个数
  70.     '''
  71.     h0 = torch.randn(2, 3, 6)
  72.     output, hn = rnn(input, h0)
  73.     print(f'output {output}')
  74.     print(f'hn {hn}')
  75.     print(f'RNN模型 {rnn}')
  76. # todo:一个一个地向模型输入单词-全零初始化
  77. def dem04_1():
  78.     '''
  79.     参数1:input_size 每个词的词向量维度(输入层神经元的个数)
  80.     参数2:hidden_size 隐藏层神经元的个数
  81.     参数3:hidden_layer 隐藏层的层数
  82.     '''
  83.     rnn = nn.RNN(5, 6, 1)
  84.     '''
  85.     参数1:sequence_length 每个样本的句子长度
  86.     参数2:batch_size 每个批次的样本数量
  87.     参数3:input_size 每个词的词向量维度(输入层神经元的个数)
  88.     '''
  89.     input = torch.randn(4, 1, 5)
  90.     print(f'input {input}')
  91.     '''
  92.     参数1:hidden_layer 隐藏层的层数
  93.     参数2:batch_size 每个批次的样本数量
  94.     参数3:hidden_size 隐藏层神经元的个数
  95.     '''
  96.     # 每个样本一次性输入神经网络
  97.     hn = torch.zeros(1, 1, 6)
  98.     print(f'hn1 {hn}')
  99.     output, hn = rnn(input, hn)
  100.     print(f'output1 {output}')
  101.     print(f'hn1 {hn}')
  102.     print(f'RNN模型1 {rnn}')
  103.     print('*' * 80)
  104.     # 每个样本逐词送入神经网络
  105.     hn = torch.zeros(1, 1, 6)
  106.     print(f'hn2 {hn}')
  107.     for i in range(4):
  108.         tmp = input[i][0]
  109.         print(f'tmp.shape {tmp.shape}')
  110.         output, hn = rnn(tmp.unsqueeze(0).unsqueeze(0), hn)
  111.         print(f'{i}-output {output}')
  112.         print(f'{i}-hn {hn}')
  113. # todo:一个一个地向模型输入单词-全一初始化
  114. def dem04_2():
  115.     '''
  116.     参数1:input_size 每个词的词向量维度(输入层神经元的个数)
  117.     参数2:hidden_size 隐藏层神经元的个数
  118.     参数3:hidden_layer 隐藏层的层数
  119.     '''
  120.     rnn = nn.RNN(5, 6, 1)
  121.     '''
  122.     参数1:sequence_length 每个样本的句子长度
  123.     参数2:batch_size 每个批次的样本数量
  124.     参数3:input_size 每个词的词向量维度(输入层神经元的个数)
  125.     '''
  126.     input = torch.randn(4, 1, 5)
  127.     print(f'input {input}')
  128.     '''
  129.     参数1:hidden_layer 隐藏层的层数
  130.     参数2:batch_size 每个批次的样本数量
  131.     参数3:hidden_size 隐藏层神经元的个数
  132.     '''
  133.     # 每个样本一次性输入神经网络
  134.     hn = torch.ones(1, 1, 6)
  135.     print(f'hn1 {hn}')
  136.     output, hn = rnn(input, hn)
  137.     print(f'output1 {output}')
  138.     print(f'hn1 {hn}')
  139.     print(f'RNN模型1 {rnn}')
  140.     print('*' * 80)
  141.     # 每个样本逐词送入神经网络
  142.     hn = torch.ones(1, 1, 6)
  143.     print(f'hn2 {hn}')
  144.     for i in range(4):
  145.         tmp = input[i][0]
  146.         print(f'tmp.shape {tmp.shape}')
  147.         output, hn = rnn(tmp.unsqueeze(0).unsqueeze(0), hn)
  148.         print(f'{i}-output {output}')
  149.         print(f'{i}-hn {hn}')
  150. # todo:一个一个地向模型输入单词-随机初始化
  151. def dem04_3():
  152.     '''
  153.     参数1:input_size 每个词的词向量维度(输入层神经元的个数)
  154.     参数2:hidden_size 隐藏层神经元的个数
  155.     参数3:hidden_layer 隐藏层的层数
  156.     '''
  157.     rnn = nn.RNN(5, 6, 1)
  158.     '''
  159.     参数1:sequence_length 每个样本的句子长度
  160.     参数2:batch_size 每个批次的样本数量
  161.     参数3:input_size 每个词的词向量维度(输入层神经元的个数)
  162.     '''
  163.     input = torch.randn(4, 1, 5)
  164.     print(f'input {input}')
  165.     '''
  166.     参数1:hidden_layer 隐藏层的层数
  167.     参数2:batch_size 每个批次的样本数量
  168.     参数3:hidden_size 隐藏层神经元的个数
  169.     '''
  170.     # 每个样本一次性输入神经网络
  171.     hn = torch.randn(1, 1, 6)
  172.     print(f'hn1 {hn}')
  173.     output, hn = rnn(input, hn)
  174.     print(f'output1 {output}')
  175.     print(f'hn1 {hn}')
  176.     print(f'RNN模型1 {rnn}')
  177.     print('*' * 80)
  178.     # 每个样本逐词送入神经网络
  179.     hn = torch.randn(1, 1, 6)
  180.     print(f'hn2 {hn}')
  181.     for i in range(4):
  182.         tmp = input[i][0]
  183.         print(f'tmp.shape {tmp.shape}')
  184.         output, hn = rnn(tmp.unsqueeze(0).unsqueeze(0), hn)
  185.         print(f'{i}-output {output}')
  186.         print(f'{i}-hn {hn}')
  187. # todo:设置batch_first=True
  188. def dem05():
  189.     '''
  190.     参数1:input_size 每个词的词向量维度(输入层神经元的个数)
  191.     参数2:hidden_size 隐藏层神经元的个数
  192.     参数3:hidden_layer 隐藏层的层数
  193.     '''
  194.     rnn = nn.RNN(5, 6, 1, batch_first=True)
  195.     '''
  196.     参数1:batch_size 每个批次的样本数量
  197.     参数2:sequence_length 每个样本的句子长度
  198.     参数3:input_size 每个词的词向量维度(输入层神经元的个数)
  199.     '''
  200.     input = torch.randn(3, 4, 5)
  201.     '''
  202.     参数1:hidden_layer 隐藏层的层数
  203.     参数2:batch_size 每个批次的样本数量
  204.     参数3:hidden_size 隐藏层神经元的个数
  205.     '''
  206.     h0 = torch.randn(1, 3, 6)
  207.     output, hn = rnn(input, h0)
  208.     print(f'output {output}')
  209.     print(f'hn {hn}')
  210.     print(f'RNN模型 {rnn}')
  211. # todo:基础LSTM模型
  212. def dem06_1():
  213.     '''
  214.     参数1:input_size 每个词的词向量维度(输入层神经元的个数)
  215.     参数2:hidden_size 隐藏层神经元的个数
  216.     参数3:hidden_layer 隐藏层的层数
  217.     '''
  218.     rnn = nn.LSTM(5, 6, 2)
  219.     '''
  220.     参数1:batch_size 每个批次的样本数量
  221.     参数2:sequence_length 每个样本的句子长度
  222.     参数3:input_size 每个词的词向量维度(输入层神经元的个数)
  223.     '''
  224.     input = torch.randn(1, 3, 5)
  225.     '''
  226.     参数1:hidden_layer 隐藏层的层数
  227.     参数2:batch_size 每个批次的样本数量
  228.     参数3:hidden_size 隐藏层神经元的个数
  229.     '''
  230.     h0 = torch.randn(2, 3, 6)
  231.     '''
  232.     参数1:hidden_layer 隐藏层的层数
  233.     参数2:batch_size 每个批次的样本数量
  234.     参数3:hidden_size 隐藏层神经元的个数
  235.     '''
  236.     c0 = torch.randn(2, 3, 6)
  237.     output, (hn, cn) = rnn(input, (h0, c0))
  238.     print(f'output {output}')
  239.     print(f'hn {hn}')
  240.     print(f'cn {cn}')
  241. # todo:双向LSTM模型
  242. def dem06_2():
  243.     '''
  244.     参数1:input_size 每个词的词向量维度(输入层神经元的个数)
  245.     参数2:hidden_size 隐藏层神经元的个数
  246.     参数3:hidden_layer 隐藏层的层数
  247.     '''
  248.     rnn = nn.LSTM(5, 6, 2,bidirectional=True)
  249.     '''
  250.     参数1:batch_size 每个批次的样本数量
  251.     参数2:sequence_length 每个样本的句子长度
  252.     参数3:input_size 每个词的词向量维度(输入层神经元的个数)
  253.     '''
  254.     input = torch.randn(1, 3, 5)
  255.     '''
  256.     参数1:hidden_layer 隐藏层的层数
  257.     参数2:batch_size 每个批次的样本数量
  258.     参数3:hidden_size 隐藏层神经元的个数
  259.     '''
  260.     h0 = torch.randn(4, 3, 6)
  261.     '''
  262.     参数1:hidden_layer 隐藏层的层数
  263.     参数2:batch_size 每个批次的样本数量
  264.     参数3:hidden_size 隐藏层神经元的个数
  265.     '''
  266.     c0 = torch.randn(4, 3, 6)
  267.     output, (hn, cn) = rnn(input, (h0, c0))
  268.     print(f'output {output}')
  269.     print(f'hn {hn}')
  270.     print(f'cn {cn}')
  271. if __name__ == '__main__':
  272.     # dem01()
  273.     # dem02()
  274.     # dem03()
  275.     # dem04_1()
  276.     # dem04_2()
  277.     # dem04_3()
  278.     # dem05()
  279.     # dem06_1()
  280.     dem06_2()
复制代码
  1. D:\nlplearning\nlpbase\python.exe D:\nlpcoding\rnncode.py
  2. output tensor([[[ 0.0207, -0.1121, -0.0706,  0.1167, -0.3322, -0.0686],
  3.          [ 0.1256,  0.1328,  0.2361,  0.2237, -0.0203, -0.2709],
  4.          [-0.2668, -0.2721, -0.2168,  0.4734,  0.2420,  0.0349]]],
  5.        grad_fn=<MkldnnRnnLayerBackward0>)
  6. hn tensor([[[ 0.1501, -0.2106,  0.0213,  0.1309,  0.3074, -0.2038],
  7.          [ 0.3639, -0.0394, -0.1912,  0.1282,  0.0369, -0.1094],
  8.          [ 0.1217, -0.0517,  0.1884, -0.1100, -0.5018, -0.4512]],
  9.         [[ 0.0207, -0.1121, -0.0706,  0.1167, -0.3322, -0.0686],
  10.          [ 0.1256,  0.1328,  0.2361,  0.2237, -0.0203, -0.2709],
  11.          [-0.2668, -0.2721, -0.2168,  0.4734,  0.2420,  0.0349]]],
  12.        grad_fn=<StackBackward0>)
  13. cn tensor([[[ 0.2791, -0.7362,  0.0501,  0.2612,  0.4655, -0.2338],
  14.          [ 0.7902, -0.0920, -0.4955,  0.3865,  0.0868, -0.1612],
  15.          [ 0.2312, -0.3736,  0.4033, -0.1386, -1.0151, -0.5971]],
  16.         [[ 0.0441, -0.2279, -0.1483,  0.3397, -0.5597, -0.4339],
  17.          [ 0.2154,  0.4119,  0.4723,  0.4731, -0.0284, -1.1095],
  18.          [-0.5016, -0.5146, -0.4286,  1.5299,  0.5992,  0.1224]]],
  19.        grad_fn=<StackBackward0>)
  20. Process finished with exit code 0
复制代码
2 GRU
  1. import torch
  2. import torch.nn as nn
  3. # todo:基础GRU
  4. def dem01():
  5.     gru = nn.GRU(5, 6, 1)
  6.     input = torch.randn(4, 3, 5)
  7.     h0 = torch.randn(1, 3, 6)
  8.     output, hn = gru(input, h0)
  9.     print(f'output {output}')
  10.     print(f'hn {hn}')
  11. if __name__ == '__main__':
  12.     dem01()
复制代码
回复

使用道具 举报

登录后关闭弹窗

登录参与点评抽奖  加入IT实名职场社区
去登录
快速回复 返回顶部 返回列表