GRU¶
- class torchrl.modules.GRU(input_size: int, hidden_size: int, num_layers: int = 1, bias: bool = True, batch_first: bool = True, dropout: float = 0.0, bidirectional: bool = False, device=None, dtype=None)[原始碼]¶
一個用於執行多層 GRU 多步運算的 PyTorch 模組。該模組的行為與
torch.nn.GRU完全相同,但此實現純粹用 Python 編寫。注意
此類在不依賴 CuDNN 的情況下實現,這使其與
torch.vmap()和torch.compile()相容。示例
>>> import torch >>> from torchrl.modules.tensordict_module.rnn import GRU
>>> device = torch.device("cuda") if torch.cuda.device_count() else torch.device("cpu") >>> B = 2 >>> T = 4 >>> N_IN = 10 >>> N_OUT = 20 >>> N_LAYERS = 2 >>> V = 4 # vector size >>> gru = GRU( ... input_size=N_IN, ... hidden_size=N_OUT, ... device=device, ... num_layers=N_LAYERS, ... )
# 單次呼叫 >>> x = torch.randn(B, T, N_IN, device=device) >>> h0 = torch.zeros(N_LAYERS, B, N_OUT, device=device) >>> with torch.no_grad(): … h1 = gru(x, h0)
# 向量化呼叫 - nn.GRU 無法實現 >>> def call_gru(x, h): … h_out = gru(x, h) … return h_out >>> batched_call = torch.vmap(call_gru) >>> x = torch.randn(V, B, T, 10, device=device) >>> h0 = torch.zeros(V, N_LAYERS, B, N_OUT, device=device) >>> with torch.no_grad(): … h1 = batched_call(x, h0)
__init__(input_size,hidden_size,num_layers=1,bias=True,batch_first=False,dropout=0.0,bidirectional=False,device=None,dtype=None)
將多層門控迴圈單元 (GRU) RNN 應用於輸入序列。對於輸入序列中的每個元素,每一層計算以下函式:
\[\begin{split}\begin{array}{ll} r_t = \sigma(W_{ir} x_t + b_{ir} + W_{hr} h_{(t-1)} + b_{hr}) \\ z_t = \sigma(W_{iz} x_t + b_{iz} + W_{hz} h_{(t-1)} + b_{hz}) \\ n_t = \tanh(W_{in} x_t + b_{in} + r_t \odot (W_{hn} h_{(t-1)}+ b_{hn})) \\ h_t = (1 - z_t) \odot n_t + z_t \odot h_{(t-1)} \end{array}\end{split}\]其中 \(h_t\) 是時間 t 時的隱藏狀態,\(x_t\) 是時間 t 時的輸入,\(h_{(t-1)}\) 是第 t-1 時間或第 0 時間的初始隱藏狀態的層的隱藏狀態,\(r_t\)、\(z_t\) 和 \(n_t\) 分別是重置門、更新門和新門。\(\sigma\) 是 sigmoid 函式,\(\odot\) 是 Hadamard 積。
在多層 GRU 中,第 \(l\) 層(\(l \ge 2\))的輸入 \(x^{(l)}_t\) 是前一層隱藏狀態 \(h^{(l-1)}_t\) 乘以 dropout \(\delta^{(l-1)}_t\),其中每個 \(\delta^{(l-1)}_t\) 是一個伯努利隨機變數,其機率為
dropout時為 \(0\)。- 引數:
input_size – 輸入 x 中預期特徵的數量
hidden_size – 隱藏狀態 h 中的特徵數量
num_layers – 迴圈層數。例如,設定
num_layers=2將意味著堆疊兩個 GRU 來形成一個 堆疊 GRU,第二個 GRU 接收第一個 GRU 的輸出並計算最終結果。預設值:1bias – 如果為
False,則該層不使用偏置權重 b_ih 和 b_hh。預設值:Truebatch_first – 如果為
True,則輸入和輸出張量以 (batch, seq, feature) 的形式提供,而不是 (seq, batch, feature)。請注意,這不適用於隱藏狀態或單元狀態。有關詳細資訊,請參閱下方的輸入/輸出部分。預設值:Falsedropout – 如果非零,則在除最後一層外的每個 GRU 層的輸出上引入 Dropout 層,dropout 機率等於
dropout。預設值:0bidirectional – 如果為
True,則成為雙向 GRU。預設值:False
- 輸入:input, h_0
input: 對於未批處理的輸入,形狀為 \((L, H_{in})\);當
batch_first=False時,形狀為 \((L, N, H_{in})\);當batch_first=True時,形狀為 \((N, L, H_{in})\),包含輸入序列的特徵。輸入也可以是打包的可變長度序列。有關詳細資訊,請參閱torch.nn.utils.rnn.pack_padded_sequence()或torch.nn.utils.rnn.pack_sequence()。h_0: 形狀為 \((D * \text{num\_layers}, H_{out})\) 或 \((D * \text{num\_layers}, N, H_{out})\) 的張量,包含輸入序列的初始隱藏狀態。如果未提供,則預設為零。
其中
\[\begin{split}\begin{aligned} N ={} & \text{batch size} \\ L ={} & \text{sequence length} \\ D ={} & 2 \text{ if bidirectional=True otherwise } 1 \\ H_{in} ={} & \text{input\_size} \\ H_{out} ={} & \text{hidden\_size} \end{aligned}\end{split}\]- 輸出:output, h_n
output: 對於未批處理的輸入,形狀為 \((L, D * H_{out})\);當
batch_first=False時,形狀為 \((L, N, D * H_{out})\);當batch_first=True時,形狀為 \((N, L, D * H_{out})\),包含 GRU 最後一層的輸出特徵 (h_t),適用於每個 t。如果輸入是torch.nn.utils.rnn.PackedSequence,則輸出也將是打包序列。h_n: 形狀為 \((D * \text{num\_layers}, H_{out})\) 或 \((D * \text{num\_layers}, N, H_{out})\) 的張量,包含輸入序列的最終隱藏狀態。
- 變數:
weight_ih_l[k] – 第 \(\text{k}^{th}\) 層的可學習輸入-隱藏權重 (W_ir|W_iz|W_in),對於 k = 0,形狀為 (3*hidden_size, input_size)。否則,形狀為 (3*hidden_size, num_directions * hidden_size)
weight_hh_l[k] – 第 \(\text{k}^{th}\) 層的可學習隱藏-隱藏權重 (W_hr|W_hz|W_hn),形狀為 (3*hidden_size, hidden_size)
bias_ih_l[k] – 第 \(\text{k}^{th}\) 層的可學習輸入-隱藏偏置 (b_ir|b_iz|b_in),形狀為 (3*hidden_size)
bias_hh_l[k] – 第 \(\text{k}^{th}\) 層的可學習隱藏-隱藏偏置 (b_hr|b_hz|b_hn),形狀為 (3*hidden_size)
注意
所有權重和偏置都從 \(\mathcal{U}(-\sqrt{k}, \sqrt{k})\) 初始化,其中 \(k = \frac{1}{\text{hidden\_size}}\)
注意
對於雙向 GRU,前向和後向分別為方向 0 和 1。例如,當
batch_first=False時,分割輸出層的方式為:output.view(seq_len, batch, num_directions, hidden_size)。注意
對於未批處理的輸入,
batch_first引數被忽略。注意
新門 \(n_t\) 的計算與原始論文和其他框架略有不同。在原始實現中,\(r_t\) 和前一個隱藏狀態 \(h_{(t-1)}\) 之間的 Hadamard 積 \((\odot)\) 在與權重矩陣 W 相乘並加上偏置之前進行。
\[\begin{aligned} n_t = \tanh(W_{in} x_t + b_{in} + W_{hn} ( r_t \odot h_{(t-1)} ) + b_{hn}) \end{aligned}\]這與 PyTorch 的實現相反,PyTorch 的實現是在 \(W_{hn} h_{(t-1)}\) 之後進行的。
\[\begin{aligned} n_t = \tanh(W_{in} x_t + b_{in} + r_t \odot (W_{hn} h_{(t-1)}+ b_{hn})) \end{aligned}\]為了效率,此實現方式特意有所不同。
示例
>>> rnn = nn.GRU(10, 20, 2) >>> input = torch.randn(5, 3, 10) >>> h0 = torch.randn(2, 3, 20) >>> output, hn = rnn(input, h0)