『PyTorch』第十彈_循環神經網絡
阿新 • • 發佈:2018-02-28
() rom tac stack 區別 div .com and tput
『cs231n』作業3問題1選講_通過代碼理解RNN&圖像標註訓練
對於torch中的RNN相關類,有原始和原始Cell之分,其中RNN和RNNCell層的區別在於前者一次能夠處理整個序列,而後者一次只處理序列中一個時間點的數據,前者封裝更完備更易於使用,後者更具靈活性。實際上RNN層的一種後端實現方式就是調用RNNCell來實現的。
一、nn.RNN
import torch as t from torch import nn from torch.autograd import Variable as V layer = 1 t.manual_seed(1000) # batch為3,step為2,每個元素4維 input = V(t.randn(2,3,4)) # 1層,3隱藏神經元,每個元素4維 lstm = nn.LSTM(4,3,layer) # 初始狀態:1層,batch為3,隱藏神經元3 h0 = V(t.randn(layer,3,3)) c0 = V(t.randn(layer,3,3)) out, hn = lstm(input,(h0,c0)) print(out, hn)
Variable containing: (0 ,.,.) = 0.0545 -0.0061 0.5615 -0.1251 0.4490 0.2640 0.1405 -0.1624 0.0303 (1 ,.,.) = 0.0168 0.1562 0.5002 0.0824 0.1454 0.4007 0.0180 -0.0267 0.0094 [torch.FloatTensor of size 2x3x3] (Variable containing: (0 ,.,.) = 0.0168 0.1562 0.5002 0.0824 0.1454 0.4007 0.0180 -0.0267 0.0094 [torch.FloatTensor of size 1x3x3] , Variable containing: (0 ,.,.) = 0.1085 0.1957 0.9778 0.5397 0.2874 0.6415 0.0480 -0.0345 0.0141 [torch.FloatTensor of size 1x3x3] )
二、nn.RNNCell
import torch as t from torch import nn from torch.autograd import Variable as V t.manual_seed(1000) # batch為3,step為2,每個元素4維 input = V(t.randn(2,3,4)) # Cell只能是1層,3隱藏神經元,每個元素4維 lstm = nn.LSTMCell(4,3) # 初始狀態:1層,batch為3,隱藏神經元3 hx = V(t.randn(3,3)) cx = V(t.randn(3,3)) out = [] # 每個step提取各個batch的四個維度 for i_ in input: print(i_.shape) hx, cx = lstm(i_,(hx,cx)) out.append(hx) t.stack(out)
torch.Size([3, 4]) torch.Size([3, 4])Variable containing: (0 ,.,.) = 0.0545 -0.0061 0.5615 -0.1251 0.4490 0.2640 0.1405 -0.1624 0.0303 (1 ,.,.) = 0.0168 0.1562 0.5002 0.0824 0.1454 0.4007 0.0180 -0.0267 0.0094 [torch.FloatTensor of size 2x3x3]
三、nn.Embedding
embedding將標量表示的字符(所以是LongTensor)轉換成矢量,這裏給出一個模擬:將標量詞embedding後送入rnn轉換一下維度。
# 5個詞,每個詞使用4維向量表示 embedding = nn.Embedding(5,4) # 使用預訓練好的詞向量初始化 embedding.weight.data = t.arange(0,20).view(5,4) # embedding將標量表示的字符(所以是LongTensor)轉換成矢量 # 實際輸入詞原始向量需要是l、LongTensor格式 input = V(t.arange(3,0,-1)).long() # 1個batch,3個step,4維矢量 input = embedding(input).unsqueeze(1) print(input) # 1層,3隱藏神經元(輸出元素4維度),每個元素4維 layer = 1 lstm = nn.LSTM(4,3,layer) # 初始狀態:1層,batch為3,隱藏神經元3 h0 = V(t.randn(layer,3,3)) c0 = V(t.randn(layer,3,3)) out, hn = lstm(input,(h0,c0)) print(out)
Variable containing: (0 ,.,.) = 12 13 14 15 (1 ,.,.) = 8 9 10 11 (2 ,.,.) = 4 5 6 7 [torch.FloatTensor of size 3x1x4] Variable containing: (0 ,.,.) = -0.6222 -0.0156 0.0266 0.1910 0.0026 0.0061 -0.5823 -0.0042 0.0932 (1 ,.,.) = 0.3199 -0.0243 0.1561 0.8229 0.0051 0.1269 0.3715 -0.0043 0.1704 (2 ,.,.) = 0.7893 -0.0398 0.2963 0.8835 0.0113 0.2767 0.8004 -0.0044 0.2982 [torch.FloatTensor of size 3x3x3]
『PyTorch』第十彈_循環神經網絡