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)[source]¶
用于执行多层 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\) 层的输入 \(x^{(l)}_t\) (\(l \ge 2\)) 是前一层的隐藏状态 \(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。默认值:True
batch_first – 如果为
True
,则输入和输出张量以 (batch, seq, feature) 而不是 (seq, batch, feature) 的形式提供。请注意,这不适用于隐藏状态或单元状态。有关详细信息,请参阅下面的输入/输出部分。默认值:False
dropout – 如果非零,则在每个 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)