循环神经网络(RNN)
申明:本书的理论和代码来自于开源书籍,TensorFlow深度学习
地址:https://github.com/jiajunhua/dragen1860-Deep-Learning-with-TensorFlow-book
1. Pytorch的RNN简单实现
2.1. 单层RNNCell
[s,b,n], s为句子长度,b为句子数量,n为词向量长度,h为状态长度
import torch
from torch import nn
from torch import optim
from torch.nn import functional as F
# [s,b,n] s:句长,b:句子数,n:词向量长度
x = torch.randn(80, 4, 100)
# param1:input_size | param2:hidden_size
cell1 = nn.RNNCell(100, 64)
# [batch, hidden_size]
h1 = torch.zeros(4, 64)
# xt [batch, input_size]
for xt in x:
h1 = cell1(xt, h1)
print(h1.shape) # [4, 64]
2.2. 多层 RNNCell
x = torch.randn(80, 4, 100)
cell1 = nn.RNNCell(100, 64)
cell2 = nn.RNNCell(64, 20)
h1 = torch.zeros(4, 64)
h2 = torch.zeros(4, 20)
for xt in x:
h1 = cell1(xt, h1)
h2 = cell2(h1, h2)
print(h2.shape) # [4, 20]
2.3 RNN
# 单层
rnn = nn.RNN(input_size=100, hidden_size=64, num_layers=1)
x = torch.randn(80, 4, 100)
out, h = rnn(x, torch.zeros(1, 4, 64))
print(out.shape, h.shape) # torch.Size([80, 4, 64]) torch.Size([1, 4, 64])
# 多层
rnn = nn.RNN(input_size=100, hidden_size=64, num_layers=3)
x = torch.randn(80, 4, 100)
out, h = rnn(x, torch.zeros(3, 4, 64))
print(out.shape, h.shape)