RNN
import torch
import torch.nn as nn
rnn = nn.RNN(5, 6, 1)
# 维度为5,每个隐藏层6个神经元,1个隐藏层
input = torch.randn(1, 3, 5)
# 批次样本数量为1,序列长度为3,维度为5
h0 = torch.randn(1, 3, 6)
# 1个隐藏层,序列长度为3,每个隐藏层6个神经元
output, hn = rnn(input, h0)
# 输出部分 = rnn(输入部分)
print(output)
print(hn)
print(output.shape)
print(hn.shape)
"""
tensor([[[-0.1758, 0.3392, 0.7851, 0.0583, 0.9421, -0.6396],
[ 0.7938, 0.3311, -0.6934, -0.9418, 0.9247, -0.3842],
[-0.4600, 0.7236, -0.5175, 0.2813, -0.5300, 0.3985]]],
grad_fn=<StackBackward0>)
tensor([[[-0.1758, 0.3392, 0.7851, 0.0583, 0.9421, -0.6396],
[ 0.7938, 0.3311, -0.6934, -0.9418, 0.9247, -0.3842],
[-0.4600, 0.7236, -0.5175, 0.2813, -0.5300, 0.3985]]],
grad_fn=<StackBackward0>)
torch.Size([1, 3, 6])
torch.Size([1, 3, 6])
"""
LSTM
import torch.nn as nn
import torch
lstm = nn.LSTM(5, 6, 2)
# 维度为5,每个隐藏层6个神经元,2个隐藏层
input = torch.randn(1, 3, 5)
# 批次样本数量为1,序列长度为3,维度为5
h0 = torch.randn(2, 3, 6)# 初始化 LSTM 的初始隐藏状态
# 1个隐藏层,序列长度为3,每个隐藏层6个神经元
c0 = torch.randn(2, 3, 6)# 初始化 LSTM 的初始细胞状态
# 2个隐藏层,序列长度为3,每个隐藏层6个神经元
output, (hn, cn) = lstm(input, (h0, c0))
# 输出部分 = lstm(输入部分)
print(output)
print(hn)
print(cn)
print(output.shape)
print(hn.shape)
print(cn.shape)
"""
tensor([[[ 0.2465, 0.4144, 0.2912, -0.0157, -0.0693, 0.1067],
[-0.0261, -0.5055, -0.4013, 0.4995, -0.5971, -0.0331],
[-0.2773, 0.0694, 0.1586, -0.1511, -0.0051, -0.4707]]],
grad_fn=<StackBackward0>)
tensor([[[-0.1957, 0.0412, -0.5626, 0.2152, 0.3757, 0.2379],
[-0.0030, 0.0779, 0.1966, 0.4887, 0.2604, 0.3688],
[ 0.0533, -0.1841, -0.0854, 0.3123, 0.2481, 0.3225]],
[[ 0.2465, 0.4144, 0.2912, -0.0157, -0.0693, 0.1067],
[-0.0261, -0.5055, -0.4013, 0.4995, -0.5971, -0.0331],
[-0.2773, 0.0694, 0.1586, -0.1511, -0.0051, -0.4707]]],
grad_fn=<StackBackward0>)
tensor([[[-0.5586, 0.1939, -1.4773, 0.4838, 0.5345, 0.4465],
[-0.0109, 0.1245, 0.2840, 0.6625, 1.6524, 0.5431],
[ 0.1326, -0.4249, -0.1355, 1.0727, 0.4292, 0.5769]],
[[ 0.4582, 0.8678, 0.6479, -0.0381, -0.1044, 0.2260],
[-0.0364, -0.7348, -0.7428, 1.0751, -1.3216, -0.0771],
[-0.4357, 0.1676, 0.5697, -0.5810, -0.0135, -0.7366]]],
grad_fn=<StackBackward0>)
torch.Size([1, 3, 6])
torch.Size([2, 3, 6])
torch.Size([2, 3, 6])
"""
GRU
import torch
import torch.nn as nn
gru=nn.GRU(5,6,2)
# 维度为5,每个隐藏层6个神经元,2个隐藏层
input1=torch.randn(1,3,5)
# 批次样本数量为1,序列长度为3,维度为5
h0=torch.randn(2,3,6)
# 2个隐藏层,序列长度为3,每个隐藏层6个神经元
output,hn = gru(input1,h0)
# 输出部分 = gru(输入部分)
print(output)
print(hn)
print(output.shape)
print(hn.shape)
"""
tensor([[[ 0.9017, -0.8316, -0.7745, 0.3363, -0.4152, -0.2663],
[-0.1789, 0.3301, 0.5574, 0.1021, -0.1050, -0.8172],
[-0.3017, 0.4322, 0.1236, 0.5446, -0.2009, -0.3321]]],
grad_fn=<StackBackward0>)
tensor([[[-0.5085, 0.8401, -0.1227, -1.2665, 0.2676, -0.7743],
[-0.0389, -0.3141, -0.4438, 0.7459, -0.2061, 0.5883],
[ 0.1583, -0.3947, 0.2210, 0.8555, 0.6499, -0.5270]],
[[ 0.9017, -0.8316, -0.7745, 0.3363, -0.4152, -0.2663],
[-0.1789, 0.3301, 0.5574, 0.1021, -0.1050, -0.8172],
[-0.3017, 0.4322, 0.1236, 0.5446, -0.2009, -0.3321]]],
grad_fn=<StackBackward0>)
torch.Size([1, 3, 6])
torch.Size([2, 3, 6])
"""