RNN循环神经网络

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')
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值