PyTorch Lecture12:RNN1 - Basics

代码1

import torch
import torch.nn as nn
from torch.autograd import Variable

# One hot encoding for each char in 'hello'
h = [1, 0, 0, 0]
e = [0, 1, 0, 0]
l = [0, 0, 1, 0]
o = [0, 0, 0, 1]

# One cell RNN input_dim(4) -> output_dim(2). sequence:5
cell = nn.RNN(input_size=4, hidden_size=2, batch_first=True)

# (num_layers * num_directions, batch,hidden_size) whether batch_first = True or False
hidden = Variable(torch.randn(1, 1, 2))
# Propagate input through RNN
# Input: (batch, seq_len, input_size) when batch_first=True
inputs = Variable(torch.Tensor([h, e, l, l, o]))
for one in inputs:
    one = one.view(1, 1, -1)
    # Input: (batch, seq_len, input_size) when batch_first=True
    out, hidden = cell(one, hidden)
    print("one input size", one.size(), "out.size", out.size())

# We can do the whole at once
# propagate input through RNN
# Input :(batch,seq_len,input_size) when batch_first =True
inputs=inputs.view(1,5,-1)
out,hidden=cell(inputs,hidden)
print("sequence input  size",inputs.size(),"out size",out.size())

# hidden:(num_layers * num_directions, batch,hidden_size) whether batch_first =True or False
hidden =Variable(torch.randn(1,3,2))

# One cell RNN input-dim(4) ->output_dim(2). sequence:5, batch 3
# 3 batches 'hello','eolll','lleel'
# rank =(3,5,4)

inputs=Variable(torch.Tensor([[h,e,l,l,o],
                             [e,o,l,l,l],
                             [l,l,e,e,l]]))

# Propagate input through RNN
# Input:(batch,seq_len.input_size) when batch_first=True
# B*S*I

out,hidden=cell(inputs,hidden)
print("batch input size",inputs.size(),"out size",out.size())

# One cell RNN input_dim(4) ->output_dim(2)
cell =nn.RNN(input_size=4,hidden_size=2)

# The given dimensions dim0 and dim1 are swapped.

inputs=inputs.transpose(0,1)
# Propagate input through RNN
# Input: (seq_len, batch_size, input_size) when batch_first=False (default)
# S x B x I
out,hidden =cell(inputs,hidden)
print("batch input size",inputs.size(),"out size",out.size())

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值