import torch
import torch.nn as nn
# 应用场景
# 数据:[3,1,5] 3个批次 每个批次1个单词 每个单词5个特征
# 模型 :经过模型处理能变6个特征
# 探究:9个参数中 主参数和辅助参数的关系
def dm01_rnn_for_base():
# 1 定义模型
# 第1个参数:5 输入数据的尺寸
# 第2个参数:6 输出数据的尺寸
# 第3个参数:隐藏层的个数(隐藏层的个数*方向数)
myrnn = nn.RNN(5, 6, 1) #A
print('myrnn-->', myrnn)
# 2 准备数据 input h0
# 第1个参数:1 单词数 seq_len 句子长度
# 第2个参数:3 批次数
# 第3个参数:5 每个单词的特征数 # 3个句话 每句话1个单词,每个单词有5个特征
input = torch.randn(1, 3, 5) #B
# 准备数据h0
# 第1个参数:1 隐藏层个数
# 第2个参数:1 批次数
# 第3个参数:6 神经元的个数
h0 = torch.randn(1, 3, 6) #C
# 3 给模型喂数据 output hn input[1,3,5], h0[1,3,6] ---> output[1,3,6],hn[1,3,6]
output, hn = myrnn(input, h0)
print('output.shape-->', output.shape)
print('hn.shape-->', hn.shape)
def dm02_rnn_for_inputdim():
# 1 定义模型
# 第1个参数:5 输入数据的尺寸
# 第2个参数:6 输出数据的尺寸
# 第3个参数:隐藏层的个数(隐藏层的个数*方向数)
myrnn = nn.RNN(55, 6, 1) #A
print('myrnn-->', myrnn)
# 2 准备数据 input h0
# 第1个参数:1 单词数 seq_len 句子长度
# 第2个参数:3 批次数
# 第3个参数:5 每个单词的特征数 # 3个句话 每句话1个单词,每个单词有5个特征
input = torch.randn(1, 3, 55) #B
# 准备数据h0
# 第1个参数:1 隐藏层个数
# 第2个参数:1 批次数
# 第3个参数:6 神经元的个数
h0 = torch.randn(1, 3, 6) #C
# 3 给模型喂数据 output hn
# input[1,3,55], h0[1,3,6] ---> output[1,3,6],hn[1,3,6]
output, hn = myrnn(input, h0)
print('output.shape-->', output.shape)
print('hn.shape-->', hn.shape)
def dm022_rnn_for_inputdim():
# 1 定义模型
# 第1个参数:5 输入数据的尺寸
# 第2个参数:6 输出数据的尺寸
# 第3个参数:隐藏层的个数(隐藏层的个数*方向数)
myrnn = nn.RNN(5, 66, 1) #A
print('myrnn-->', myrnn)
# 2 准备数据 input h0
# 第1个参数:1 单词数 seq_len 句子长度
# 第2个参数:3 批次数
# 第3个参数:5 每个单词的特征数 # 3个句话 每句话1个单词,每个单词有5个特征
input = torch.randn(1, 3, 5) #B
# 准备数据h0
# 第1个参数:1 隐藏层个数
# 第2个参数:1 批次数
# 第3个参数:6 神经元的个数
h0 = torch.randn(1, 3, 66) #C
# 3 给模型喂数据 output hn
# input[1,3,5], h0[1,3,66] ---> output[1,3,66],hn[1,3,66]
output, hn = myrnn(input, h0)
print('output.shape-->', output.shape)
print('hn.shape-->', hn.shape)
def dm03_rnn_for_sequencelen():
# 1 定义模型
# 第1个参数:5 输入数据的尺寸
# 第2个参数:6 输出数据的尺寸
# 第3个参数:隐藏层的个数(隐藏层的个数*方向数)
myrnn = nn.RNN(5, 6, 1) #A
print('myrnn-->', myrnn)
# 2 准备数据 input h0
# 第1个参数:1 单词数 seq_len 句子长度
# 第2个参数:3 批次数
# 第3个参数:5 每个单词的特征数 # 3个句话 每句话1个单词,每个单词有5个特征
input = torch.randn(11, 3, 5) #B
# 准备数据h0
# 第1个参数:1 隐藏层个数
# 第2个参数:1 批次数
# 第3个参数:6 神经元的个数
h0 = torch.randn(1, 3, 6) #C
# 3 给模型喂数据 output hn
# input[11,3,5], h0[1,3,6] ---> output[11,3,6],hn[1,3,6]
output, hn = myrnn(input, h0)
print('output.shape-->', output.shape)
print('hn.shape-->', hn.shape)
def dm04_rnn_for_batchsize():
# 1 定义模型
# 第1个参数:5 输入数据的尺寸
# 第2个参数:6 输出数据的尺寸
# 第3个参数:隐藏层的个数(隐藏层的个数*方向数)
myrnn = nn.RNN(5, 6, 1) #A
print('myrnn-->', myrnn)
# 2 准备数据 input h0
# 第1个参数:1 单词数 seq_len 句子长度
# 第2个参数:3 批次数
# 第3个参数:5 每个单词的特征数 # 3个句话 每句话1个单词,每个单词有5个特征
input = torch.randn(1, 33, 5) #B
# 准备数据h0
# 第1个参数:1 隐藏层个数
# 第2个参数:1 批次数
# 第3个参数:6 神经元的个数
h0 = torch.randn(1, 33, 6) #C
# 3 给模型喂数据 output hn
# input[1,33,5], h0[1,33,6] ---> output[1,33,6],hn[1,33,6]
output, hn = myrnn(input, h0)
print('output.shape-->', output.shape)
print('hn.shape-->', hn.shape)
###
def dm05_rnn_for_hiddennum():
# 1 定义模型
# 第1个参数:5 输入数据的尺寸
# 第2个参数:6 输出数据的尺寸
# 第3个参数:隐藏层的个数(隐藏层的个数*方向数)
myrnn = nn.RNN(5, 6, 2) #A
print('myrnn-->', myrnn)
# 2 准备数据 input h0
# 第1个参数:1 单词数 seq_len 句子长度
# 第2个参数:3 批次数
# 第3个参数:5 每个单词的特征数 # 3个句话 每句话1个单词,每个单词有5个特征
input = torch.randn(1, 3, 5) #B
# 准备数据h0
# 第1个参数:1 隐藏层个数
# 第2个参数:1 批次数
# 第3个参数:6 神经元的个数
h0 = torch.randn(2, 3, 6) #C
# 3 给模型喂数据 output hn
# input[1,3,5], h0[2,3,6] ---> output[1,3,6], hn[2,3,6]
output, hn = myrnn(input, h0)
print('output.shape-->', output.shape, output)
print('hn.shape-->', hn.shape, hn)
# 当隐藏层个数=1 则output和hn是相等的
# 当隐藏层个数=2 则output的输出 和 hn1 hn2 和U最后一个3*6 一样!
if __name__ == '__main__':
# dm01_rnn_for_base()
# dm02_rnn_for_inputdim()
# dm022_rnn_for_inputdim()
# dm03_rnn_for_sequencelen()
# dm04_rnn_for_batchsize()
dm05_rnn_for_hiddennum()
# dm06_rnn_for_batch_first()
# dm07_rnn_for_multinum()
print('RNN End')
RNN循环神经网络
最新推荐文章于 2025-08-22 21:46:23 发布