快捷方式

RNNCell

class torch.nn.RNNCell(input_size, hidden_size, bias=True, nonlinearity='tanh', device=None, dtype=None)[source][source]

帶有 tanh 或 ReLU 非線性的 Elman RNN Cell。

h=tanh(Wihx+bih+Whhh+bhh)h' = \tanh(W_{ih} x + b_{ih} + W_{hh} h + b_{hh})

如果 nonlinearity‘relu’,則使用 ReLU 代替 tanh。

引數
  • input_size (int) – 輸入 x 中預期特徵的數量

  • hidden_size (int) – 隱狀態 h 中的特徵數量

  • bias (bool) – 如果為 False,則該層不使用偏置權重 b_ihb_hh。預設值:True

  • nonlinearity (str) – 要使用的非線性啟用函式。可以是 'tanh''relu'。預設值:'tanh'

輸入:input, hidden
  • input: 包含輸入特徵的張量

  • hidden: 包含初始隱狀態的張量。如果未提供,則預設為零。

輸出:h’
  • 形狀為 (batch, hidden_size)h’: 包含批次中每個元素的下一個隱狀態的張量

形狀
  • input: (N,Hin)(N, H_{in}) or (Hin)(H_{in}) tensor containing input features where HinH_{in} = input_size.

  • hidden: (N,Hout)(N, H_{out}) or (Hout)(H_{out}) tensor containing the initial hidden state where HoutH_{out} = hidden_size。如果未提供,則預設為零。

  • output: (N,Hout)(N, H_{out}) or (Hout)(H_{out}) 包含下一個隱狀態的張量。

變數
  • weight_ih (torch.Tensor) – 可學習的輸入到隱層權重,形狀為 (hidden_size, input_size)

  • weight_hh (torch.Tensor) – 可學習的隱層到隱層權重,形狀為 (hidden_size, hidden_size)

  • bias_ih – 可學習的輸入到隱層偏置,形狀為 (hidden_size)

  • bias_hh – 可學習的隱層到隱層偏置,形狀為 (hidden_size)

注意

所有權重和偏置均從 U(k,k)\mathcal{U}(-\sqrt{k}, \sqrt{k}) 中初始化,其中 k=1hidden_sizek = \frac{1}{\text{hidden\_size}}

示例

>>> rnn = nn.RNNCell(10, 20)
>>> input = torch.randn(6, 3, 10)
>>> hx = torch.randn(3, 20)
>>> output = []
>>> for i in range(6):
...     hx = rnn(input[i], hx)
...     output.append(hx)

文件

查閱 PyTorch 的全面開發者文件

檢視文件

教程

獲取針對初學者和高階開發者的深入教程

檢視教程

資源

查詢開發資源並獲得問題解答

檢視資源