循环神经网络PyTorch版

循环神经网络(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)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

coder_jyz

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值